Cheat Sheet

This summarizes the different patterns and their uses. It also includes the example usages of the patterns in the Java API.

We'll cover the following...

Notes

PatternPurpose

Prototype Pattern

Prototype pattern involves creating new objects by copying existing objects. The object whose copies are made is called the prototype. In Java the clone() method of java.lang.Object is an example of this pattern.

Factory Method Pattern

The factory method is defined as providing an interface for object creation but delegating the actual instantiation of objects to subclasses. For instance the method getInstance() of the class java.util.Calendar is an example of a factory method pattern.

Abstract Factory

The abstract factory pattern defines an interface to create families of related or dependent objects without specifying their concrete classes. The abstract factory is particularly useful for frameworks and toolkits that work on different operating systems. For instance, if your library provides fancy widgets for the UI, then you may need a family of products that work on MacOS and a similar family of products that work on Windows.

Adapter Pattern

The Adapter pattern allows two incompatible classes to interoperate that otherwise can't work with eachother. Consider the method asList() offered by java.util.Arrays as an exampe of the adapter pattern. It takes an array and returns a list. ...