cisc 322
play

CISC 322 Software Architecture Lecture 16: Design Patterns 3 Emad - PowerPoint PPT Presentation

CISC 322 Software Architecture Lecture 16: Design Patterns 3 Emad Shihab Material drawn from [Gamma95, Coplien95] Slides adapted from Spiros Mancoridis and Ahmed E. Hassan Template Pattern Intent Define the skeleton of an algorithm in an


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

  2. Template Pattern Intent ■ Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. ■ The Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

  3. Template Pattern Motivation ■ Consider an application that provides Application and Document classes – Application: opens existing document – Document: represents the information in a doc ■ By defining some of the steps of an algorithm, using abstract operations, the template method fixes their ordering.

  4. Template Pattern Motivation ■ Specific applications can subclass Application and Document to suit their specific needs – Drawing application: defines DrawApplication and DrawDocument sublclasees – Spreadsheet application: defines SpreadsheetApplication and SpreadsheetDocument sublclasees

  5. Template Pattern Example OpenDoc is a template • CanOpenDoc() – method that defines each Document Application step for opening a doc check if doc can be document AddDoc() Save() opened OpenDoc() Open() • DoCreateDoc() – DoCreateDoc() Close() create doc CanOpenDoc() DoRead() • AboutToOpenDoc() AboutToOpenDoc() – lets application know when a doc is about to be opened MyDocument Application DoCreateDoc() DoRead() CanOpenDoc() AboutToOpenDoc() return new MyDoc

  6. Template Pattern Structure AbstractClass – defines Implements a template method defining the skeleton. The abstract primitive operations template method calls primitive that concrete subclass ops and operations defined in implement the Abstract class AbstractClass ... PrimitiveOp1() TemplateMethod() PrimitiveOp2() PrimitiveOp1() ... PrimitiveOp2() Concrete class – implements primitive ops to carry out subclasss-specific steps of an algorithm ConcreteClass PrimitiveOp1() PrimitiveOp2()

  7. Observer Pattern Motivation ■ A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. ■ How can you achieve consistency?

  8. Observer Intent ■ Define a one-to-many dependency between objects so that when one object changes state , all its dependents are notified and updated automatically .

  9. Observer Pattern Example a b c b x 60 30 10 Observer y 50 30 20 a c z 80 10 10 a b c a = 50% b = 30% Subject c = 20% requests, modifications change notification

  10. Observer Pattern Structure Defines interface for objects Provides an interface for that should be notified of attaching and detaching changes in a subject Observer objects observers Subject Observer Attach(Observer) Update() Detach(Observer) for all o in Notify() Implements the Observer observers { interface to keep its state o -> Update()} consistent with the subject Sends a notification to observers when its state ConcreteObserver changes observerState = Update() subject->GetState() subject observerState ConcreteSubject GetState() return subjectState SetState() subjectState

  11. Master-Slave Pattern Motivation ■ Fault tolerance is a critical factor in many systems. ■ Replication of services and delegation of the same task to several independent suppliers is a common strategy to handle such cases.

  12. Master-Slave Pattern Intent ■ Independent components providing the same service (slaves) are separated from a component (master) responsible for invoking them and for selecting a particular result from the results returned by the slaves. ■ (Master) Handles the computation of replicated services within a software system to achieve fault tolerance and robustness.

  13. Master-Slave Pattern Example Slave1 RadLevel() NuclearPP Voter Slave2 acceptableRL() RadLevel() RadLevel() return max( Slave3 slave1->RadLevel(), slave2->RadLevel(), RadLevel() slave3->RadLevel())

  14. Master-Slave Pattern Structure Slave1 ServiceImp1() Organizes the invocation of forward Requests a service to solve replicated services and its task decides which of the request results to pass to clients forward Client Master Slave2 request Compute() service() ServiceImp1() request service Implements a service forward request Slave3 ServiceImp1()

  15. Singleton Intent ■ Ensure a class only has one instance, and provide a global point of access to it

  16. Singelton Pattern Motivation ■ It is important that some classes have only one instance – E.g., one printer spooler, one window manager ■ How to ensure that a class only has one instance?

  17. Singelton Pattern Motivation ■ Make the class itself responsible for keeping track of its sole instance ■ The class can ensure that no other instance can be created and provides a way to access the instance

  18. Singleton Pattern Structure Defines an instance operation that lets clients access its unique instance Singleton Static Instance() return instance Singleton getInstance() Operations

  19. Singleton example public class SimpleSingleton { private SimpleSingleton singleInstance = null; //Marking default constructor private //to avoid direct instantiation. private SimpleSingleton() { } //Get instance for class SimpleSingleton public static SimpleSingleton getInstance() { if(null == singleInstance) { singleInstance = new SimpleSingleton(); } return singleInstance; } } http://viralpatel.net/blogs/2009/01/java-singleton-design-pattern-tutorial-example-singleton-j2ee-design-pattern.html

  20. Schedule Group Group Group meeting meeting meeting Group Presentations Presentations Presentations meeting Reports Due

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