cisc 322
play

CISC 322 Software Architecture Lecture 15: Design Patterns 2 Emad - PowerPoint PPT Presentation

CISC 322 Software Architecture Lecture 15: Design Patterns 2 Emad Shihab Material drawn from [Gamma95, Coplien95] Slides adapted from Spiros Mancoridis and Ahmed E. Hassan Faade Pattern Motivation Structuring a system into subsystems


  1. CISC 322 Software Architecture Lecture 15: Design Patterns 2 Emad Shihab Material drawn from [Gamma95, Coplien95] Slides adapted from Spiros Mancoridis and Ahmed E. Hassan

  2. Façade Pattern Motivation ■ Structuring a system into subsystems helps reduce complexity ■ A common design goal is to minimize the communication and dependencies between subsystems ■ Use a facade object to provide a single, simplified interface to the more general facilities of a subsystem

  3. Façade Pattern Intent ■ Provide a unified interface to a set of interfaces in a subsystem. ■ Facade defines a higher-level interface that makes the subsystem easier to use

  4. Façade Example – Programming Environment Compiler Compile() ■ Programming environment that Scanner Token provides access to its CodeGenerator Parser compiler ■ Contains many classes ProgNodeBuilder (e.g. scanner, parser) RISCCG ■ Most clients don’t care ProgNode about details like StackMachineCG parsing and code generation…just Statement Node compile my code! ■ Low-level interfaces just Expression Node complicate their task Variable Node Compiler Subsystem Classes Software Design (OOD Patterns)

  5. Façade Example – Programming Environment Compiler Compile() ■ Higher-level interface (i.e., Compiler class) Scanner Token shields clients from low CodeGenerator Parser level classes ■ Compiler class defines ProgNodeBuilder a unified interface to the compiler’s RISCCG ProgNode functionality StackMachineCG ■ Compiler class acts as a Façade. It offers Statement Node clients a simple interface to the Expression Node compiler subsystem Variable Node Compiler Subsystem Classes Software Design (OOD Patterns)

  6. Façade Pattern Structure Client Classes Facade Subsystem Classes Software Design (OOD Patterns)

  7. Participants of Façade Pattern ■ Façade (compiler) – Knows which subsystem classes are responsible for a request – Delegates client requests to appropriate subsystem objects ■ Subsystem classes (Scanner, Parser,etc..) – Implements subsystem functionality – Handles work assigned by the façade object

  8. Façade Pattern Applicability ■ Use a façade when – To provide a simple interface to a complex subsystem – To decouple clients and implementation classes – To define an entry point to a layered subsystem

  9. Façade Pattern Collaborations ■ Clients communicate with the subsystem by sending requests to façade, which then forwards requests to the appropriate subsystems ■ Clients that use the façade don’t have access to its subsystem objects directly. However, clients can access subsystem classes if they need to

  10. Composite Pattern Motivation ■ Assume you have client code that needs to deal with individual objects and compositions of these objects ■ You would have to treat primitives and container classes differently, making the application more complex than necessary

  11. Composite Pattern Intent ■ Lets clients treat individual objects and compositions of objects uniformly

  12. Composite Pattern Example ■ Graphic Graphic applications allow Draw() users to build Add(Graphic) complex diagrams Remove(Graphic) out of simple GetChild(int) components ■ Users group components to form Aggregate of Graphic objects graphics larger components Line Rect. Text Picture Draw() Draw() Draw() Draw() Add(Graphic) Remove(Graphic) Primitive graphical objects GetChild(int) forall g in graphics g.Draw()

  13. Composite Pattern Example ■ A simple implementation defines classes for Graphic graphical primitives Draw() (e.g. Text and lines) Add(Graphic) plus other classes Remove(Graphic) GetChild(int) that act as containers for these primitives ■ The problem is user graphics must treat primitive and container Line Rect. Text Picture Draw() objects differently, Draw() Draw() Draw() Add(Graphic) making the Remove(Graphic) applications more GetChild(int) complex forall g in graphics g.Draw()

  14. Composite Pattern Example ■ Key is an abstract class Graphic that represents Draw() both primitives Add(Graphic) and their Remove(Graphic) GetChild(int) containers ■ Graphic declares operations such as draw that are graphics specific to graphical objects Line Rect. Text Picture Draw() ■ Also operations Draw() Draw() Draw() Add(Graphic) for accessing Remove(Graphic) and managing GetChild(int) children forall g in graphics g.Draw()

  15. Structure of Composite Pattern Manipulates objects in the composition through Component interface Declares interface for Client objects and child components Component Operation() Add(Component) Remove(Component) GetChild(int) Defines behavior for Defines behavior for components having primitive objects. Leafs children. Implements have no children children child-related operations Leaf Composite forall g in children Operation() Operation() g.Operation() Add(Component) Remove(Component) GetChild(int)

  16. Iterator Pattern Motivation ■ Aggregate objects (e.g. list) should give you a way to access its elements without exposing its internal structure ■ You might want to traverse an aggregate object in different ways ■ Sometimes cannot decide on all ways to traverse the aggregate object apriori ■ Should not bloat the interface of aggregate objects with different traversals

  17. Iterator Pattern Intent ■ Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation

  18. Iterator Pattern Example Access and traversal responsibilities are taken out of List object into an iterator object (ListIterator) list List ListIterator Count() First() Append(Element) Next() Remove(Element) IsDone() … CurrentItem() index Can define different traversal policies without enumerating them in the List interface

  19. Structure of Iterator Pattern Provides a common Interface for accessing and interface for creating traversing elements Iterator object Aggregate Iterator CreateIterator() First() Next() IsDone() CurrentItem() Implements the Iterator creation interface to return Implements the Iterator instance of ConcreteIterator interface ConcreteAggregate ConcreteIterator CreateIterator() return new ConcreteIterator(this)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend