Jump to content

Recommended Posts

Hello,

 

I have a question about data association.  I think the easiest way to explain it would be an example. 

 

Pretend that I am making a program to keep track of cars, and I have a "CarModel" class that simply holds data for a particular car model.

 

Now I want to keep track of a lot of information about the colors these models come in.  I want to know the names of the colors, sure, but I also want a swatch image of the color, and a hex code for the color, and a color ID that comes from the manufacturer.  So I make a "CarColor" class, and give it these properties.

 

CarColor

    > color name

    > color hex code

    > path to a swatch image of color

    > color ID from manufacturer

 

and my CarModel class:

    > manufacturer

    > model

    > year

    > array colors: array of CarColor objects (a list of all the colors that the car is available in)

 

So this is fine and good - I'm happy with this so far.  But now I decide that I want an image of the whole car in each color, not just a swatch image of the color.  So there are two ways I could do this.  I could add a property to the CarColor class; that would be easiest:

 

CarColor

    > color name

    > color hex code

  > path to an image of the whole car in this color

    > path to a swatch image of color

    > color ID from manufacturer

 

This is easy because it's stored right with the other color data, so it's nice and neat and tidy.  BUT, what if I want to pass that color data around to another car model?  Then those images wouldn't apply any more - they would be pictures of the right color, but the wrong car!  (I don't forsee needing to do this, but maybe some day...who knows?)  So perhaps I should instead put my colorful pictures in the CarModel class:

 

CarModel class:

    > manufacturer

    > model

    > year

    > array colors: array of CarColor objects

    > array of color images: keys point to a color object, values contain links to images of the whole car in each color

 

 

So, what do you think?  The second method takes more work - I need to have some way to connect the images to the colors - but it is also more in-line with the data that the classes are supposed to represent.  Pictures of the car belong to the CarModel class more logically than they belong to the CarColor class.

 

Thanks for any thoughts!

-Ryan S

 

Link to comment
https://forums.phpfreaks.com/topic/205190-where-does-data-belong/
Share on other sites

How about you make a CarImage separate from both Model and Color? In CarModel you can have (array) property 'pictures', where you can store images of given model, in CarColor, you can also have property 'pictures' where you can store imeges depicting given color.

Yes, that sounds like a good organization system.  So, you are saying that the CarModel class could contain an array of all the images for that model, regardless of color.  Then the Color objects within that CarModel would contain images for that specific color for that specific model.  That way, the data would be in both places, and it would be a fine way to organize it, and easy to get at.  If you just want to get all the images for the CarModel, look in its images list.  If you want a specific color, go to the Color object.

 

I can see that working!  I know space is not a huge issue unless you are designing a fast and optimized program, but does it matter at all that the images are repeated?  I suppose, since they are objects, you could even avoid this by using object "references" so the CarModel and the CarColor point to the same CarImage objects.

 

If I'm understanding you right, your method gives you some cool functionality - again, being able to get all the model images or get a specific color image - but for me,  I don't need to get a list of all the pictures for a model, I only need to get specific color images.  My script will ask the CarModel object for a picture of the car in blue, and the object will return the proper image.  So I only need those images in one place - I'm just not sure if those images should go in the CarColor class or directly in the CarModel class.

 

Just to make sure I'm not mis-understanding you, is this what you are suggesting?

 

CarModel:

    > normal stuff...

    > colors array (each color does contain the car image - for example, the blue object contains a picture of the car in blue)

    > images array - a list of all the images of this car model; this list contains pictures of various colors

 

Thanks for posting!  you are quick

 

I think it would be best to have a CarImage with a class variable of type CarColor as that makes the most sense. This allows you to query the color of the car in the image. It does not make much sense to put the CarColor in the CarModel because as the CarImage changes, so does the CarColor.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.