Professional Documents
Culture Documents
04 Handout 1 (Midterms)
04 Handout 1 (Midterms)
04 Handout 1 (Midterms)
Design Patterns
Designing with Patterns
In software engineering, design patterns are the reusable solutions to commonly occurring problems in software design. These
patterns started as best practices used in the industry that were applied multiple times to similar problems encountered in
different contexts. Design patterns are used as templates or blueprints that any developer can customize to solve a recurring
design problem on codes. Design patterns are not a specific piece of code, but a general concept for solving a particular problem.
These are also used to design the architecture of an entire software system.
Design patterns are developed in the software industry. When a solution is repeatedly used in various projects, someone
eventually puts a name to it and describes the solution in detail. That’s basically how a pattern gets discovered.
Design patterns are said to have four (4) essential features:
• Pattern Name – This is the name of a pattern that can be used to describe a design problem, its solution, and consequences.
• Problem – This describes when to use the pattern. It explains the problem and its context.
• Solution – This describes the elements that make up the design, their relationships, responsibilities, and collaborations.
The pattern provides an abstract description of a design problem and how a general arrangement of element solves it.
• Consequences – These are the results and interchanges of applying the pattern to the problem. They include time and
space tradeoffs, but also flexibility, extensibility, and portability, among others.
Categories of Patterns
Design patterns differ by their complexity, level of detail, and scale of applicability to the entire software system being designed.
Developers can implement design patterns in any language.
There are three (3) categories of design patterns in object-oriented design:
• Creational Patterns – These patterns deal with when and how objects are created. These provide object creation
mechanisms that increase flexibility and reuse of existing codes.
• Structural Patterns – These describe how objects are composed into larger groups and explain how to assemble objects
and classes into larger structures while keeping their structures flexible and efficient.
• Behavioral Patterns – These describe how responsibilities are distributed between objects in the design and how
communication happens between objects.
Creational Patterns
Creational design patterns are all about class instantiation. These are patterns that deal with object creation mechanisms, trying
to create objects in a manner suitable to a certain situation. The basic form of object creation could result in design problems
or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
There are five (5) creational patterns:
• Singleton Pattern – This design pattern lets developers ensure that a class has only one (1) instance while providing a global
access point to the instance.
Problem:
The Singleton pattern solves two (2) problems at the same time, therefore violating the Single Responsibility Principle:
1. Ensure that a class has just a single instance. This is to control access to some shared resource—for example, a database
or a file.
2. Provide a global access point to that instance. While those global variables (which developers used to store some
essential objects) are very handy, they’re also very unsafe since any code can potentially overwrite the contents of
those variables and crash the app. Just like a global variable, the Singleton pattern lets developers access some objects
from anywhere in the program. However, it also protects that instance from being overwritten by other code.
Solution:
The Singleton pattern implementation has two (2) steps:
1. Make the default constructor private to prevent other objects from using the new operator with a Singleton class.
2. Create a static creation method that acts as a constructor. This method will call the private constructor to create an
• Factory Method Pattern – This provides an interface for creating objects in a superclass but allows subclasses to alter the
type of object that will be created.
Problem:
A developer is creating a logistics management application. The first version of his app can only handle transportation by
trucks, so the bulk of his code lives inside the Truck class. After a while, his app becomes pretty popular. Each day he
receives dozens of requests from sea transportation companies to incorporate sea logistics into the app.
At present, most of his code is coupled to the Truck class. Adding Ships into the app would require making changes to the
entire codebase. Furthermore, if he decides to add another type of transportation to the app later, he will probably need
to make all of these changes again.
As a result, he will end up with pretty nasty code, riddled with conditionals that switch the app’s behavior depending on
the class of transportation objects.
Solution:
The Factory Method pattern suggests that he replace direct object construction using the new operator with calls to a
special factory method. The objects are still created via the new operator, but it’s being called from within the factory
method. Objects returned by a factory method are often referred to as “products”.
Structure:
o In Figure 2, Product declares the interface, which is
common to all objects that can be produced by the
creator and its subclasses.
o The ConcreteProduct classes are different
implementations of the product interface.
o The Creator class declares the factory method that
returns new product objects. The return type of this
method must match the product interface.
Developers can declare the factory method as an
abstract to force all subclasses to implement their
own versions of the method. As an alternative, the
base factory method can return some default
product type.
o The ConcreteCreator classes override the base Figure 2. Factory method structure
factory method, so it returns a different type of Source: https://refactoring.guru/design-patterns/factory-method
product.
• Prototype Pattern – This creational design pattern lets a developer copy existing objects without making his code
dependent on their classes.
• Facade Pattern – This provides a simplified interface to a library, a framework, or any other complex set of classes.
Problem:
A developer is creating a code that works with a broad set of objects that belong to a sophisticated library or framework.
He will simply need to initialize all of those objects, keep track of dependencies, execute methods in the correct order, and
so on. As a result, the business logic of his classes would become tightly coupled to the implementation details of third-
party classes, making it hard to comprehend and maintain.
Solution:
A facade is a class that provides a simple interface to a complex subsystem which contains lots of moving parts. A facade
might provide limited functionality in comparison to working with the subsystem directly. However, it includes only those
features that clients really care about. Using a facade is convenient when the program needs to integrate with a library
that has dozens of features, but the program only needs its few functionalities.
Structure:
o In Figure 11, the Facade provides convenient access to a particular part of the subsystem’s functionality. It knows where
to direct the client’s request and how to operate all the moving parts.
o An Additional Facade class can be created to prevent polluting a single facade with unrelated features that might
make it yet another complex structure. Both clients and other facades can use additional facades.
o The Complex Subsystem consists of dozens of various objects. To make them all do something meaningful, developers
• Flyweight Pattern – This allows developers to fit more objects into the available amount of RAM by sharing common parts
of state between multiple objects instead of keeping all of the data in each other.
Problem:
A developer creates a video game with realistic particle system of multiple bullets, missiles, and explosions. When the game
is built and tested on his computer, the game is always crashing during gameplay. The developer discovers that the game
keeps on crashing because of an insufficient amount of memory. This problem is caused by the realistic particle system.
Each particle, such as the bullet, is represented by a separate object containing plenty of data. When the program creates
multiple particles, some of these particles may not fit into the remaining memory—hence the crash.
Solution:
The Flyweight pattern suggests creating an object that is divided into two (2) parts: the state-dependent part and the state-
independent part.
In the given problem, the particle system objects shared
some fields, such as color and sprit; these are state-
independents and their values remain constant. While
other fields such as coordinates, movement, and speed
are unique to each particle object, these are state-
dependents and their values may always change.
The state-independent is stored in the Flyweight object.
The state-dependent is stored or computed by client
objects and is passed to the Flyweight object when its
operations are invoked.
Structure:
o In Figure 12, the Flyweight pattern is merely an
optimization. Before applying it, make sure that a Figure 12. Flyweight pattern structure
program does have the RAM consumption problem Source: https://refactoring.guru/design-patterns/flyweight
related to having a massive number of similar objects
in memory at the same time. Make sure that this problem cannot be solved in any other meaningful way.
o The Flyweight class contains the portion of the original object’s state that can be shared between multiple objects. The
same flyweight object can be used in many different contexts. The state stored inside a flyweight is called “intrinsic.”
REFERENCES:
Classification of patterns (n.d.). In Refactoring.guru. Retrieved from https://refactoring.guru/design-patterns/classification
Design Patterns (n.d.). In Sourcemaking. Retrieved from https://sourcemaking.com/design_patterns
Dooley J. (2017). Software development, design and coding: With patterns, debugging, unit testing, and refactoring (2nd ed.).
Retrieved from https://books.google.com.ph/books?id=LGRADwAAQBAJ&dq=Software+Development,+Design+and+
Coding:+With+Patterns,+Debugging,+Unit+Testing,+and+Refactoring
Joshi, B. (2016). Beginning SOLID principles and design patterns for asp.net developers. California: Apress Media, LLC.
What’s a design pattern (n.d.). In Refactoring.guru. Retrieved from https://refactoring.guru/design-patterns/what-is-pattern
04 Handout 1 *Property of STI
student.feedback@sti.edu Page 14 of 14