Mhd Adel Alajami 1 
DESIGN PATTERN 
8/29/2014 Mhd Adel Alajami
Agenda 
Mhd Adel Alajami 8/29/2014 
2 
 Understanding the need for design patterns 
 What are design patterns 
 Using design patterns 
 State Design Patter 
 Singleton Design Patter 
 Façade Design Patter
Software Development 
Mhd Adel Alajami 8/29/2014 
3
Mhd Adel Alajami 8/29/2014 
4 
Software Development
Mhd Adel Alajami 8/29/2014 
5 
Software Development
Mhd Adel Alajami 8/29/2014 
6 
Software Development
the need for design patterns 
Mhd Adel Alajami 8/29/2014 
7 
 Software changes 
We need to add a new features - boss 
There is a change in the specification - customer 
We have found a big bug – team 
Flexible code 
Maintainable Code
Use Design Pattern 
Mhd Adel Alajami 8/29/2014 
8 
 By learning theme 
 And then applying them in your OOP designs 
 You will create more flexible and maintainable 
code
Dealing with change 
Mhd Adel Alajami 8/29/2014 
9 
 Software has lots of ways to change 
 There are many design pattern to deal with 
changes 
 Design patterns address many of the ways 
software changes over time
What are design pattern ? 
Mhd Adel Alajami 8/29/2014 
10 
 Design patterns are general solutions to 
common problems 
 A pattern is a guideline for flexible and resilient 
code design 
 It does not go in detail of implementation 
 There is no DP for Banking system and other 
for games 
 It is a general solution for common problem in 
software development
What are design pattern ? 
Mhd Adel Alajami 8/29/2014 
11 
 “I have a problem, when one of my object 
changes , I need to let all other objects know. 
Is there a good way to do that?” 
 This is a common problem in software 
development 
 There is a proven method to solve it : The 
Observer Pattern
Where did DP come from ? 
Mhd Adel Alajami 8/29/2014 
12 
 23 original pattern 
 Now, there are many patterns for many 
different software problems
How To Use Design Pattern 
Mhd Adel Alajami 8/29/2014 
13 
 A design pattern is not a library, module or 
package 
 It’s a guideline for how to solve a problem 
 Higher level than a library 
 First understand the pattern, and then use its 
design in your software
Using Design Pattern 
Mhd Adel Alajami 8/29/2014 
14 
 Ultimate in reuse 
 Build from years of software development 
experience 
 Save times, and trial and error 
 Not reusing code, but reusing experience
State Machine 
Mhd Adel Alajami 8/29/2014 
15
Classic Way – Without Pattern 
Mhd Adel Alajami 8/29/2014 
16
Mhd Adel Alajami 8/29/2014 
17 
Classic Way – Without Pattern
Mhd Adel Alajami 8/29/2014 
18 
Classic Way – Without Pattern
We Need Another State 
Mhd Adel Alajami 8/29/2014 
19
We Need Another State 
Mhd Adel Alajami 8/29/2014 
20
Disadvantage 
Mhd Adel Alajami 8/29/2014 
21 
 Is not really OO at all 
 Any additions require many change to code 
 Difficult to understand all the state and 
transitions 
 Violates open closed principle
Think of Each State as an 
object 
Mhd Adel Alajami 8/29/2014 
22
Mhd Adel Alajami 8/29/2014 
23 
Think of Each State as an 
object
Class Diagram 
Mhd Adel Alajami 8/29/2014 
24
Implement State Design Pattern 
Mhd Adel Alajami 8/29/2014 
25
Mhd Adel Alajami 8/29/2014 
26 
Implement State Design Pattern
Mhd Adel Alajami 8/29/2014 
27 
Implement State Design Pattern
Advantage 
Mhd Adel Alajami 8/29/2014 
28
Singleton Design Pattern 
Mhd Adel Alajami 8/29/2014 
29
Singleton Uses 
Mhd Adel Alajami 8/29/2014 
30
Why can’t I Just Instantiate 
One? 
Mhd Adel Alajami 8/29/2014 
31
Prevent Instantiation 
Mhd Adel Alajami 8/29/2014 
32
Prevent Instantiation 
Mhd Adel Alajami 8/29/2014 
33
Mhd Adel Alajami 8/29/2014 
34 
Prevent Instantiation
Class Diagram 
Mhd Adel Alajami 8/29/2014 
35
What About Singleton With 
Multithreading 
Mhd Adel Alajami 8/29/2014 
36 
 To Avoid creation Multi Instance in 
Multithreading
Mhd Adel Alajami 8/29/2014 
37 
What About Singleton With 
Multithreading
Before Façade Design Pattern 
Need to communicate 
with 
Mhd Adel Alajami 8/29/2014 
38 
Client Classes 
Subsystem classes
Façade Design Pattern 
Mhd Adel Alajami 8/29/2014 
39 
Client Classes 
Subsystem classes 
Facade
Benefits 
Mhd Adel Alajami 8/29/2014 
40 
 Shields clients from subsystem classes; reduces 
the number of objects that clients deal with. 
 Promotes weak coupling between subsystem and 
its clients. 
 Helps in layering the system. Helps eliminate 
circular dependencies. 
 Makes using the subsystem easier
Any Questions ? 
Mhd Adel Alajami 8/29/2014 
41

Design pattern

  • 1.
    Mhd Adel Alajami1 DESIGN PATTERN 8/29/2014 Mhd Adel Alajami
  • 2.
    Agenda Mhd AdelAlajami 8/29/2014 2  Understanding the need for design patterns  What are design patterns  Using design patterns  State Design Patter  Singleton Design Patter  Façade Design Patter
  • 3.
    Software Development MhdAdel Alajami 8/29/2014 3
  • 4.
    Mhd Adel Alajami8/29/2014 4 Software Development
  • 5.
    Mhd Adel Alajami8/29/2014 5 Software Development
  • 6.
    Mhd Adel Alajami8/29/2014 6 Software Development
  • 7.
    the need fordesign patterns Mhd Adel Alajami 8/29/2014 7  Software changes We need to add a new features - boss There is a change in the specification - customer We have found a big bug – team Flexible code Maintainable Code
  • 8.
    Use Design Pattern Mhd Adel Alajami 8/29/2014 8  By learning theme  And then applying them in your OOP designs  You will create more flexible and maintainable code
  • 9.
    Dealing with change Mhd Adel Alajami 8/29/2014 9  Software has lots of ways to change  There are many design pattern to deal with changes  Design patterns address many of the ways software changes over time
  • 10.
    What are designpattern ? Mhd Adel Alajami 8/29/2014 10  Design patterns are general solutions to common problems  A pattern is a guideline for flexible and resilient code design  It does not go in detail of implementation  There is no DP for Banking system and other for games  It is a general solution for common problem in software development
  • 11.
    What are designpattern ? Mhd Adel Alajami 8/29/2014 11  “I have a problem, when one of my object changes , I need to let all other objects know. Is there a good way to do that?”  This is a common problem in software development  There is a proven method to solve it : The Observer Pattern
  • 12.
    Where did DPcome from ? Mhd Adel Alajami 8/29/2014 12  23 original pattern  Now, there are many patterns for many different software problems
  • 13.
    How To UseDesign Pattern Mhd Adel Alajami 8/29/2014 13  A design pattern is not a library, module or package  It’s a guideline for how to solve a problem  Higher level than a library  First understand the pattern, and then use its design in your software
  • 14.
    Using Design Pattern Mhd Adel Alajami 8/29/2014 14  Ultimate in reuse  Build from years of software development experience  Save times, and trial and error  Not reusing code, but reusing experience
  • 15.
    State Machine MhdAdel Alajami 8/29/2014 15
  • 16.
    Classic Way –Without Pattern Mhd Adel Alajami 8/29/2014 16
  • 17.
    Mhd Adel Alajami8/29/2014 17 Classic Way – Without Pattern
  • 18.
    Mhd Adel Alajami8/29/2014 18 Classic Way – Without Pattern
  • 19.
    We Need AnotherState Mhd Adel Alajami 8/29/2014 19
  • 20.
    We Need AnotherState Mhd Adel Alajami 8/29/2014 20
  • 21.
    Disadvantage Mhd AdelAlajami 8/29/2014 21  Is not really OO at all  Any additions require many change to code  Difficult to understand all the state and transitions  Violates open closed principle
  • 22.
    Think of EachState as an object Mhd Adel Alajami 8/29/2014 22
  • 23.
    Mhd Adel Alajami8/29/2014 23 Think of Each State as an object
  • 24.
    Class Diagram MhdAdel Alajami 8/29/2014 24
  • 25.
    Implement State DesignPattern Mhd Adel Alajami 8/29/2014 25
  • 26.
    Mhd Adel Alajami8/29/2014 26 Implement State Design Pattern
  • 27.
    Mhd Adel Alajami8/29/2014 27 Implement State Design Pattern
  • 28.
    Advantage Mhd AdelAlajami 8/29/2014 28
  • 29.
    Singleton Design Pattern Mhd Adel Alajami 8/29/2014 29
  • 30.
    Singleton Uses MhdAdel Alajami 8/29/2014 30
  • 31.
    Why can’t IJust Instantiate One? Mhd Adel Alajami 8/29/2014 31
  • 32.
    Prevent Instantiation MhdAdel Alajami 8/29/2014 32
  • 33.
    Prevent Instantiation MhdAdel Alajami 8/29/2014 33
  • 34.
    Mhd Adel Alajami8/29/2014 34 Prevent Instantiation
  • 35.
    Class Diagram MhdAdel Alajami 8/29/2014 35
  • 36.
    What About SingletonWith Multithreading Mhd Adel Alajami 8/29/2014 36  To Avoid creation Multi Instance in Multithreading
  • 37.
    Mhd Adel Alajami8/29/2014 37 What About Singleton With Multithreading
  • 38.
    Before Façade DesignPattern Need to communicate with Mhd Adel Alajami 8/29/2014 38 Client Classes Subsystem classes
  • 39.
    Façade Design Pattern Mhd Adel Alajami 8/29/2014 39 Client Classes Subsystem classes Facade
  • 40.
    Benefits Mhd AdelAlajami 8/29/2014 40  Shields clients from subsystem classes; reduces the number of objects that clients deal with.  Promotes weak coupling between subsystem and its clients.  Helps in layering the system. Helps eliminate circular dependencies.  Makes using the subsystem easier
  • 41.
    Any Questions ? Mhd Adel Alajami 8/29/2014 41