CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
-
Upload
bryan-harrell -
Category
Documents
-
view
221 -
download
0
Transcript of CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
![Page 1: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/1.jpg)
CS212: Object Oriented Analysis and Design
Lecture 38: Design Pattern-II
![Page 2: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/2.jpg)
Recap of Lecture 37
• Design Pattern
• Pattern everywhere
• Pattern in computation
• Design pattern
• Iterator Patter
• Gang of Four Pattern
![Page 3: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/3.jpg)
Outline of Lecture 38
• GoF Pattern
• Abstract Factory
• Singleton
• Adapter
![Page 4: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/4.jpg)
Pros of Design Patterns
• Help capture and disseminate expert knowledge.• Promotes reuse and avoid mistakes.
• Provide a common vocabulary:• Help improve communication among the developers.
• Reduce the number of design iterations:• Help improve the design quality and designer productivity.
Mar-15 Software Engineering
![Page 5: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/5.jpg)
Pros of Design Patterns
• Patterns solve software structural problems attributable to:
• Abstraction,
• Encapsulation
• Information hiding
• Separation of concerns
• Coupling and cohesion
• Separation of interface and implementation
• Single point of reference
• Divide and conquerMar-15 Software Engineering
![Page 6: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/6.jpg)
Cons of Design Patterns
• Design patterns do not directly lead to code reuse.
• To help select the right design pattern at the right point during a design exercise
Mar-15 Software Engineering
![Page 7: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/7.jpg)
Mar-15
Catalogue of Design Patterns (GoF)
Purpose
Creational Structural Behavioral Class Factory Method Adapter Interpreter
Template Method
Scop
e
Object Abstract Factory Builder Prototype Singleton
Adapter Bridge Composite Decorator Facade Proxy
Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor
Software Engineering
![Page 8: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/8.jpg)
Abstract Factory: Intent
• Provide an interface for creating families of objects
• A hierarchy that encapsulates
• Construction of a suite of "products"
• The new operator considered harmful
![Page 9: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/9.jpg)
Abstract Factory: Problem
• If an application is to be portable, it needs to encapsulate platform dependencies
• platform : windowing system, OS, database, etc.
• Lots of #ifdef case statements
![Page 10: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/10.jpg)
Abstract Factory: Discussion
• Abstracts the creation of families of related or dependent objects
• Without directly specifying their concrete classes
• The "factory" object has the responsibility for providing creation services for the entire platform family
• Clients never create platform objects directly, they ask the factory to do that for them.
• It is routinely implemented as a Singleton
![Page 11: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/11.jpg)
Abstract Factory: Structure
![Page 12: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/12.jpg)
Abstract Factory: Example
![Page 13: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/13.jpg)
Abstract Factory: Check List
• Map out a matrix of "platforms" versus "products".
• Define a factory interface that consists of a factory method per product.
• Define a factory derived class for each platform that encapsulates all references to the new operator.
• The client should retire all references to new, and use the factory methods to create the product objects.
Demonstration: abstractFactory.cpp
![Page 14: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/14.jpg)
Singleton: Intent
• Ensure a class has only one instance
• Provide a global point of access to it.
• Encapsulated
• "just-in-time initialization“
• "initialization on first use".
![Page 15: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/15.jpg)
Singleton: Problem
• Application needs one, and only one, instance of an object.
• Lazy initialization and global access are necessary.
![Page 16: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/16.jpg)
Singleton: Discussion
• Make the class of the single instance object responsible for creation, initialization, access, and enforcement.
• Declare the instance as a private static data member.
• Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
![Page 17: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/17.jpg)
Singleton: Structure
Make the class of the single instance responsible for access and"initialization on first use".
• The single instance is a private static attribute.
• The accessor function is a public static method.
![Page 18: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/18.jpg)
Singleton: Example
![Page 19: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/19.jpg)
Singleton: Checklist
• Define a private static attribute in the "single instance" class.
• Define a public static accessor function in the class.
• Define all constructors to be protected or private.
• Clients may only use the accessor function to manipulate the Singleton.
Demonstration: singleton.cpp
![Page 20: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/20.jpg)
Mar-15
Catalogue of Design Patterns (GoF)
Purpose
Creational Structural Behavioral Class Factory Method Adapter Interpreter
Template Method
Scop
e
Object Abstract Factory Builder Prototype Singleton
Adapter Bridge Composite Decorator Facade Proxy
Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor
Software Engineering
![Page 21: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/21.jpg)
Adapter: Intent
• Convert the interface of a class into another interface clients expect.
• Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
• Wrap an existing class with a new interface.
• Impedance match an old component to a new system
![Page 22: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/22.jpg)
Adapter: Problem
• An "off the shelf" component
• Offers compelling functionality that you would like to reuse
![Page 23: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/23.jpg)
Adapter: Discussion
• Reuse has always been painful and elusive
• Something not quite right between the old and the new
• Creating an intermediary abstraction that translates
• Adapter functions as a wrapper or modifier of an existing class.
• It provides a different or translated view of that class.
![Page 24: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/24.jpg)
Adapter: Structure
![Page 25: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/25.jpg)
Adapter: Structure
![Page 26: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/26.jpg)
Adapter: Example
• Allows otherwise incompatible classes to work together
• Converting the interface of one class into an interface expected by the clients.
![Page 27: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/27.jpg)
Adapter: Check list
Identify the players: the client, the adaptee
Identify the interface that the client requires
Design a "wrapper" class that can "impedance match“
The adapter/ wrapper class "has a" instance of the adaptee class
The adapter/wrapper class "maps" the client interface to the adaptee interface.
The client uses (is coupled to) the new interface
![Page 28: CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.](https://reader035.fdocuments.net/reader035/viewer/2022062322/5697bfd81a28abf838caf2ef/html5/thumbnails/28.jpg)
Thank youNext Lecture: Design Pattern