0% found this document useful (0 votes)
3 views6 pages

Design Patterns

Design patterns are best practices for solving common software problems, providing templates for reliable, scalable, and maintainable applications. Key principles include Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. The document also outlines various design patterns categorized into Creational, Structural, and Behavioral patterns, each with specific use cases and advantages.

Uploaded by

Muhammed Elsayed
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)
3 views6 pages

Design Patterns

Design patterns are best practices for solving common software problems, providing templates for reliable, scalable, and maintainable applications. Key principles include Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. The document also outlines various design patterns categorized into Creational, Structural, and Behavioral patterns, each with specific use cases and advantages.

Uploaded by

Muhammed Elsayed
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

What are Design Patterns: Best practice to solve common software problems, Solutions in the form of

templates that may be applied to real-world problems. makes your application reliable, scalable, and maintainable.

Principles:
• Single Responsibility Principle (SRP): The class should solve only one problem; it should have a single reason to
change.

• Open/Closed Principle (OCP): The class should be open for extension and closed for modifications.

• Liskov Substitution Principle: If you substitute any type with one of its subtypes, the behavior should not change.

• Interface Segregation Principle: Avoid making a general interface containing all methods.

• Dependency Inversion Principle: Higher Level Classes should not know the implementation of low-level classes but
depends on abstraction.

UML (Unified Model Language):


UML is a developmental modeling language in the field of software engineering that is intended to visualize the design
of system in terms of classes and objects (Its primary purpose during analysis workflow).

Not represented in a UML:


How the classes interact with each other & Algorithmic details; how a particular behavior is implemented.
Creational Patterns
Pattern Description UML
+
(Example)
Singleton Advantages:
• Can be sure that a class has only a single
instance.
• Gain global access point to that instance.
• Singleton object is initialized only when it’s
requested for the first time
Disadvantages:
• Violates the Single Responsibility Principle.
• Singleton patterns require a special
mechanism in a multi-threaded (Multiple
instances at the same time).
• Difficult to unit test.
Prototype Copy initialization objects instead of creating
it.
Use it when:
• the classes are instantiated at runtime.
• the cost of creating an object is expensive or
complicated.
• you want to keep the number of classes in an
application minimum.
• the client application needs to be unaware of
object creation and representation.
Advantages:
•reduces the need of sub-classing.
•hides complexities of creating objects.
•The clients can get new objects without
knowing which type of object it will be.
•lets you add or remove objects at runtime.

Builder Use it when you want:


• To build an object made up of other objects.
(Starbucks) • The creation of these parts to be independent
(Doc Type)
(Car Type)
of the main object.
• Hide the creation of the parts from the clients
so both aren't dependent.
The builder knows the specifics.
Advantages it provides:
• Separation between the construction and
representation of an object.
• Control over the construction process.
• Changing the internal representation of
objects
Factory Define an interface for creating an object, but
let subclasses decide which class to
(Bank Type) instantiate.
(Shapes)
Create an object without exposing the creation
logic to the client and refer to newly created
object using a common interface.

Structural Patterns
Pattern Description UML
+
(Example)

Proxy • Provide a surrogate or placeholder for


another object to control access to it
(Internet Types:
access) ➢ Remote: clients can access objects in a
remote location as if they are co-
located with them.
➢ Virtual: creates an instance of an
expensive Object only on demand.
➢ Protection: regulates access to the
original object similar to authorization.

Decorator Attach additional responsibilities to an object


dynamically.
(Starbucks)
Decorators provide a flexible alternative to
subclasses to extend flexibility, It is like
wrapper.

Useful in providing runtime modification.

Easy to maintain and extend when the number


of choices is more.

Adapter Convert the interface of a class into another


interface that client expects.
(Ducks)
(Rectangles) Makes classes work together that could not
because of incompatible interfaces.
Façade unified interface to a set of interfaces in a
subsystem.
(Car Engine)
Defines a higher-level interface that makes
the subsystem easier to use.

The primary purpose of the Facade is to hide


the complexities of a system by providing a
simpler interface to deal with.
computer system's POST procedure which
runs when we start our system it checks RAM,
CPU.
Flyweight Use sharing to support large numbers of
fine-grained objects efficiently.
It is an optimization:
Before applying it, make sure your program
does have the RAM consumption problem
related to having a massive number of
similar objects in memory.

Let’s you fit more objects into the available


RAM by sharing common parts of the state
between multiple objects, instead of
storing all of the data in each object
individually
Composite Allows you to compose objects into tree
structures to represent part-whole
)Army) hierarchies.

Composite lets clients treat individual objects


and compositions of objects uniformly
Bridge Decoupling the interface from
implementation and hiding the
(Shape+Color) implementation detail of abstraction from
the client is the main objective of the
bridge design pattern

Difference between Façade and Proxy Pattern:


• The purpose of the Proxy is to add behavior while the purpose of the Facade is to simplify.
• Proxy object represents a single object while Facade object represents a subsystem of the object.
• The client object cannot access the target object directly while the client object does have the ability to access the subsystem
object.
Difference between Façade and builder/factory Pattern:
• The facade pattern is used when you want to hide an implementation or otherwise make available a different interface
externally.
• The builder/factory pattern is used when you want to hide the details of constructing instances.
Behavioral Patterns
Pattern Description UML
+
(Example)

Strategy Enables an algorithm’s behavior to be


(Ducks)
(Engineer & selected at run time:
workers)
• Defines a family of algorithms.
• Encapsulates each one.
• Makes them interchangeable.

A Strategy pattern is a behavioral


pattern that can be used to select a
function algorithm at runtime based
on some input parameters’ behavioral

COR • Decouples the sender’s request and its


receivers
(Spam Email)
(Student
questions) • Simplifies your object: it doesn’t have to
know the chain’s structure and keeps direct
references to chain members.

•Allows you to add or remove


responsibilities dynamically
Command Encapsulates a request as a command and
pass it to invoker.
(Remote) Invoker pass the command to the
(Money corresponding object and that object
Transfer) executes the command.

• Also known as Action or Transaction.


Mediator The mediator pattern is used to reduce
communication complexity between multiple
(Airplanes objects or classes. This pattern provides a
Landing)
mediator class which normally handles all the
communications between different classes
and supports easy maintainability of the code
by loose coupling.
Memento Use the Memento Pattern when you need to be
able to return an object to one of its previous
states; for instance, if your user requests an
“undo.”

Observer An Observer Pattern says that "just define a


one-to-one dependency so that when one
object changes state, all its dependents are
notified and updated automatically".

The observer pattern is also known as


Dependents or Publish Subscribe

State A State Pattern says that "the class behavior


changes based on its state". In State Pattern,
we create objects that represent various
states and a context object whose behavior
varies as its state object changes. The State
Pattern is also known as Objects for States

You might also like