![](https://csdnimg.cn/release/download_crawler_static/10609233/bg12.jpg)
CHAPTER 1: Hello, Design Patterns!
5
analogies to assimilate the theme of the design pattern. Also, there are tips and notes to
dispel any confusion where applicable.
Are you ready? Let’s roll!
Design Déjà-vu
As a developer, you have likely had the feeling that “I’ve solved this problem before but
don't know exactly where or how.” This happens all the time, especially when you’re
working on particular types of projects repeatedly. For example, every database
application has features like database access so that your application can store and
retrieve its data. If you do a good job recording the details of the problems and how you
solved them, you can easily reapply the strategies instead of reinventing them.
The experiences of some of the most common design déjà-vu scenarios as well as
strategies applicable to them were first authoritatively described and cataloged in
Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma,
Richard Helm, Ralph Johnson, and John Vlissides (Addison-Wesley Professional, 1994).
It captured design experiences in designing object-oriented software in a form that
people could use effectively as design patterns. Ironically, the predecessor of Cocoa
(Touch) frameworks, NEXTSTEP, was designed with many elegant, reusable object-
oriented software design patterns and it played a major role as a source of inspiration for
the original “Gang of Four” (a popular nickname for the authors of the above book).
According to Design Patterns: Elements of Reusable Object-Oriented Software,
A design pattern is a description of communicating objects and classes that are
customized to solve a general design problem in a particular context.
In short, a design pattern is a customized solution to a problem in a context. The context
is a recurring situation in which the problem occurs. The problem is the goal you are
trying to achieve in this context. The same problem in a different context may have
different constraints and challenges. And the customized solution is a design that
achieves the goal in a particular context and resolves the constraints that come with it.
A design pattern abstracts the key aspects of a particular object-oriented design
problem or issue that has proven to be effective over time. It expresses important
insights on object-oriented designs. Some principles of design influence design
patterns. These principles are rules of thumb for constructing reusable and maintainable
object-oriented applications, such as “favor object composition over class inheritance”
and “program to an interface, not an implementation.”
For example, if you encapsulate and isolate some parts of a program that vary by
defining interfaces for them, they can vary independently of other parts of the program
because they are not tied to any specifics. You can then change or extend those
variable parts without affecting the other parts of the program. The program
consequently becomes more flexible and robust to change because you eliminate
dependencies and reduce couplings between parts. These benefits make design
patterns important when you want to write reusable software.