Access specific composite data in composite pattern

Suppose I have an Item class which is the base class of a composite pattern.

I have two specific subclasses Movie and Music containing different data :

  • Movie : director, producer, list of actors ...
  • Music : chanteur, number of record sold ...

If I want to access Movie data (for example) in a client code, I need to downcast the Item corresponding to it because getProducer() is not a method of the Item class.

I am stuck between three different approach :

  1. Put all the subclass methods in the Item interface
  2. Downcast Item in client code
  3. Use an object that would extract the data from the different Item (using a kind of visitor pattern)

The third proposition seems interesting because it hides Item subclasses to client, but I don't know how to let client update an Item (for example change the producer of a movie).

The Item subclasse types supported by the application will grow through releases, and I want to design it to facilitate those type additions.

Maybe I am totally going in the wrong way and there is a different and better solution.


Option 1 is a bad idea, simply because getProducer() does not belong to an item.

Option 3 would work if you'd also proved setters in addition to the getter methods you were planning to implement there.

Option 2 sounds the most reasonable to me. Casting is a common task in a polymorphic solutions and in your case it is adequate.

IMO, Movie and Music objects are completely different and should not be derived from a common base class. But, if you still want to do it, modify the Item class as shown below (Just an example code. please do not use this code as-is)

abstract class Item
    virtual object GetData(string sDataID);

Now, in the derived classes you can override this method something like below.

class Movie: Item
    private Producer m_Producer = new Producer();
    override object GetData(string sDataID)
        object ret = null;
            case "Producer":
               ret = m_Producer;

And, in the client code you can do something like below.

Item objItem = new Movie();
Producer objProducer = (Producer)objItem.GetData("Producer");

You get the idea!

Need Your Help

100% width fixed div has left margin only in IE7

html css internet-explorer css3 internet-explorer-7

I have this piece of code below, and it works fine in every modern browser and in Internet Explorer for the most part, but not in IE7.

Unable to use h:components to display text in JQueryUI Dialog

jquery-ui jsf jsp-tags

I'm trying to create JQueryUI dialog in JSF. I have included the dialog code inside the < div > < /div > in the parent page as shown in JQueryUI Dialog demo

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.