CMPSC 56 Chapter Notes - Chapter 3: Decorator Pattern, Subtyping, Observer Pattern
Document Summary
Design principle: classes should be open for extension, but closed for modification i. e. the observer pattern in chapter 2. But applying the open-closed principle everywhere is wasteful and unnecessary, and can lead to complex, hard-to-understand code. Decorator pattern = attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorators have the same supertype as the objects they decorate. You can use one or more decorators to wrap an object. Given that the decorator has the same supertype as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. You can decorate objects dynamically at runtime with as many decorators as you like. Decorators use inheritance to achieve type matching, but inheritance isn"t used to get behavior.