The Decorator Pattern

Decorator – Attach additional reponsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

CName: Wrap

OO Principles:

  • Classes should be open for extension but close for modification.

Bullet Points:

  • Inheritance is one form of extension, but not necessarily the best way to achieve flexibility in our designs.
  • In our designs we should allow behavior to be extended without the need to modify existing code.
  • Compostion and delegation can often be used to add new behaviors at runtime.

The Observer Pattern

Observer – defines a ont-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

OO Principles

  • Strive for loosely coupled designs between objects that interact.

Bullet Points

  • The Observer Pattern defines a ont-to-many relationship between objects.
  • Subjects, or we also known them, Observables, update Observers using a common interface.
  • Observers are loosely coupled in that the observalbe knows nothing about them, other than that they implement the Observer interface.
  • You can push or pull the data of the Observalbe when using the pattern(pull is considered more “correct”).
  • Don’t depend on a specific order of notification for you Observers.

The Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each once, and makes them interchangebale. Strategy lets the algorithm vary independently from clients that use it.

OO Principles:

  • Encapsulate what varies.
  • Favor compostion ove inheritence.
  • Program to interface, not implementation.

Bullet Points:

  • Knowing the OO basics does not make you a great OO designer.
  • Good OO designs are reusable, extensible and maitainable.
  • Patterns are proven object-oriented experience.
  • Patterns don’t give you code, the give you general solution to design problems. You apply them to your specific application.

Road to Excellence



Tag: Road to Excellence