views:

275

answers:

2

the leaf in the composite implements the Component that has the Add Remove and GetChild methods that he is never going to use (violation of the Interface segregation)

so is the usage of composite pattern SOLID ?

link to composite http://www.dofactory.com/Patterns/PatternComposite.aspx

+1  A: 

The real smell in the pattern as depicted in your link and most books is that Component has the methods of a Composite. I think this is probably because the pattern is fairly old and has been repeated that way for years. My take is that only the Composite should have any methods related to compositing.

I once converted a board game over to a computer game. The playing pieces were placed on a map of earth, divided up into hexagons. 99% of all hexagons represented a single location. Unfortunately, a few of the hexagons contained multiple locations, for example, some had a couple islands inside them. I used the composite pattern to represent these locations, but not as depicted on your link. It was something like this (in Java):

public interface Location {
   Set<Army> getArmies();
}

public class SingleLocation implements Location {

   public Set<Army> getArmies() {
      return armies ;
   }

   private Set<Army> armies = new HashSet<Army>();
}

public class CompositeLocation implements Location {

   public Set<Army> getArmies() {

      Set<Army> armies = new HashSet<Army>();

      for(Location subLocation: subLocations) {
         armies.addAll(subLocation.getArmies());
      }

      return armies;
   }

   public void addSubLocation(Location location) {
      subLocations.add(location);
   }

   private Set<Location> subLocations = new HashSet<Location>();
}

Note that only the Composite has compositing methods, and doesn't even expose the fact that it has children to most clients (in this example, the client only wants a list of Armies from a location - the fact that they are at many sub-locations is irrelevant).

Keep in mind design patterns are not set-in-stone things you must implement exactly. Think of them as recipes. When you follow a recipe while cooking, you certainly can just follow it exactly. However, some cooks will throw in their own twists on the recipe. Others won't even look at it because they are experts and can throw something together in the spirit of the recipe without even thinking about it. The same goes for design patterns. They are malleable recipes.

You can also take those SOLID principles too far. If you read Robert Martin's articles, he states that applying the principles across the board without any thought will yield overly complex code. Software is designed through a series of trade-offs and balancings - sometimes you forgo pure SOLID because it yields cleaner less complex code. If you were to make your code perfectly encapsulated, flexible, decoupled, etc., you will have invented a new programming language :-)

SingleShot
That's very common, for good reasons - but there are reasons for the standard version. For example, the items may have functional dependencies on each other besides the simple compositing - the height of a paragraph, for example, depends on the height of each line of text, which depends on the height of each character within the line. Using a separate hash means the composited items have to refer back to the composite to find their immediate relatives. Of course, this has advantages too - and the composite can take responsibility for subtree summaries like that paragraph height.
Steve314
A: 

I would say that the Composite pattern as described in your link violates the Liskov substitution principle, one of the five SOLID principles.

Component has methods that only make sense for a Composite e.g. Add(). Leaf inherits from Component so it will have an Add() method like any other Component. But Leafs don't have children, so the following method call cannot return a meaningful result:

myLeaf.Add( someChild );

That call would have to throw a MethodNotSupportedException, return null or indicate in some other way to the caller that adding a child to a Leaf does not make sense.

Therefore you cannot treat a Leaf like any other Component because you'll get an exception if you try to. The Liskov substition principle states:

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.

Components have the property that you can add children to them. But you cannot add children to a Leaf, even though Leaf is a subtype of Component, which violates the principle.

ctford
A type has to deviate from the semantic interoperability with its parent type to qualify as a Liskov Substitution Principle violation, so if the Leaf type does so then it would be due to an inability to call Add() with a child and subsequently to call GetChild() with the expected index. Merely omitting behavior wouldn't qualify as a violation, however. The Add() method has no implicit or explicit contract requiring that it provide feedback that the child was added, but there is an implicit contract which assumes an added child can be retrieved by the GetChild() method.
Derek Greer
BTW, the reason the Null Object pattern is effective is because it omits behavior while maintaining semantic interoperability.
Derek Greer