tags:

views:

55

answers:

3
A: 

I see a lot of inheritance here. Decorator pattern dictates lesser inheritance and more composition. So you create decorators and embellish your objects at runtime. See Decorator Patter.

SidCool
Or, you can use simple compositions between objects that define the real world scenario. Like a House HAS stories, a storey HAS rooms, a room HAS items. Inheritance seems useless here. A storey does not inherit any behavior from a house....
SidCool
indeed, prefere composition over inheritance :)
Antoine Claval
+1  A: 

In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically.

This how wikipedia starts it's article on the decorator pattern.

So the decorator pattern is about adding behaviour to an existing object. And items in a room are not an additional behaviour for that room.

I think, the Composite pattern would be a better choice to build a model for that domain.

(But if you still want a Decorator for your house you may find a better solution here ;-) )

Andreas_D
+1  A: 

I dont think decorator is the right pattern here. What I'm seeing the design is multiple instances of "is-a" and "has-a" relationships.

  1. An "is-a" relation is a specialization. Examples : "Bungalow" "is-a" "House", "ConcreteRoom1" "is-a" "Room".
    • "is-a" relationships are implemented using inheritance. In some cases, favoring composition over inheritance, "is-a" maybe implemented using decorators.
    • Ideally, the "Room" or "House" constructs must be interfaces (or abstract classes), which define object contracts and base properties
    • The concrete implementations of these interfaces are the actual objects that you will work with.
  2. A "has-a" relationship is a composition. Examples : "House" has "Floors", "Room" has "Items"
    • Generally, a has-a relationship maybe implemented by storing attributes for each object that your class "has".
    • it is always a good idea to store the attributes as a collection of the interfaces - e.g. Room should have List<Item> items

Hope that clarifies the usage expected in your design.

madhurtanwani