Download presentation
Presentation is loading. Please wait.
1
GoF Patterns Ch. 26
2
Adapter Pattern Problem: how to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces? Solution: Convert the original interface of a component into another interface, through an intermediate adapter object.
3
Adapter Next gen POS needs to interface with several third-party services: tax calculators, credit authorization services, inventory systems, and accounting. Each has a different API which cannot be changed. Add indirection: adapts to external, has consistent internal I/F - adapter
4
Using the Adapter Includes pattern in type name
Façade – wrap access to system or subsystem with a single object Adapter supports protected variations – changing ext interfaces, indirection, polymorphism
5
Most design patterns are seen as specialization of GRASP
Adapter: indirection and protected variation that uses polymorphism
6
Updating the domain model!
Discover noteworthy domain concepts after creating a new class
7
Factory Problem: Who should be responsible for creating objects when there are special considerations, such as complex creation logic, a desire to separate the creation responsibilities for better cohesion, etc? Solution: Create a Pure Fabrication object called Factory that handles the creation. Design to maintain separation of concerns, the more distinct the concerns, the more cohesive is the purpose! Factory object is designed to create objects separate responsibility into a cohesive helper object hide complex creation logic allow performance-enhancing memory management through caching or recycling
8
Factory Dynamically loads the class!!! – in this case from an external property Protected variations if adapter changes, without changing source code of factory we can create new adapters! Factory ~ Singleton
9
Singleton Problem: Exactly one instance of a class is allowed in a “singleton”. Objects need a global and single point of access. Solution: Define a static method of the class that returns the singleton. Who creates the factory and how is it accessed? Static is a form of visibility: we do not need to pass the object as a parameter to be able to access its methods
10
Singleton Class X defines a static method getInstance that itself provides an instance of X Lazy (preferred, less work!, check if null, then init) vs Eager initialization
11
Implicit getInstance SIngleton
Wy not make service methods static methods of the class itself? Static methods not polymorphic, do not permit overriding Related: factory, façade
12
Adapter, Factory, & Singleton
Building blocks To handle the problem of varying interfaces for external services, let’s use Adapters generated from a Singleton Factory
13
Strategy Problem: How to design for varying, but related, algorithms or policies? How to design for the ability to change algorithms or policies? Solution: Define each algorithm/policy/strategy in a separate class with a common interface.
14
Strategy Multiple sale pricing strategy classes, polymorphic getTotal method
15
Strategy Application
16
Creating Strategy with Factory
17
Creating Strategy with Factory
18
Creating a strategy
19
Composite Problem: How to treat a group or composition structure of bjects the same way (polymorphically) as a non-composite (atomic) object? Solution: Define classes for composite and atomic objects so that they implement the same interface.
20
Composite Outer composite object contains a list of inner objects and both the outer and inner objects implement the same interface. CompositeBestForCustomerPRicingStrategy inherits the attribute pricing Strategies
21
Composite
23
Creating Composite Strategy
24
Pricing strategy for customer discount
25
Pricing strategy for customer discount
Based on polymorphism and protected variations
26
Recap - GoF Adapter Factory Singleton Strategy Composite Still left:
Façade Observer
27
Facade Problem: A common, unified interface to disparate set of implementations or interface such as within a subsystem is required. There may be undesirable coupling to many things in the subsystem, or the implementation of the subsystem may change. What to do? Solution: Define a single point of contact to the subsystem, a façade object that wraps the subsystem. This façade object presents a single, unified interface and is responsible for collaborating with the subsystem components.
28
Façade Front end interface, hides subsystem behind an object
Singleton pattern Separation of concerns
29
Observer
30
Observer (Publish-Subscribe)
Problem: Different kinds of subscriber objects are interested in the state changes or events of a publisher object, and want to react in their own unique way when the publisher generates an event. Moreover, the publisher wants to maintain low coupling to the subscribers. What to do? Solution: Define a subscriber or listener interface. Subscribers implement this interface. The publisher can dynamically register subscribers who are interested in an event and notify them when an event occurs.
31
Observer
32
Observer
33
Practice with patterns
Design-Patterns-1&user_name=I _quiz.htm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.