Design Patterns – In-Depth Definition of ‘Property Container’ Design Pattern

architectural-patternsArchitectureclass-designdesign-patternspatterns-and-practices

Could anybody, please, explain what is a Property Container or at least where can I get information about that pattern on the Internet?

I've found that it exists as one of the Fundamental patterns in this article.
Unfortunately, it is not described in the corresponding English article.
The only thing is a short description in Wikipedia that I've tried to approximately translate below:

This pattern allows to add additional properties for the class in
container (inside the class), instead of extending the class with the
new properties.

If a short example is possible in PHP / C# / C++ or Java, it would be quite useful for understanding.

P.S.: It seems like it has been described in "SanFrancisco(TM) Design Patterns: Blueprints for Business Software", but I don't have access to it. Google does not help or I'm searching with the wrong keywords…

Best Answer

A Property Container is a class whose properties can be customized at runtime without having to edit your class definition.

This article has the only thorough example I could find. In it, they use the example of a customizable Movie class.

The PropertyContainer base class provides methods to add/remove/retrieve properties stored in the class. Basically, it acts a lot like a HashMap:

PropertyContainer:
   + addPropertyBy()
   + removeProperty()
   + getPropertyBy()
   + getPropertyKeys()

Then you have a Movie class that inherits from the PropertyContainer:

Movie extends PropertyContainer:
   - rating
   - available
   - description
   - title
   - id
   - price

   + [getters and setters for private properties]

Later, if you need to add releasedate as a property to the Movie class in one project, you have the ability to add it at runtime instead of updating your class.

Movie m = new Movie()
m.addPropertyBy("3/5/14", "releasedate")
Related Topic