[go: up one dir, main page]

0% found this document useful (0 votes)
5 views24 pages

Software Design Design Patterns

The document provides an overview of design patterns in software engineering, categorizing them into creational, structural, and behavioral patterns. It explains various design patterns such as Singleton, Abstract Factory, Factory Method, Adapter, Bridge, Composite, Chain of Responsibility, and Visitor, highlighting their purposes, advantages, and disadvantages. The conclusion emphasizes that design patterns are general solutions to common problems in software design, serving as templates rather than direct implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Software Design Design Patterns

The document provides an overview of design patterns in software engineering, categorizing them into creational, structural, and behavioral patterns. It explains various design patterns such as Singleton, Abstract Factory, Factory Method, Adapter, Bridge, Composite, Chain of Responsibility, and Visitor, highlighting their purposes, advantages, and disadvantages. The conclusion emphasizes that design patterns are general solutions to common problems in software design, serving as templates rather than direct implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Design pattern

awet.fesseha@rca.ac.rw

1/22/2025

awet Fesseha Design pattern 1 / 24


Outline

1 Introduction
2 Creational patterns
1.1 Singleton Pattern:
1.2 Abstract Factory
1.3 Factory Method
3 2. Structural Patterns
2.1 Adapter
2.2 Bridge
2.3 Composite
4 3. Behavioral design pattern
3.1 Chain of Responsibility
3.2 Visitor
5 Conclusion

awet Fesseha Design pattern 2 / 24


Why should I learn patterns?

Design patterns are a toolkit of tried and tested solutions to common


problems in software design. Even if you never encounter these
problems, knowing patterns is still useful because it teaches you how
to solve all sorts of problems using principles of object-oriented
design.
Design patterns define a common language that you and your
teammates can use to communicate more efficiently. You can say,
“Oh, just use a Singleton for that,” and everyone will understand the
idea behind your suggestion.
No need to explain what a singleton is if you know the pattern and its
name.

awet Fesseha Design pattern 3 / 24


What is design pattern ?

Reusable solutions for typical software design challenges are known as


design patterns.
Expert object-oriented software engineers use these best practices to
write more structured, manageable, and scalable code.

awet Fesseha Design pattern 4 / 24


Categories of pattern

1 Creational patterns
2 Structural patterns
3 Behavioral patterns

awet Fesseha Design pattern 5 / 24


Creational patterns

Creational Design Patterns are concerned with the way in which


objects are created .
They reduce complexities and instability by creating objects in a
controlled manner.
we are going to see some of the creational pattern
1 Singleton
2 Abstract factory
3 Factory

awet Fesseha Design pattern 6 / 24


1.1 Singleton Pattern:

Ensures that a class has only one instance and provides a global point
of access to it.
Ensure that a class has just a single instance. Why would anyone
want to control how many instances a class has? The most common
reason for this is to control access to some shared resource—for
example, a database or a file.
Provide a global access point to that instance. Remember those
global variables that you (all right, me) used to store some essential
objects? While they’re very handy, they’re also very unsafe since any
code can potentially overwrite the contents of those variables and
crash the app

awet Fesseha Design pattern 7 / 24


Pros and Cons of singleton

Pros
You can be sure that a class has only a single instance.
You gain a global access point to that instance.
Useful when exactly one object is needed to coordinate actions across
the system.
cons
The Singleton pattern can mask bad design, for instance, when the
components of the program know too much about each other.
The pattern requires special treatment in a multithreaded
environment so that multiple threads won’t create a singleton object
several times.

awet Fesseha Design pattern 8 / 24


1.2 Abstract Factory

Abstract Factory is a creational design pattern that lets you produce


families of related objects without specifying their concrete classes, in
other words
This is an interface or an abstract class that declares a set of methods
for creating a family of products. It doesn’t provide the
implementation details of these methods.
Analogy:
A furniture company offers themed sets like ”Modern” or ”Victorian.”
Choosing a theme automatically determines the design of all items
(chair, table, sofa).

awet Fesseha Design pattern 9 / 24


Pros and cons of Abstract factory
Pros
The pattern centralizes and hides the creation logic for families of
related objects. This ensures the client code does not need to know
the specifics of how objects are instantiated.
Ensures that objects created in a family are compatible with each
other. For example, in a GUI framework, all widgets (buttons, menus,
scrollbars) match the same design theme.
You avoid tight coupling between concrete products and client code.
cons
The Abstract Factory Pattern introduces additional layers of
abstraction, which can make the code harder to understand and
maintain for developers unfamiliar with the pattern.
The client code becomes dependent on the factory interface, which
can make testing or refactoring harder if the factory is not
well-designed.
awet Fesseha Design pattern 10 / 24
1.3 Factory Method

Factory Method is a creational design pattern that provides an


interface for creating objects in a superclass, but allows subclasses to
alter the type of objects that will be created
Analogy:
A coffee shop menu allows customers to choose between cappuccino,
latte, or espresso. The barista (factory method) prepares the
appropriate coffee based on the order.

awet Fesseha Design pattern 11 / 24


Pros and cons of Factory Method

Pros
The Factory pattern hides the object creation details, making the
code cleaner and more modular. The client only interacts with an
interface and doesn’t need to know the concrete classes.
Adding new types of objects is easy. You can create a new subclass
and update the factory method without changing existing code,
adhering to the Open/Closed Principle.
cons
Introducing a factory adds additional layers of abstraction, which can
make the code more complex, especially for simple object creation.
For straightforward applications with no complex object creation
requirements, a Factory pattern might be overkill and unnecessarily
increase the codebase.

awet Fesseha Design pattern 12 / 24


2. Structural Patterns

Structural patterns are design patterns that deal with the composition
and organization of classes and objects. They help you define how
different components of your system interact and relate to each other.
Structural patterns can simplify your code by reducing the number of
classes, hiding the complexity of the internal structure, or providing a
common interface for different implementations.
Some examples of structural patterns are Adapter, Bridge,
Composite, Decorator, Facade, Flyweight, and Proxy.

awet Fesseha Design pattern 13 / 24


2.1 Adapter

Adapter is a structural design pattern that allows objects with


incompatible interfaces to collaborate.
This is a special object that converts the interface of one object so
that another object can understand it.
An adapter wraps one of the objects to hide the complexity of
conversion happening behind the scenes. The wrapped object isn’t
even aware of the adapter.
For example, you can wrap an object that operates in meters and
kilometers with an adapter that converts all of the data to imperial
units such as feet and miles.

awet Fesseha Design pattern 14 / 24


2.1.1 Real-World Example of Adapter Design Pattern

Let’s understand this concept using a simple example:


real world example
Suppose you have two buddies, one of them speaks French exclusively and
the other English exclusively. The language barrier prevents them from
communicating the way you want them to.

You act as an adapter, translating messages between them. Your role


allows the English speaker to convey messages to you, and you convert
those messages into French for the other person.
In this way, despite the language difference, your adaptation enables
smooth communication between your friends.
This role you play is similar to the Adapter design pattern, bridging the
gap between incompatible interfaces.

awet Fesseha Design pattern 15 / 24


2.2 Bridge

Bridge is a structural design pattern that lets you split a large class
or a set of closely related classes into two separate
hierarchiesabstraction and implementationwhich can be developed
independently of eachother.
The Bridge pattern attempts to solve this problem by switching from
inheritance to the object composition.
What this means is that you extract one of the dimensions into a
separate class hierarchy, so that the original classes will reference an
object of the new hierarchy, instead of having all of its state and
behaviors within one class.

awet Fesseha Design pattern 16 / 24


2.3 Composite

The Composite Design Pattern is a structural design pattern that lets


you compose objects into tree-like structures to represent part-whole
hierarchies.
It allows clients to treat individual objects and compositions of objects
uniformly. In other words, whether dealing with a single object or a
group of objects (composite), clients can use them interchangeably.

Figure: compostie pattern

awet Fesseha Design pattern 17 / 24


3. Behavioral design pattern

Behavioral patterns are design patterns that deal with the


communication and cooperation of classes and objects. They help you
define how different components of your system behave and respond
to each other.
Behavioral patterns can improve your code by enhancing the
flexibility, modularity, and reusability of your algorithms, strategies,
and commands.
Some examples of behavioral patterns are Chain of Responsibility,
Command, Iterator, Mediator, Memento, Observer, State,
Strategy, Template Method.

awet Fesseha Design pattern 18 / 24


3.1 Chain of Responsibility

Chain of Responsibility is a behavioral design pattern that lets you


pass requests along a chain of handlers. Upon receiving a request,
each handler decides either to process the request or to pass it to the
next handler in the chain.

awet Fesseha Design pattern 19 / 24


Real-World Analogy of the Chain Of Responsibility Design
Pattern

real world example of CRDP


Imagine a customer service department with multiple levels of support
staff, each responsible for handling different types of customer inquiries
based on their complexity. The chain of responsibility can be illustrated as
follows:

Level 1 Support: This represents the first point of contact for


customer inquiries. Level 1 support staff handle basic inquiries and
provide general assistance. If they cannot resolve the issue, they
escalate it to Level 2 support.
Level 2 Support: This level consists of more experienced support staff
who can handle more complex issues that Level 1 support cannot
resolve. If Level 2 support cannot resolve the issue, they escalate it to
Level 3 support. .
awet Fesseha Design pattern 20 / 24
Real-World Analogy of the Chain Of Responsibility Design
Pattern

Level 3 Support: This is the highest level of support, consisting of


senior or specialized staff who can handle critical or highly technical
issues. If Level 3 support cannot resolve the issue, they may involve
other departments or experts within the organization.

Figure: Enter Caption

awet Fesseha Design pattern 21 / 24


3.2 Visitor

Visitor is a behavioral design pattern that lets you separate algorithms


from the objects on which they operate.
The Visitor pattern suggests that you place the new behavior into a
separate class called visitor, instead of trying to integrate it into
existing classes.
The original object that had to perform the behavior is now passed to
one of the visitor’s methods as an argument, providing the method
access to all necessary data contained within the object.

awet Fesseha Design pattern 22 / 24


Conclusion of Design Pattern

In software engineering , a design pattern is a general repeatable


solution to a commonly occurring problem in software design.
A design pattern isn’t a finished design that can be transformed
directly into code.
It is a description or template for how to solve a problem that can be
used in many different situations.

awet Fesseha Design pattern 23 / 24


Thank you!
Questions?

awet Fesseha Design pattern 24 / 24

You might also like