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

Design p atterns

  • 1.
  • 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 Designpattern Design + Pattern
  • 4.
  • 5.
  • 6.
    Pattern • Its likea show, or some repeatable design.
  • 7.
    Principles of ClassDesign (OOP Design Principles) • Low level Class Design Principles • High Level Class Design Principles
  • 8.
    Low Level ClassDesign Principles • Tell Don’t Ask • Once and only Once • Law of Demeter • Favor Composition over inheritance. • Command Query Separation.
  • 9.
    Low Level ClassDesign 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 ClassDesign 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 ClassDesign 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 ClassDesign 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 DesignPrinciples • Single Responsibility. • Open Closed Principle. • Liskove Substitution Principle • Interface Segregation Principle. • Dependency Inversion Principle.
  • 14.
    High-level Class DesignPrinciples • 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 DesignPrinciples • 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 DesignPrinciples • 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 DesignPrinciples • 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)
  • 18.
  • 19.
    Design PAtterns Creational Factory Abstract Factory singelton objectbool prototype Builder Structural Bridge Composite Decorator Facade proxy Behavior Interpreter Iterator Chain observer Strategy template
  • 20.
    Singleton • Singleton isused 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. • Howto make sure that a class could be instantiated only once. • Private constructor. • Static keyword.
  • 22.
  • 23.
  • 24.
    Singleton cont. • Otherways 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 ?
  • 26.
  • 27.
  • 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 patterncont. • 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 patterncont. • Use Case : use enumeration as iterator
  • 32.
  • 33.
    Adapter design patterncont. • Class Adapter pattern. • Think how to make the Adapter on the Class level, not on the object level.
  • 34.
    Adapter design patterncont. • 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 • DesignPrinciples • 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 Patterncont. What you think ?
  • 38.
    Template Design Patterncont. • To enhance this code, we use template design pattern.
  • 39.
  • 40.
    Template Design Patterncont. • 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 Patterncont. • 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.
  • 43.
  • 44.
  • 45.
    Abstract Factory Designpattern. • 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.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
    Observer Design Patterncont. • 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 Patterncont. • 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.
  • 52.
  • 53.
  • 54.
  • 55.
    Wrap up • DesignPrinciples • 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 singletonimplementation. 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.