The Factory Method Pattern

Factory method pattern class diagram

The factory method pattern is used when a class needs to instantiate a derivation of another class, but does not know which one. The factory method allows a derived class to make this decision.

Product is the interface for the type of object that the factory creates.Creator is the interface that defines the Factory Method. Clients will need to subclass Creator to make a particular ConcreteProduct. The abstract method FactoryMethod() is implemented in the concrete class and it knows the rules to enable it to create and return the appropriate concrete product.

The Bridge Pattern

Bridge pattern class diagram

The bridge pattern is similar to the strategy pattern, where you define a family of algorithms, encapsulate each one, and make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it. The bridge pattern decouples an abstraction from its implementation so that the two can vary independently.

The Adapter Pattern

Adapter pattern class diagram

This is a common pattern that can be used by many other patterns. The GoF states that the intention of the adapter pattern is to:

Convert the interface of a class into another interface that the clients expect.  Adapter lets classes work together that could not otherwise because of incompatible interfaces.

The Facade Pattern

Facade pattern class diagram

The Façade pattern enables us to use a complex system more easily, either to use just a subset of the system or to use the system in a particular way.

OOP Design Principles

These are some of the more important design principles and strategies to consider during your OOP software design and development activities.

Objects are things with well defined responsibilities

At a conceptual level an object is a set of responsibilities (at a specification level it is a set of methods, at an implementation level an object is code and data). The responsibilities define the behaviour of the object. This focuses on what objects are supposed to do and its public interface, rather than the actual implementation.

Decorator Pattern

Decorator pattern class diagram

The decorator pattern is used to attach additional responsibilities to an object dynamically. The object provides the basic functionality, but you need a variety of additional functionality to be added at run-time. For example you want to add a header and or footer to an invoice print, and the header or footer can also vary. The power of the Decorator pattern is that the instantiation of the chain of objects is completely decoupled from the client objects that use the decorated ConcreteComponent.

Compound Patterns

Compound patterns class diagram

A lot of patterns work together very well to form compound patterns. Shown is a UML class diagram that implements various design patterns to form a compound pattern. Here are some notes describing how and why this design was arrived at.

Composition vs Inheritance

Composition and inheritance class diagram

So how exactly do composition and inheritance compare? Here are several points of comparison:

Commonality Variability Analysis

A principle of good software design is to separate things that change from those that do not. Commonality variability analysis is a way of achieving this.

Benefits

Commonality variability analysis helps us to focus on interfaces and stop us from looking at implementation too soon. It leads us to hide the implementation by focussing on the abstractions first. It encourages us to look at the high and low level aspects of design, but separately. It helps us to design by contract, thus creating an architecture that is more testable.

The Fragile Base Class Problem

Polymorphism using composition

The fragile base class problem is caused by the use of implementation inheritance. It occurs when a derived class extends a base class, using some of the methods or attributes in the base class.