Download presentation
Presentation is loading. Please wait.
Published byGinger Clara Lindsey Modified over 6 years ago
1
The Object-Oriented Thought Process Chapter 15
Design Patterns
2
Design Patterns One of the most important books on object-oriented software development is Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. The book’s authors have become known as the Gang of Four (GoF).
3
Why Design Patterns? The concept of design patterns did not necessarily start with the need for reusable software. In fact, the seminal work on design patterns is about constructing buildings and cities. As Christopher Alexander noted in A Pattern Language: Towns, Buildings, Construction, “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use the solution a million times over, without ever doing it the same way twice.”
4
The Four Elements of a Pattern
The GoF describe a pattern as having four essential elements: The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two. The problem describes when to apply the pattern. The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. The consequences are the results and trade-offs of applying the pattern.
5
Smalltalk’s Model/View/Controller
For historical perspective, we need to consider the Model/View/Controller (MVC) introduced in Smalltalk (and used in other object-oriented languages). MVC is often used to illustrate the origins of design patterns. The Model/View/Controller paradigm was used to create user interfaces in Smalltalk. Smalltalk was perhaps the first popular object-oriented language.
6
Smalltalk Smalltalk is the result of several great ideas that emerged from Xerox PARC. These ideas included the mouse and using a windowing environment, among others. Smalltalk is a wonderful language that provided the foundation for all the object-oriented languages that followed. One of the complaints about C++ is that it’s not really object-oriented, whereas Smalltalk is.
7
MVC The MVC was one of the early pioneers in this separation of interfaces. The MVC explicitly defines the interfaces between specific components pertaining to a very common and basic programming problem: the creation of user interfaces and their connection to the business logic and data behind them.
8
MVC Drawbacks Although the MVC is a great design, it can be somewhat complex in that a lot of attention must be paid to the upfront design. This is a problem with object-oriented design in general—there is a fine line between a good design and a cumbersome design. The question remains: How much complexity should you build into the system with regard to a complete design?
9
Types of Design Patterns
Design Patterns is inherently a design book, and the patterns can be implemented in any number of languages. The authors of the book divided the patterns into three categories: Creational patterns create objects for you, rather than having you instantiate objects directly. Structural patterns help you compose groups of objects into larger structures, such as complex user interfaces or accounting data. Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.
10
Creational Patterns The creational patterns consist of the following categories: Abstract factory Builder Factory method Prototype Singleton
11
The Singleton Design Pattern
The singleton pattern is a creational pattern used to regulate the creation of objects from a class to a single object. For example, if you have a website that has a counter object to keep track of the hits on your site, you certainly do not want a new counter to be instantiated each time your web page is hit.
12
More Than One Reference
There may well be more than one reference to the singleton. If you create references in the application and each reference is referring to the singleton, you will have to manage the multiple references.
13
Two References to a Single Counter
Be aware that in this example, two separate references are pointing to the counter. Thus, when the counter changes, both references will reflect the update.
14
Structural Patterns Structural patterns are used to create larger structures from groups of objects. The following seven design patterns are members of the structural category: Adapter Bridge Composite Decorator Façade Flyweight Proxy
15
The Adapter Design Pattern
The adapter pattern is a way for you to create a different interface for a class that already exists. The adapter pattern basically provides a class wrapper. In other words, you create a new class that incorporates (wraps) the functionality of an existing class with a new and—ideally—better interface.
16
Behavioral Patterns The behavioral patterns consist of the following categories: Chain of response Command Interpreter Iterator Mediator Memento Observer State
17
The Iterator Design Pattern
Iterators provide a standard mechanism for traversing a collection, such as a vector. Functionality must be provided so that each item of the collection can be accessed one at a time. The iterator pattern provides information hiding, keeping the internal structure of the collection secure.
18
Antipatterns The term antipattern derives from the fact that design patterns are created to proactively solve a specific type of problem. An antipattern, on the other hand, is a reaction to a problem and is gleaned from bad experiences. In short, whereas design patterns are based on solid design practices, antipatterns can be thought of as practices to avoid.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.