Design Patterns

Book description


Design Patterns demonstrates how software developers can improve the performance, maintainability, portability, and scalability of their code through the use of the Gang of Four design patterns. After a discussion of patterns methodology, reasons for using design patterns, the book delves into each of the 23 patterns.

Each pattern section gives a detailed description of the pattern, refactored from either Boolean logic or simpler, less-maintainable code that you might encounter in the real world, and shows readers how to use the pattern in their code. The text walks readers through making the move from current code to the pattern, lists the benefits of using the pattern, and shows how the pattern performs after the refactoring effort, with a goal throughout of providing practical implementations.

Table of contents

  1. Contents
  2. Introduction
    1. Audience and Goal
    2. Assumptions This Book Makes
    3. Conventions Used in This Book
    4. Using Code Examples
  3. Acknowledgments
  4. 1 Why Pattern?
    1. Common Aspects of Object-Oriented Languages
    2. Patterns Cross-Reference Each Other
    3. Refactoring Legacy Code and Improving New Code
    4. Reflection and OOP
  5. 2 Creational Patterns
    1. Factory Pattern
      1. What Is a Factory Pattern?
      2. Problem 1: Implementation classes with a common base are created in multiple places and no uniformity exists between creational logic
      3. Solution 1: Use a factory to encapsulate the class creation code in one place
      4. Problem 2: Class types to be created are determined by immutable logical code and extending the returnable types requires scale modifications to the factory
      5. Solution 2a: Replace conditional logic with a class activator, using reflection to determine the needed types for different workflows
      6. Solution 2b: Use abstractions of the factory to determine allowable class types using inheritance (instead of reflection)
      7. Comparison to Similar Patterns
      8. What We Have Learned
      9. Related Patterns
    2. Abstract Factory Pattern
      1. What Is an Abstract Factory Pattern?
      2. Problem 1: Different class types need a common interface to a factory method for creation of internal attributes
      3. Solution 1: Allow an abstract factory to act as a common base interface for the factories between classes
      4. Problem 2: We need to make a builder more extendible to allow different functional processes to occur without changing the builder
      5. Solution 2: Use an abstract factory to define common creational methods for the builder
      6. Comparison to Similar Patterns
      7. What We Have Learned
      8. Related Patterns
    3. Builder Pattern
      1. What Is a Builder Pattern?
      2. Problem 1: A class needs several operations to create and initialize each of its attributes in an expected order of operations, and each class type instance needs different attribute values
      3. Solution 1: Use a combination of a director and a builder to encapsulate and uniformly define the processes in the expected order, and use inherited members of the builder to change each type’s reaction to the expected operations
      4. Problem 2: A factory produces classes that need initialization, configuration, data, or state set during creation and this can differ between class types and spans multiple operations
      5. Solution 2: Use registered builders for each class type inside your factory to aid in the initialization of class types
      6. Comparison to Similar Patterns
      7. What We Have Learned
      8. Related Patterns
    4. Prototype Pattern
      1. What Is a Prototype Pattern?
      2. Problem: A class that cannot be constructed outside its assembly or package needs a copy made that keeps its current state intact
      3. Solution: Create a prototype method on the class to return a cloned instance with its state copied to the needed depth
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    5. Singleton Pattern
      1. What Is a Singleton Pattern?
      2. Problem: A global class in the application needs to hide its constructor and maintain only one instance of itself while allowing all of its methods to be accessed as instance methods
      3. Solution: Use the Singleton pattern to hide the constructor and maintain an instance of the class inside itself
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
  6. 3 Behavioral Patterns
    1. Chain of Responsibility Pattern
      1. What Is a Chain of Responsibility Pattern?
      2. Problem: A group of classes each have processes to run in turn, but there is no way to directly determine in which class order each should run its process
      3. Solution: Use a chained association of classes that have interdependence on each other to define the order of operations
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    2. Command Pattern
      1. What Is a Command Pattern?
      2. Problem: A document object needs a way to add and store undo and redo actions
      3. Solution: Use a command as the request to store the text and allow the command to handle undo and redo requests of the document
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    3. Interpreter Pattern
      1. What Is an Interpreter Pattern?
      2. Problem 1: The format of a date needs to be different depending on the particular type of date expression needed
      3. Solution 1: Use an interpreter and expression engine to manage the different date formats by expression type
      4. Problem 2: We need to create classes from metadata from remote packages or assemblies and load them using a common interpreter
      5. Solution 2: Allow an interpreter to create the classes from metadata using reflection to recurse into the remote assemblies or packages
      6. Comparison to Similar Patterns
      7. What We Have Learned
      8. Related Patterns
    4. Iterator Pattern
      1. What Is an Iterator Pattern?
      2. Problem: A list object needs a way to move through each element in the list in consecutive order, retaining the position of the current record
      3. Solution: Create a list iterator that keeps the current record position and has methods to read and move to the next record or to any record in the list
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    5. Mediator Pattern
      1. What Is a Mediator Pattern?
      2. Problem: Message windows reference each other directly, and if new windows are added the code cannot easily manage each window’s reference to the other
      3. Solution: Use a mediator to control messages between each window
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    6. Memento Pattern
      1. What Is a Memento Pattern?
      2. Problem: A class needs to have its internal state captured and then restored without violating the class’s encapsulation
      3. Solution: Use a memento and a caretaker to capture and store the object’s state
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    7. Observer Pattern
      1. What Is an Observer Pattern?
      2. Problem: We have several child forms of an MDI form that we need to inform of changes that occur in the MDI form, and we have no way to do this automatically
      3. Solution: Use the Observer pattern to provide notifications to the child forms when the MDI form state is changed
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    8. State Pattern
      1. What Is a State Pattern?
      2. Problem 1: We have no way to determine the state of an object without performing calculations outside an object
      3. Solution 1: Use a state object that can change itself as the context object changes to determine the context state
      4. Problem 2: We need to prevent unsynchronized or dirty updates between factory class instantiation calls
      5. Solution 2: Make the factory a static interface, and maintain class states across multiple threads
      6. Comparison to Similar Patterns
      7. What We Have Learned
      8. Related Patterns
    9. Strategy Pattern
      1. What Is a Strategy Pattern?
      2. Problem: We have a series of algorithmic classes that have no correlation and are not easily exchanged but are needed to do so
      3. Solution: Make each class inherit from a common base to provide ease of scalability and exchange between class types so one algorithm class can easily be exchanged for another
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    10. Template Pattern
      1. What Is a Template Pattern?
      2. Problem: Shared functionality between classes is desired without copying functionality
      3. Solution: Make both classes inherit from a single base that contains shared functionality
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    11. Visitor Pattern
      1. What Is a Visitor Pattern?
      2. Problem: We have a need for a class that encapsulates database transactions and performs specific functions based on the class instance type
      3. Solution: Place each database transaction type in a class that controls that functionality, and use this class to send the data into the database
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
  7. 4 Structural Patterns
    1. Adapter Pattern
      1. What Is an Adapter Pattern?
      2. Problem: We wish to allow one class to use another’s methods without using direct inheritance
      3. Solution: Allow an adapter class to hold an instance of the desired class and adapt its methods, properties, and events through the adapter’s methods, properties, and events
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    2. Bridge Pattern
      1. What Is a Bridge Pattern?
      2. Problem: We need to allow a class to use functionality from a series of class types without using direct inheritance
      3. Solution: Create a series of classes and allow a bridge class to hold the desired instance of the series and adapt its methods, properties, and events through the bridge’s methods, properties, and events
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    3. Composite Pattern
      1. What Is a Composite Pattern?
      2. Problem: A way is needed to allow a group of classes with a common method and different functionality to work together in sequence without compiling the sequence beforehand
      3. Solution: Group each class into a composite, which will manage when each class will have its common method called and will allow manipulation of the sequence of calls
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    4. Decorator Pattern
      1. What Is a Decorator Pattern?
      2. Problem: We wish to have certain functions associated with a class type only when desired, but not all the time
      3. Solution: We allow a decorator that inherits from the class we are using to take its place and act as the class, but with the increased functionality of the decorator
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    5. Facade Pattern
      1. What Is a Facade Pattern?
      2. Problem: We have several classes whose methods make up a process, but we need to control the order in which these classes are called by code outside the assembly
      3. Solution: Create a facade layer with interfaces to desired classes that are called in the proper order and allow the subsystem classes to only be called inside the facade assembly
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    6. Flyweight Pattern
      1. What Is a Flyweight Pattern?
      2. Problem: A text document uses individual object instances for each character and quickly becomes unwieldy
      3. Solution: Use shared flyweight objects to instantiate a limited number of character objects that use their current context to change the extrinsic state
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
    7. Proxy Pattern
      1. What Is a Proxy Pattern?
      2. Problem: You have an object in a different domain or server address you would like to access in the current domain, but do not want to allow construction of the object in the local domain
      3. Solution: Use a proxy to bridge the two separate domains and allow access between programs
      4. Comparison to Similar Patterns
      5. What We Have Learned
      6. Related Patterns
  8. Glossary of Terms
  9. Index

Product information

  • Title: Design Patterns
  • Author(s): Christopher G. Lasater
  • Release date: October 2006
  • Publisher(s): Jones & Bartlett Learning
  • ISBN: 9781449612887