Next: How to Use A Design Pattern | Previous: Frameworks

| ## How to Select a Design Pattern With more than twenty design patterns in the catalog to choose from, it might be hard to find the one that addresses a particular design problem, especially if the catalog is new and unfamiliar to you. Here are several different approaches to finding the design pattern that's right for your problem: Consider how design patterns solve design problems. Section 1.6 discusses how design patterns help you find appropriate objects, determine object granularity, specify object interfaces, and several other ways in which design patterns solve design problems. Referring to these discussions can help guide your search for the right pattern. Scan Intent sections. Section 1.4 lists the Intent sections from all the patterns in the catalog. Read through each pattern's intent to find one or more that sound relevant to your problem. You can use the classification scheme presented in Table 1.1 to narrow your search. Study how patterns interrelate. Figure 1.1 shows relationships between design patterns graphically. Studying these relationships can help direct you to right pattern or group of patterns. Study patterns of like purpose. The catalog has three chapters, one for creational patterns, another for structural patterns, and a third for behavioral patterns. Each chapter starts off with introductory comments on the patterns and concludes with a section that compares and contrasts them. These sections give you insight into the similarities and differences between patterns of like purpose. Examine a cause of redesign. Look at the causes of redesign starting to see if your problem involves one or more of them. Then look at the patterns that help you avoid the causes of redesign. Consider what should be variable in your design. This approach is the opposite of focusing on the causes of redesign. Instead of considering what might force a change to a design, consider what you want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns. Table 1.2 (below) lists the design aspect(s) that design patterns let you vary independently, thereby letting you change them without redesign.
Table 1.2: Design aspects that design patterns let you vary
Purpose | Design Pattern | Aspect(s) That Can Vary
Creational | Abstract Factory Builder Factory Method Prototype Singleton | families of product objects how a composite object gets created subclass of object that is instantiated class of object that is instantiated the sole instance of a class
Structural | Adapter Bridge Composite Decorator Facade Flyweight Proxy | interface to an object implementation of an object structure and composition of an object responsibilities of an object without subclassing interface to a subsystem storage costs of objects how an object is accesed; its location
Behavioral | Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor | object that can fulfill a request when and how a request is fulfilled grammar and interpretation of a language how an aggregate's elements are accessed, traversed how and which objects interact with each other what private information is stored outside an object, and when number of objects that depend on another object; how the dependent objects stay up to date states of an object an algorithm steps of an algorithm operations that can be applied to object(s) without changing their class(es)
Next: How to Use A Design Pattern | Previous: Frameworks