![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged. In Java, of course, the simplest way to create an instance of an object is by using the new operator.
Fred = new Fred(); //instance of Fred class
However, this really amounts to hard coding how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program from time to time and abstracting the creation process into a special "creator" class can make your program more flexible and general. (Each chapter is in PDF format)
The Factory Method provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
The Abstract Factory Method provides an interface to create and return one of several families of related objects.
The Builder Pattern separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.
The Prototype Pattern starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.
The Singleton Pattern provides a class of which there can be no more than instance, and provides a single global point of access to that instance.
Summary of Creational Patterns
Summary of Creational Patterns
- The Factory Pattern is used to choose and return an instance of a class from a number of similar classes based on data you provide to the factory.
- The Abstract Factory Pattern is used to return one of several groups of classes. In some cases it actually returns a Factory for that group of classes.
- The Builder Pattern assembles a number of objects to make a new object, based on the data with which it is presented. Frequently, the choice of which way the objects are assembled is achieved using a Factory.
- The Prototype Pattern copies or clones an existing class rather than creating a new instance when creating new instances is more expensive.
- The Singleton Pattern is a pattern that insures there is one and only one instance of an object, and that it is possible to obtain global access to that one instance.
Behavioral patterns are those patterns that are most specifically concerned with communication between objects. In this chapter, we’ll see that: (Each chapter is in PDF format)
- The Observer pattern defines the way a number of classes can be notified of a change,
- The Mediator defines how communication between classes can be simplified by using another class to keep all classes from having to know about each other.
- The Chain of Responsibility allows an even further decoupling between classes, by passing a request between classes until it is recognized.
- The Template pattern provides an abstract definition of an algorithm, and
- The Interpreter provides a definition of how to include language elements in a program.
- The Strategy pattern encapsulates an algorithm inside a class,
- The Visitor pattern adds function to a class,
- The State pattern provides a memory for a class’s instance variables.
- The Command pattern provides a simple way to separate execution of a command from the interface environment that produced it, and
- The Iterator pattern formalizes the way we move through a list of data within a class.
Structural patterns describe how classes and objects can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe how inheritance can be used to provide more useful program interfaces. Object patterns, on the other hand, describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects. (Each chapter is in PDF format)
For example, we’ll see that the Adapter pattern can be used to make one class interface match another to make programming easier. We’ll also look at a number of other structural patterns where we combine objects to provide new functionality. The Composite, for instance, is exactly that: a composition of objects, each of which may be either simple or itself a composite object. The Proxy pattern is frequently a simple object that takes the place of a more complex object that may be invoked later, for example when the program runs in a network environment.
The Flyweight pattern is a pattern for sharing objects, where each instance does not contain its own state, but stores it externally. This allows efficient sharing of objects to save space, when there are many instances, but only a few different types.
The Façade pattern is used to make a single class represent an entire subsystem, and the Bridge pattern separates an object’s interface from its implementation, so you can vary them separately. Finally, we’ll look at the Decorator pattern, which can be used to add responsibilities to objects dynamically.
You’ll see that there is some overlap among these patterns and even some overlap with the behavioral patterns in the next chapter. We’ll summarize these similarities after we describe the patterns.

