Wednesday, June 4, 2014

Structural Patterns

Structural patterns are used to organize and build relationships.  These patterns are invoked in the following situations (this is not exhaustive by any means):
  1. Adapter - I have type A, but I need type B.
  2. Facade - I need to simplify the use of an API or a set of classes.
  3. Flyweight - I have many instances of type A that all share a set of properties where the values are the same.
  4. Proxy - I need to control access to an object due to expensive operations or validation requirements.
Adapters are used in established systems.  Adapters take class(es) that already exist and wrap them to match an interface that is already expected.  An example of an Adapter patter would be creating a plugin.

Facades are used to simplify complex systems.  Facades are often comprised of multiple classes and handle much of the coordination between the systems beneath it.  Be careful not to make the facade a "god object" that is all knowing.  It's really easy to fall into that trap.

Facades and Adapters - They seem similar, right?  They really are.  Both are wrappers.  The difference is in the intent.  Facades simplify while Adapters provide an interface that is expected.

Flyweights are used in situations where performance or memory consumption is a focus and priority.  Flyweights are managed behind a Factory.  The client must use the Factory and must not create Flyweights directly. Flyweights usually will not exist outside of the context of a Factory without loosing its intent.  A great example of a Flyweight implementation is a word processor.  Word processors can use a single instance of a character and multiple "location" instances indicating what location that character exists in the document.  The shared property in this case would be the character's value (for example 'a', 'b', or '1') and the unique values for each instance is the location where that character exists.

Proxies are used to control access to a resource.  An often used example of the use of a proxy pattern is a reference counter.  Other uses could be for license validation or logging.