Design Pattern lecture 2

1,253 views

Published on

Lecture two,

An introduction to Design Pattern
History
Pattern Language,
Categorization according to GoF
MVC
Creational Design Patterm
Factory Method
Abstract Factory
Singleton
Builder

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,253
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • for example, in a road management pattern language, the problem of cars colliding at an uncontrolled intersection is identified as problem type: uncontrolled intersection.Then a generic solution is found, the result is the creation of the intersection controller design patternfor example, in the previous scenario, we applied the intersection controller design pattern to solve an uncontrolled intersection problem, in which cars collided. However, if we had another uncontrolled intersection problem that involved cars and trains, then we would implement the intersection controller design pattern differently: in the car scenario, we might implement four sets of traffic lights, whereas in the car–train scenario we might implement four sets of traffic lights and a set of crossing gates.
  • for example, in a road management pattern language, the problem of cars colliding at an uncontrolled intersection is identified as problem type: uncontrolled intersection.Then a generic solution is found, the result is the creation of the intersection controller design patternfor example, in the previous scenario, we applied the intersection controller design pattern to solve an uncontrolled intersection problem, in which cars collided. However, if we had another uncontrolled intersection problem that involved cars and trains, then we would implement the intersection controller design pattern differently: in the car scenario, we might implement four sets of traffic lights, whereas in the car–train scenario we might implement four sets of traffic lights and a set of crossing gates.
  • for example, in a road management pattern language, the problem of cars colliding at an uncontrolled intersection is identified as problem type: uncontrolled intersection.Then a generic solution is found, the result is the creation of the intersection controller design patternfor example, in the previous scenario, we applied the intersection controller design pattern to solve an uncontrolled intersection problem, in which cars collided. However, if we had another uncontrolled intersection problem that involved cars and trains, then we would implement the intersection controller design pattern differently: in the car scenario, we might implement four sets of traffic lights, whereas in the car–train scenario we might implement four sets of traffic lights and a set of crossing gates.
  • It discuss a set of 23 design patterns that they observed were commonly found in software development.
  • It discuss a set of 23 design patterns that they observed were commonly found in software development.
  • Design Pattern lecture 2

    1. 1. Design Pattern By Julie IskanderMSc. Communication and Electronics
    2. 2. Outlines Lecture 2• What is Design Patterns?• History of Design Patterns• How to learn Design Patterns?• Design Patterns Classifications acc. to GoF• Creational DP: • Singleton • Factory Method • Abstract Method • Builder • Prototype
    3. 3. What is Design Pattern• A pattern is a solution to a common problem,• NOT a code solution ready for copy-and-paste but a best practice, a useful abstraction, and a template for solving categories of problems.
    4. 4. What is Design Pattern• Design Patterns are frequently used ways of organizing objects to make solutions easier to extend and modify• Design Patterns name and describe effective solutions for common problems• Design Pattern help a designer get a design right faster. • Never reinvent the wheel.
    5. 5. What is Design Pattern• “Each pattern describes a problem which occurs over and over again, and then describes the core of the solution in such a way that you can use this solution many times without ever doing it the same way twice”, Christopher
    6. 6. History of Design Pattern• Dr. Christopher Alexander (in civil engineering), in1977, A Pattern Language.• In 1978–79, Prof. Trygve Reenskaug (in computer science), wrote “The design pattern: Model-View– Controller (MVC)”.• In 1995, a catalog of 23 design patterns by Drs. Gamma, Helm, Johnson, and Vlissides “Gang of Four” (GoF), “Design Patterns: Elements of Reusable Object- Oriented Software (Addison-Wesley, 1995)”.
    7. 7. How to learn Design patterns ?The best way touse patterns is toload your brain withthem and thenrecognize places inyour designs andexistingapplications whereyou can apply them
    8. 8. “Designing OO software is hard and designingreusable OO software even harder”,Erich GammaExperience  Reusing solutions that worked beforeKnowing patterns that worked in the past  makedesigner more productive and creating flexible andreusable designs
    9. 9. Instead of code reuse,with design patterns youget experience reuse
    10. 10. Advantages of learning Design Pattern• It helps us to communicate in an efficient manner, creates a common vocabulary• It enables us to discuss low-level implementation in a high-level, abstract manner.• It enables us to apply a proven solution (experience reuse)• It helps extendibility and avoid solution redesign• It help present design alternatives if found.• It can help improve maintenance and documentation
    11. 11. Design Pattern elements• Pattern Name• Problem : When to apply? Problem and Context• Solution• Consequence : results, trade offs of applying it
    12. 12. Model-View-Controller MVC
    13. 13. MVC• What , Where • a requirement to vary perspectives or views (output) without changing the underlying information source (model).• Why • it is more manageable and less expensive to encapsulate change in a set of controller objects than it is to disfigure the model object through constant reengineering.
    14. 14. MVC• How • Encapsulating the client requirements in a controller object that transposes the information from the model object into the variety of formats required by the client. • a model object (model) • a view interface (IView), or abstract class, if you prefer; • Deriving any number of views (GraphView and BarView classes) from the interface; • creating a controller object (controller), to represent each view; and then wiring up the functionality.
    15. 15. Design Patternsaccording to the GOF
    16. 16. DP Classifications• According to scope Class Object Deals with relationships Deals with objects between classes and their relationships that can be subclasses changed at runtime, dynamically Done by inheritance
    17. 17. DP Classifications• According to purpose Creational Structural Behavioral Gives program the Deals with the Characterizes the flexibility in composition of way classes and deciding which classes and objects objects interact and object to create. to create larger distribute When and how to structures responsibilities create objects Or add functionalities
    18. 18. DP Classifications• According to purpose Creational Structural Behavioral Singleton Adapter (Class Chain of Responsibility Factory Method Pattern) Command (Class Pattern) Bridge Iterator(Class Pattern) Abstract Factory Composite Mediator Builder Decorator Memento Prototype Façade Observer Flyweight State Proxy Strategy Template Method (Class Pattern) Visitor Interpreter
    19. 19. Creational Patterns
    20. 20. Singleton(object – creational dp)
    21. 21. Singleton (object – Creational DP)• What, Why • A class has one and only one instance created• How • private constructor
    22. 22. Singleton(object – Creational DP)
    23. 23. Singleton (object – Creational DP)• Notes: • Can permit a variable # of instances
    24. 24. Factory method (Class – Creational DP) There is more to making objects than just usingthe new operator. Instantiation is an activity thatshouldn’t always be done in public and can often lead to coupling problems.
    25. 25. Factory method (Class – Creational DP)• What • A design that defines an interface for creating objects and delegates the choice of what objects to create to subclasses. • A creator class is required to be decoupled from creating specific objects.• Why • Flexibility in creating a variety of objects• How • Subclasses, with overridden factory method create specific types, which in turn create content objects.
    26. 26. Factory method(Class – Creational DP)
    27. 27. Factory method (Class – Creational DP)• Example • An application with multiple documents • Document type to create depends on specific application • Drawing Application  Drawing Documents • Writing Application  Writing Documents
    28. 28. Factory method(Class – Creational DP)
    29. 29. Factory method (Class – Creational DP)• Another implementation: • Parametrized Factory Method • Parameter specifies the type of product to instantiate • No inheritance in creator class • All Products must implement the product interface
    30. 30. Abstract Factory DP (object– Creational DP)• What • Client deals with a high-level abstraction for creating families of products while leaving the factory abstraction to deal with coupling the implementation. • A requirement not to couple the client to an implementation that creates sets or families of objects. The pattern enables the client to commit to an interface and avoid commitment to an implementation.• Why • Enhancing the client’s ability to switch between a set or family of objects.• How • Client code has an association with the interface of the factory class. The factory objects create sets of objects .
    31. 31. Abstract Factory DP(object– Creational DP)
    32. 32. Abstract Factory DP(object– Creational DP)
    33. 33. Abstract Factory DP(object– Creational DP)
    34. 34. Abstract Factory DP (object– Creational DP)• Another Example • Web application that renders a set of controls appropriate for a given web browser type or version.
    35. 35. Abstract Factory DP (object– Creational DP)• Factories can be singletons• Factory Method for each Product, each ConcreteFactory class overrides it choosing the type of products to create •  a new concrete factory subclass for each product family• If many product families used Prototype DP
    36. 36. Builder DP(object– Creational DP)
    37. 37. Builder DP (object– Creational DP)• What • Separate Construction of complex objects from the representation of its parts • Algorithm for creating a complex object should be independent of the parts that make up the object and how theyre assembled.
    38. 38. Builder DP(object– Creational DP)
    39. 39. Builder DP(object– Creational DP)
    40. 40. Builder DP (object– Creational DP)• Example • A Schedule Builder where there are two types of Schedules(Inhouse or OnSite). • A RTF Converter where there may be text or Images (convert to PDF or ASCII text or……)
    41. 41. Report #1:Prototype DP withcode exampleN.B. Hand Written 

    ×