0:14
There are many design patterns you can find in books and via the Internet.
One of the most famous books on the topic is
Design Patterns: Elements of Reusable Object-Oriented Software.
This book was written by four authors who were given the nickname, The Gang of Four.
The Gang of Four's book pulled from their experience as developers.
Each author had written their own programs and graphical applications,
and while doing so found,
patterns emerging in their design solutions.
As a group, they decided to formalize the patterns into a useful reference.
The patterns are organized to be readable,
and they are often named to suggest their purpose.
With these patterns, they formed a catalog.
In this lesson, I will go over The Gang of Four's design pattern catalog.
This catalog is not like a recipe book.
You can't pull universal patterns out of it to using your code like puzzle pieces.
Instead, It's closer to a list of tropes.
A trope is a storytelling device or convention often found in fiction.
Films and TV shows use a pattern of storytelling.
For example, you might see the Hero's Journey recur in fiction.
The Hero's Journey is a pattern of storytelling that follows a very distinct order.
The hero starts from humble beginnings in ordinary world,
finds a special world, and must face trials in order to return home.
This trope can be found in hundreds of pieces of fiction.
Just like tropes recurring in film,
design patterns emerge in software.
Occurrences of the same pattern can be found in thousands of programs.
A very simple example of a pattern would be Defining and Calling methods.
The Gang of Four's catalog contains a number of
patterns and solutions with a variety of different purposes.
A collection of patterns that are related to
a certain problem space is called a Pattern Language.
Depending on the context of the problem,
you would need to use different pattern languages.
When designing games, you might use
a different pattern language than if you were creating accounting software.
For games, you have designed words such as encounters, quests, and players.
These words are important in design patterns for game software,
but would not be found in design patterns for accounting software.
In accounting software,
double entry bookkeeping is a vital design solution to reduce errors,
and ensure consistency in the transactions.
Double entry bookkeeping would not need to be
defined in the pattern language for game design.
Think of it like human languages.
If you were trying to speak with someone who only spoke Mandarin,
you would choose the appropriate language to converse with them.
Just like you would choose the language of speaking based on your audience,
you would also choose your pattern language,
and design patterns based on the problem space.
Now, it's your turn to design a pattern language.
There are 23 patterns found in the Gang of Four's catalog.
I will go over the categories of patterns that the Gang of Four defined.
One of the main categories of patterns involves Creational Patterns.
Creational Patterns tackle how you handle creating new objects.
There are several different patterns based upon creating and cloning objects.
For example, if you are creating an object that is similar to an existing one,
instead of instantiating a new object,
you might clone existing objects.
You might make the choice to clone,
rather than instantiate based upon the language you are implementing in.
Languages without the notion of classes would
encourage you to clone and add to existing objects.
Language like Javascript does not contain traditional classes to be instantiated.
Objects are instead cloned and expanded to meet
the purposes of those particular instances, called prototypes.
Javascript allows for changes to these prototypes at run time.
Languages like Java and C-Sharp on the other hand,
rely heavily on instantiating objects using specific classes defined at compile time.
The different ways of creating objects can strongly impact how you solve a problem.
An Implementation Language, is how we design pattern language is realized,
which can heavily influence what patterns are possible to use.
Another pattern category used by the Gang of Four involved Structural Patterns.
Structural patterns, describe how objects are connected to each other.
Previously, we examined the major design principles
like Decomposition and Generalization,
and how they are expressed in UML class diagrams with Association,
Aggregation, Composition, Inheritance, and Interface relationships.
There are many different ways you can structure objects
depending on the relationship you'd like between them.
Not only do structural patterns describe how different objects have relationships,
but also how subclasses and classes interact through inheritance.
Structural Patterns use these relationships,
and describe how they should work to achieve a particular design goal.
Think of a structural pattern like a food dish with a combination of flavor pairings.
A flavor pairing determines which ingredients may be
combined together to form a suitable relationship.
Some ingredients are mixed together,
to the point where they are nearly indistinguishable,
such as garlic and chickpeas and hummus.
Others are combined but still maintain some independence,
such as a salad of mixed vegetables.
Some food just pairs well with others and is not physically combined,
such as wine and cheese.
The relationships among the food are defined by the pairings.
Similarly, in software, each structural pattern
determines this various suitable relationships among the objects.
The final pattern category involves Behavioral Patterns.
These patterns focus on how objects distribute work.
They describe how each object does a single cohesive function.
Behavioral patterns also focus on how independent objects work towards a common goal.
Think of a behavioral pattern like a race car pit crew at a track.
In the pit crew, the roles of the members
describe how the team is able to achieve victory,
which is their common goal.
Each member has a specific responsibility, their role in the race.
Some members change the tires, others unmount,
and mount the wheel nuts,
others refuel the car,
but all must work together to win.
Like a game plan, a Behavioral Pattern lays out
the overall goal and the purpose for each of the objects.
All of these categories may not be so clear cut.
Some patterns have elements that could make them span categories.
The Gang of Four just uses these categories in order
to organize and characterize the patterns in their book.
Patterns will help you as a designer to develop
an intuition for and appreciate object-oriented design.
By looking for specific design patterns,
you can better recognize object-oriented design elsewhere.
Patterns sometimes come with their share of consequences.
You must decide which ones will suit your problem or design issue the best.
There are trade offs that must be made when designing your systems.
Some patterns may be more resource-intensive,
like more memory, or processing.
By learning different design patterns,
you will be able to design software for different purposes and in different ways.