Tuesday, July 17, 2007

Design Patterns: Structural Patterns

Structural patterns describe how classes and objects can be combined to form larger structures.

Class Patterns

  • They describe how inheritance can be used to provide more useful program interfaces.
  • Examples: Adapter Pattern

Object Patterns

  • They describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects.
  • Examples: Adapter Pattern

----------------------------------------------------------------------------------

Adapter Pattern

  • It is used to convert the programming interface of one class into that of another.
  • We use adapters whenever we want unrelated classes to work together in a single program. The concept of an adapter is thus pretty simple; we write a class that has the desired interface and then make it communicate with the class that has a different interface.
  • There are two ways to do this: by inheritance, and by object composition. In the first case, we derive a new class from the nonconforming one and add the methods we need to make the new derived class match the desired interface. The other way is to include the original class inside the new one and create the methods to translate calls within the new class.

Bridge Pattern

  • This pattern is meant to "decouple an abstraction from its implementation so that the two can vary independently".
  • There are many types of shapes (Abstraction) each with its own properties but the one thing all shapes can do is draw themselves. However, drawing (Implementation) graphics to a screen is dependent on different graphics implementations or operating systems.
    Shapes have to be able to be drawn on many types of systems, but having the shape itself implement them all or modifying the shape class to work with different architectures is not practical.
    The bridge helps by allowing the creation of new classes that provide the drawing implementation. The abstract - shape - class provides methods for getting the size or properties of a shape while the implementation - drawing - class provides an interface for drawing.

Composite Pattern

  • This pattern allows a group of objects to be treated in the same way as a single instance of an object.
  • When dealing with tree-structured data, we have to discriminate between a leaf-node and a branch. The solution is an interface that allows treating complex and primitive objects uniformly. This is known as a "has-a" relationship between objects.

Decorator Pattern

  • This pattern allows new/additional behaviour to be added to an existing method of an object dynamically.
  • This pattern works by wrapping the new "decorator" object around the original object, which is typically achieved by passing the original object as a parameter to the constructor of the decorator, with the decorator implementing the new functionality.
  • Decorators are alternatives to subclassing. Subclassing adds behaviour at compile time whereas decorators provide a new behaviour at runtime.
  • An example of the decorator pattern is the Java I/O Streams implementation.

Facade Pattern

A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:

  • make a software library easier to use and understand, since the facade has convenient methods for common tasks;
  • make code that uses the library more readable, for the same reason;
  • reduce dependencies of outside code on the inner workings of a library, since most codeuses the facade, thus allowing more flexibility in developing the system;
  • wrap a poorly designed collection of APIs with a single well-designed API.

Flyweight Pattern

  • This pattern is used when there is a need to generate a very large number of small class instances to represent data.
  • Sometimes you can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters. If you can move those variables outside the class instance and pass them in as part of a method call, the number of separate instances can be greatly reduced.

Proxy Pattern

  • The Proxy pattern is used when you need to represent a complex object by a simpler one.
  • If creating an object is expensive in time or computer resources, Proxy allows you to postpone this creation until you need the actual object. A Proxy usually has the same methods as the object it represents, and once the object is loaded, it passes on the method calls from the Proxy to the actual object.

No comments: