SlideShare a Scribd company logo
Design Patterns
By Amr A. Ellatief
Agenda
• Design Principles
• What is Design pattern
• Types of design patterns
• Singleton pattern
• Strategy pattern
• Adaptor pattern
• Template pattern
• Factory pattern
• Abstract Factory
• Observer pattern
What is Design pattern
Design + Pattern
Design
Design
Pattern
• Its like a show, or some repeatable design.
Principles of Class Design (OOP Design
Principles)
• Low level Class Design Principles
• High Level Class Design Principles
Low Level Class Design Principles
• Tell Don’t Ask
• Once and only Once
• Law of Demeter
• Favor Composition over inheritance.
• Command Query Separation.
Low Level Class Design Principles
• Tell Don’t Ask.
• Encapsulate object with its behavior.
• Instead of getting data from object and process it, try to process data in the object.
• Once and only Once.
• If there are two code snippets in your project which are identical, try to put them in
a function and reuse it.
• It reduce the number of lines hence number of probaple bugs, defects,and testing.
• Best line of code, is the line that you don’t write.
• public final constant , Constants.
Low Level Class Design Principles
• Law of Demeter
• Try to not call many nested classes.
• As CObject1.getObject2().getObject3().getObject4()
• This will decrease the decoupling.
• Will reduce the ability to test.
• Reusing your code will be easier.
Low Level Class Design Principles
• Favor Composition over inheritance.
• In inheritance you benefit from the Super (father) class properties, but think
what happens if we changed the Super Class.(Fragile code)
• Composition is better for testability.
• Composition gives you benefits which may be changed in runtime.
Low Level Class Design Principles
• Command Query Separation.
• Queries : return values but don’t change the Queried Entity State.
• getEmployeeName();
• Commands: Don’t return values ,but change the target Entity State.
• updateEmployee();
High-level Class Design Principles
• Single Responsibility.
• Open Closed Principle.
• Liskove Substitution Principle
• Interface Segregation Principle.
• Dependency Inversion Principle.
High-level Class Design Principles
• Single Responsibility.
• Try to make your Class responsible for single task only.
• Applies on Layers too, jsp and java code.
• Advantages:
• Low coupling .
• Lower dependency.
• Change in one point.
• Disadvantages
• Many classes.
High-level Class Design Principles
• Open Closed Principle.
• Better if the System extension could be done, without the change of the code
of the system.
• What we mean is that: system open for extension ,closed for modification.
• As Template design pattern.
High-level Class Design Principles
• Liskove Substitution Principle
• Try to make the overridden method have the same result in the child
methods.
• Interface Segregation Principle.
• Don’t use the fat interfaces. It leads to fat Classes.
• We don’t like to make the client override un wanted methods.
• Same idea of modularity.
High-level Class Design Principles
• Dependency Inversion Principle.
• Try to make all Classes are depend on interface in their creation.
• The creation should depend on abstraction.
• It enable a third party to control the instantiation of objects.
• You may reduce the new operator instead try some creational patterns
(factory ,abstract factory)
High-level Class Design Principles
Design PAtterns
Creational
Factory
Abstract Factory
singelton
object bool
prototype
Builder
Structural
Bridge
Composite
Decorator
Facade
proxy
Behavior
Interpreter
Iterator
Chain
observer
Strategy
template
Singleton
• Singleton is used when you need to create certain instance from
object.
• You need to keep the same instance most-likely for the hole life cycle
of your app.
• It may be more than one instance could be 1 ,2, .. N instances.
Singleton Cont.
• How to make sure that a class could be instantiated only once.
• Private constructor.
• Static keyword.
Singleton cont.
• Class diagram
Singleton cont.
Singleton cont.
• Other ways to make singleton
• Thread safe singleton
• Eager singelton
Strategy Pattern
• Problem:
• When you choose from different algorithms in run time, and you may
extend those algorithms in Future.
• Pseudo Code (Bad implementation):
public void doAlgorithm(){
if(algorithm.type=="Algorithm 1"){
perform algorithm_1;
}else if (algorithm.type=="Algorithm 2"){
perform algorithm_1;
}else if(algorithm.type="Algorithm 3"){
perform algorithm 3;
}
What your opinion in this Code ?
Strategy Pattern cont.
Strategy Pattern cont.
Strategy Pattern cont.
• We use the Strategy pattern usually to enable the client Choice from
Algorithms at runtime.
• It hides the complexity of the Algorithm Details from the client.
• It enable for add new algorithm concrete classes without change in
the user class. (Open Closed Principle OCP)
• It reduce the complexity of nested if statement in the Code.
Adapter design pattern
• Problem:
• Some time you want to use an object but in the shape of other
object.
• As example use an Enumeration as iterator.
• enumeration.hasMoreElements() -- > iterator.hasNext()
• enumeration. nextElement() ---> iterator. next()
Adapter design pattern cont.
• Adapters change the interface of a class without changing its basic
functionality.
• For instance, they might permit interoperability between a geometry
package that requires angles to be specified in radians and a client
that expects to pass angles in degrees.
Adapter design pattern cont.
• Use Case : use enumeration as iterator
Adapter design pattern cont.
Adapter design pattern cont.
• Class Adapter pattern.
• Think how to make the Adapter on the Class level, not on the object
level.
Adapter design pattern cont.
• Adapter is about wrapping the basic class in other representable
interface.
• Its Similar to the Facade pattern (however in Façade pattern we may
wrap many classes in one simple interface)
Wrap up
• Design Principles
• What is Design pattern
• Types of design patterns
• Singleton pattern
• Strategy pattern
• Adaptor pattern
• Template pattern
• Factory pattern
• Abstract Factory
• Observer pattern
Template Design Pattern
• Problem:
• There are two concrete classes which have the similar functionality
however differ only in a few methods.
Template Design Pattern cont.
What you think ?
Template Design Pattern cont.
• To enhance this code, we use template design pattern.
Template Design Pattern cont.
Template Design Pattern cont.
• We put the same functionality in one abstract class.
• Making all the concrete objects inherit those common functionality
and perform its own action.
• We could override one of the inherited methods, in this case we call
those as hooks.
• If we want to inforce the user to use certain method with the
functionality we decided in the abstract class we use final.
Template Design Pattern cont.
• Note that the method prepare in the abstract class above defines the
main algorithm steps.
•
Factory Design pattern
• it's used to construct objects such that they can be decoupled from
the implementing system.
• The factory design pattern is a solution for the object creation, so it
considered as an creational design pattern.
• Its an alternative way for the new operator.
• It generally used to make a single point for creating a category of
opjects.
Factory Design pattern cont.
Factory Design pattern cont.
Abstract Factory Design pattern.
• It considered as additional abstract layer of factory design pattern.
• Abstract factory is used to generate factories , which generates the
objects.
• It should be used when the need is for two categorization levels of
created object required.
Abstract Factory Design pattern.
Abstract Factory Design pattern.
Abstract Factory Design pattern.
Observer Design Pattern
Observer Design Pattern cont.
• One subject many object that Listen.
• When the subject changed the other object must know
• New Object could be added.
Public updateSubscripers(){
data1=getData ();
Subscriper1.update(data1);
Subscriper1.update(data1);
Subscriper1.update(data1);
// what ypur openion of this solution
}
Observer Design Pattern cont.
• Subject : the instance that holds the data, and update the observers.
• Observers: the objects that use the data,and depend on the subject
to update them.
• Each observer implements an interface has method in the Subject.
• Each Subject updates the data in observer by calling this method.
Observer Design Pattern cont.
Observer Design Pattern cont.
Observer Design Pattern cont.
Wrap up
• Design Principles
• What is Design pattern
• Types of design patterns
• Singleton pattern
• Strategy pattern
• Adaptor pattern
• Template pattern
• Factory pattern
• Abstract Factory
• Observer pattern
Labs
1. create singleton implementation.
2. create singleton implementation supports 3 instances.
3. create singleton implementation supports N instances.
4. Create template design pattern.
5. Create strategy design pattern.
6. Create Factory design pattern.
7. Create factory design pattern which reads the created instances
from properties file.
8. Create observer design pattern.
Design p atterns
Design p atterns

More Related Content

PPTX
Micro frontend
PPTX
Introduction to Angular 2
PPTX
Swagger - Making REST APIs friendlier
PDF
Angular 2 overview
PDF
AngularJS Basics
PPTX
PDF
Using JHipster for generating Angular/Spring Boot apps
PDF
Overview of the AngularJS framework
Micro frontend
Introduction to Angular 2
Swagger - Making REST APIs friendlier
Angular 2 overview
AngularJS Basics
Using JHipster for generating Angular/Spring Boot apps
Overview of the AngularJS framework

What's hot (20)

PPTX
Angular 2
PDF
An Intro to Angular 2
PPTX
Valentine with Angular js - Introduction
PPTX
Angularjs
PPT
Angular js
PDF
Using JHipster for generating Angular/Spring Boot apps
PPT
Angular js
PDF
Angular 2 interview questions and answers
PDF
Introduction to React Native
PDF
Tech Webinar: Angular 2, Introduction to a new framework
PDF
Introduction to Angular 2
PPTX
PPT on Angular 2 Development Tutorial
PPTX
What’s new in angular 2
PDF
What angular 13 will bring to the table
PPTX
Introduction to angular 2
PDF
Appcelerator Titanium Alloy
PDF
Titanium Alloy Tutorial
PPTX
Angular elements - embed your angular components EVERYWHERE
PDF
Introduction to React Native
PDF
Angular 2 Essential Training
Angular 2
An Intro to Angular 2
Valentine with Angular js - Introduction
Angularjs
Angular js
Using JHipster for generating Angular/Spring Boot apps
Angular js
Angular 2 interview questions and answers
Introduction to React Native
Tech Webinar: Angular 2, Introduction to a new framework
Introduction to Angular 2
PPT on Angular 2 Development Tutorial
What’s new in angular 2
What angular 13 will bring to the table
Introduction to angular 2
Appcelerator Titanium Alloy
Titanium Alloy Tutorial
Angular elements - embed your angular components EVERYWHERE
Introduction to React Native
Angular 2 Essential Training
Ad

Similar to Design p atterns (20)

PPTX
Creational Design Patterns.pptx
PPTX
Segue to design patterns
PPTX
Code reviews
PPTX
Principled And Clean Coding
PPTX
design patter related ppt and presentation
PPTX
Clean code presentation
PPTX
Clean code
PPTX
Design pattern
PPTX
Generalization in Auto-Testing. How we put what we had into new Technological...
PDF
Orthogonality: A Strategy for Reusable Code
PPTX
Improving Software Quality Using Object Oriented Design Principles
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
10-design-patterns1.ppt.software engineering
PPTX
UNIT IV DESIGN PATTERNS.pptx
PDF
Software Design Patterns. Part I :: Structural Patterns
PDF
Working With Concurrency In Java 8
PPTX
Eurosport's Kodakademi #2
PDF
Commonly used design patterns
PDF
Building iOS App Project & Architecture
Creational Design Patterns.pptx
Segue to design patterns
Code reviews
Principled And Clean Coding
design patter related ppt and presentation
Clean code presentation
Clean code
Design pattern
Generalization in Auto-Testing. How we put what we had into new Technological...
Orthogonality: A Strategy for Reusable Code
Improving Software Quality Using Object Oriented Design Principles
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
10-design-patterns1.ppt.software engineering
UNIT IV DESIGN PATTERNS.pptx
Software Design Patterns. Part I :: Structural Patterns
Working With Concurrency In Java 8
Eurosport's Kodakademi #2
Commonly used design patterns
Building iOS App Project & Architecture
Ad

More from Amr Abd El Latief (11)

PPTX
master-journey.pptx
PPTX
I feel presentation [autosaved]
PPTX
AngularJs advanced Topics
PPTX
Angular js slides
PPTX
Data mining concepts and work
PPTX
Test vector compression
PPTX
Designing energy efficient lte
PPT
Stock market analysis using ga and neural network
DOCX
Chromium os architecture report
PPTX
Marketing plane of cadbry bupply kids
PPTX
Test vector compression in Digital Testing
master-journey.pptx
I feel presentation [autosaved]
AngularJs advanced Topics
Angular js slides
Data mining concepts and work
Test vector compression
Designing energy efficient lte
Stock market analysis using ga and neural network
Chromium os architecture report
Marketing plane of cadbry bupply kids
Test vector compression in Digital Testing

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Introduction to Artificial Intelligence
PDF
medical staffing services at VALiNTRY
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
System and Network Administration Chapter 2
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administraation Chapter 3
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How Creative Agencies Leverage Project Management Software.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Introduction to Artificial Intelligence
medical staffing services at VALiNTRY
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Reimagine Home Health with the Power of Agentic AI​
System and Network Administration Chapter 2
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administraation Chapter 3
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How Creative Agencies Leverage Project Management Software.pdf

Design p atterns

  • 2. Agenda • Design Principles • What is Design pattern • Types of design patterns • Singleton pattern • Strategy pattern • Adaptor pattern • Template pattern • Factory pattern • Abstract Factory • Observer pattern
  • 3. What is Design pattern Design + Pattern
  • 6. Pattern • Its like a show, or some repeatable design.
  • 7. Principles of Class Design (OOP Design Principles) • Low level Class Design Principles • High Level Class Design Principles
  • 8. Low Level Class Design Principles • Tell Don’t Ask • Once and only Once • Law of Demeter • Favor Composition over inheritance. • Command Query Separation.
  • 9. Low Level Class Design Principles • Tell Don’t Ask. • Encapsulate object with its behavior. • Instead of getting data from object and process it, try to process data in the object. • Once and only Once. • If there are two code snippets in your project which are identical, try to put them in a function and reuse it. • It reduce the number of lines hence number of probaple bugs, defects,and testing. • Best line of code, is the line that you don’t write. • public final constant , Constants.
  • 10. Low Level Class Design Principles • Law of Demeter • Try to not call many nested classes. • As CObject1.getObject2().getObject3().getObject4() • This will decrease the decoupling. • Will reduce the ability to test. • Reusing your code will be easier.
  • 11. Low Level Class Design Principles • Favor Composition over inheritance. • In inheritance you benefit from the Super (father) class properties, but think what happens if we changed the Super Class.(Fragile code) • Composition is better for testability. • Composition gives you benefits which may be changed in runtime.
  • 12. Low Level Class Design Principles • Command Query Separation. • Queries : return values but don’t change the Queried Entity State. • getEmployeeName(); • Commands: Don’t return values ,but change the target Entity State. • updateEmployee();
  • 13. High-level Class Design Principles • Single Responsibility. • Open Closed Principle. • Liskove Substitution Principle • Interface Segregation Principle. • Dependency Inversion Principle.
  • 14. High-level Class Design Principles • Single Responsibility. • Try to make your Class responsible for single task only. • Applies on Layers too, jsp and java code. • Advantages: • Low coupling . • Lower dependency. • Change in one point. • Disadvantages • Many classes.
  • 15. High-level Class Design Principles • Open Closed Principle. • Better if the System extension could be done, without the change of the code of the system. • What we mean is that: system open for extension ,closed for modification. • As Template design pattern.
  • 16. High-level Class Design Principles • Liskove Substitution Principle • Try to make the overridden method have the same result in the child methods. • Interface Segregation Principle. • Don’t use the fat interfaces. It leads to fat Classes. • We don’t like to make the client override un wanted methods. • Same idea of modularity.
  • 17. High-level Class Design Principles • Dependency Inversion Principle. • Try to make all Classes are depend on interface in their creation. • The creation should depend on abstraction. • It enable a third party to control the instantiation of objects. • You may reduce the new operator instead try some creational patterns (factory ,abstract factory)
  • 19. Design PAtterns Creational Factory Abstract Factory singelton object bool prototype Builder Structural Bridge Composite Decorator Facade proxy Behavior Interpreter Iterator Chain observer Strategy template
  • 20. Singleton • Singleton is used when you need to create certain instance from object. • You need to keep the same instance most-likely for the hole life cycle of your app. • It may be more than one instance could be 1 ,2, .. N instances.
  • 21. Singleton Cont. • How to make sure that a class could be instantiated only once. • Private constructor. • Static keyword.
  • 24. Singleton cont. • Other ways to make singleton • Thread safe singleton • Eager singelton
  • 25. Strategy Pattern • Problem: • When you choose from different algorithms in run time, and you may extend those algorithms in Future. • Pseudo Code (Bad implementation): public void doAlgorithm(){ if(algorithm.type=="Algorithm 1"){ perform algorithm_1; }else if (algorithm.type=="Algorithm 2"){ perform algorithm_1; }else if(algorithm.type="Algorithm 3"){ perform algorithm 3; } What your opinion in this Code ?
  • 28. Strategy Pattern cont. • We use the Strategy pattern usually to enable the client Choice from Algorithms at runtime. • It hides the complexity of the Algorithm Details from the client. • It enable for add new algorithm concrete classes without change in the user class. (Open Closed Principle OCP) • It reduce the complexity of nested if statement in the Code.
  • 29. Adapter design pattern • Problem: • Some time you want to use an object but in the shape of other object. • As example use an Enumeration as iterator. • enumeration.hasMoreElements() -- > iterator.hasNext() • enumeration. nextElement() ---> iterator. next()
  • 30. Adapter design pattern cont. • Adapters change the interface of a class without changing its basic functionality. • For instance, they might permit interoperability between a geometry package that requires angles to be specified in radians and a client that expects to pass angles in degrees.
  • 31. Adapter design pattern cont. • Use Case : use enumeration as iterator
  • 33. Adapter design pattern cont. • Class Adapter pattern. • Think how to make the Adapter on the Class level, not on the object level.
  • 34. Adapter design pattern cont. • Adapter is about wrapping the basic class in other representable interface. • Its Similar to the Facade pattern (however in Façade pattern we may wrap many classes in one simple interface)
  • 35. Wrap up • Design Principles • What is Design pattern • Types of design patterns • Singleton pattern • Strategy pattern • Adaptor pattern • Template pattern • Factory pattern • Abstract Factory • Observer pattern
  • 36. Template Design Pattern • Problem: • There are two concrete classes which have the similar functionality however differ only in a few methods.
  • 37. Template Design Pattern cont. What you think ?
  • 38. Template Design Pattern cont. • To enhance this code, we use template design pattern.
  • 40. Template Design Pattern cont. • We put the same functionality in one abstract class. • Making all the concrete objects inherit those common functionality and perform its own action. • We could override one of the inherited methods, in this case we call those as hooks. • If we want to inforce the user to use certain method with the functionality we decided in the abstract class we use final.
  • 41. Template Design Pattern cont. • Note that the method prepare in the abstract class above defines the main algorithm steps. •
  • 42. Factory Design pattern • it's used to construct objects such that they can be decoupled from the implementing system. • The factory design pattern is a solution for the object creation, so it considered as an creational design pattern. • Its an alternative way for the new operator. • It generally used to make a single point for creating a category of opjects.
  • 45. Abstract Factory Design pattern. • It considered as additional abstract layer of factory design pattern. • Abstract factory is used to generate factories , which generates the objects. • It should be used when the need is for two categorization levels of created object required.
  • 50. Observer Design Pattern cont. • One subject many object that Listen. • When the subject changed the other object must know • New Object could be added. Public updateSubscripers(){ data1=getData (); Subscriper1.update(data1); Subscriper1.update(data1); Subscriper1.update(data1); // what ypur openion of this solution }
  • 51. Observer Design Pattern cont. • Subject : the instance that holds the data, and update the observers. • Observers: the objects that use the data,and depend on the subject to update them. • Each observer implements an interface has method in the Subject. • Each Subject updates the data in observer by calling this method.
  • 55. Wrap up • Design Principles • What is Design pattern • Types of design patterns • Singleton pattern • Strategy pattern • Adaptor pattern • Template pattern • Factory pattern • Abstract Factory • Observer pattern
  • 56. Labs 1. create singleton implementation. 2. create singleton implementation supports 3 instances. 3. create singleton implementation supports N instances. 4. Create template design pattern. 5. Create strategy design pattern. 6. Create Factory design pattern. 7. Create factory design pattern which reads the created instances from properties file. 8. Create observer design pattern.