Reputation: 3168
What does this statement mean in terms of Object Oriented Programming
Decorate objects so you can easily add/remove features.
Upvotes: 2
Views: 269
Reputation: 1335
A good oops design is based on open close principle, Open means that a class must be open for extension and close means that the class is closed for modification.
Decorator pattern uses this principle, to extend functionality of a class you need to create a wrapper over it.
IMO the best way to learn pattern is to search it in standard implementation, you will find that java io
package is built on same pattern.
In java.IO package we have
FileInputStream, StringBufferInputStream, ByteArrayInputStream
classes that extends the base class InputStream. These classes are capable of being decorated by decorator classes.
If you ever used Java.IO package you must have used BufferedInputStream which is a decorated InputStream
.
Upvotes: 2
Reputation: 425328
The decorator pattern is basically where a subclass overrides a method, calls the overridden method in the superclass (thus not losing functionality), then executes its own extra code - to decorate (ie embellish) the functionality.
By using this technique, and always referring to the object using the abstract type (see Liskov Substitution Principle), enhancements can be swapped in and out easily by providing the client with the concrete class that provides the extra functionality desired.
The decision of which class to use can be made at runtime or compile time (see Abstract Factory pattern)
Upvotes: 2