The Open-Closed Principle
Calsses should be open for extension,but closed for modification.
Our goal is to allow classes to be easily extended to incorperate new behavior without modifying exsiting code.Following the Open-Closed Principle usually introduces new levels of abstraction,which adds complexity to our code.You want to concentrate on those areas that are most likely to change in your designs and apply the principles there.Experience in designing OO systems and a matter of knowing the domain is helpful to you.
- Decorators have the same supertype as the objects they decorate.
- You can use one or more decorators to wrap and object.
- Given that the decorator has the same supertype as the ojbect it decorates,we can pass around a decorated ojbect in place of the original (wrapped) object.
- The decorator adds its own behavior either before and/or after delgating to the object it decorates to do the rest of the job.
- Objects can be decorated at any time ,so we can decorate objects dynamically at runtime with as many decorators as we like.
The Decorator Pattern attaches addtional responsibility to an object dynamically.Decorators provide a fexible alternative to subclassing for extending functionally.