Download presentation
Presentation is loading. Please wait.
1
GoF Patterns (GoF) popo
2
Gang of Four (GoF) is a set of 23 object design patterns
GoF Patterns (GoF) Gang of Four (GoF) is a set of 23 object design patterns Authored by four people, Gamma, Helm, Johnson and Vlissides, are therefore called the Gang of Four (GoF) design patterns. Some of the important GoF patterns are. popo
3
GoF Patterns (GoF) Adapter (GoF) Factory (GoF) Singleton (GoF)
Strategy (GoF) Facade (GoF) popo
4
GoF Patterns (GoF) Adapter (GoF) Pattern Name: Adapter
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. popo
5
Each has a different API, which can't be changed.
GoF Patterns (GoF) Adapter (GoF) Example The NextGen POS system needs to support several kinds of external third-party services, including tax calculators, credit authorization services, inventory systems, and accounting systems, among others. Each has a different API, which can't be changed. popo
6
GoF Patterns (GoF) Adapter (GoF)
A solution is to add a level of indirection with objects that adapt the varying external interfaces to a consistent interface used within the application popo
7
The adapter pattern can be related to some GRASP patterns.
GoF Patterns (GoF) Adapter (GoF) The adapter pattern can be related to some GRASP patterns. Adapter supports Protected Variations with respect to changing external interfaces or third-party packages through the use of an Indirection object that applies interfaces and Polymorphism. popo
8
GoF Patterns (GoF) Factory (GoF)
This is also called Simple Factory or Concrete Factory. The adapter raises a new problem in the design: In the Adapter pattern solution for external services with varying interfaces, who creates the adapters? And how to determine which class of adapter to create, such as TaxMaster-Adapter or GoodAsGoldTaxProAdapter popo
9
GoF Patterns (GoF) Factory (GoF) If any domain object creates them,
The responsibilities of the domain object are going beyond pure application logic. This will leads to: Design to maintain extra. Here, the domain layer of software objects emphasizes relatively pure application logic responsibilities, Whereas a different group of objects is responsible for the concern of connectivity to external systems. popo
10
GoF Patterns (GoF) Factory (GoF)
A common alternative in this case is to apply the Factory pattern, In which a Pure Fabrication "factory" object is defined to create objects. Factory objects have several advantages: Separate the responsibility of complex creation into helper objects. Hide potentially complex creation logic. Allow introduction of performance-enhancing memory management strategies, such as object caching or recycling popo
11
GoF Patterns (GoF) Factory (GoF) Pattern Name: 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, and so on? Solution: Create a Pure Fabrication object called a Factory that handles the creation. popo
12
Then, How to get visibility to this single Services instance?
GoF Patterns (GoF) Singleton (GoF) The Factory pattern raises another new problem in the design: Who creates, and how is it accessed? The process and the methods that may need to be called from various places in the code, as different places need access to the adapters for calling on the external services. Then, How to get visibility to this single Services instance? popo
13
This is possible but inconvenient;
GoF Patterns (GoF) Singleton (GoF) One solution is pass the instance as a parameter to wherever a visibility need This is possible but inconvenient; An alternative is the Singleton pattern. It support global visibility or a single access point to a single instance of the class rather than some other form of visibility popo
14
Pattern Name: Singleton Problem:
GoF Patterns (GoF) Singleton (GoF) Pattern Name: Singleton Problem: Exactly one instance of a class is allowed. It is a "singleton" Objects need a global and single point of access. Solution: Define a static method of the class that returns the singleton. popo
15
GoF Patterns (GoF) Singleton (GoF)
The key idea is that class X defines a static method that itself provides a single instance of X. With this approach, a developer has global visibility to this single instance popo
16
GoF Patterns (GoF) Singleton (GoF) Example
public class ServicesFactory { // eager initialization static ServicesFactory instance = new ServicesFactory( ) ; public static ServicesFactory getlnstance() { return instance; } // other methods... popo
17
GoF Patterns (GoF) Singleton (GoF) popo
18
GoF Patterns (GoF) Strategy (GoF)
The next design problem to resolved more complex pricing logic, such as a discount for the day, senior citizen discounts, and so on. The pricing strategy for a sale can vary. During some time it may be 10% off all sales, later it may be Rs10/- off if the sale total is greater than Rs200/-, and may be other variations. How do we design for these varying pricing algorithms? popo
19
Pattern Name: Strategy
GoF Patterns (GoF) Strategy (GoF) Pattern Name: Strategy Problem: How to design for varying, but related, algorithms or policies? How to design for the ability to change these algorithms or policies? Solution: Define each algorithm/policy/strategy in a separate class, with a common interface. popo
20
GoF Patterns (GoF) Strategy (GoF)
Since the behavior of pricing varies by the strategy , we create multiple SalePricingStrategy classes, each with a polymorphic getTotal method Each getTotal method takes the Sale object as a parameter, so that the pricing strategy object can find the pre-discount price from the Sale, and then apply the discounting rule. The implementation of each getTotal method will be different. popo
21
GoF Patterns (GoF) Strategy (GoF) popo
22
GoF Patterns (GoF) popo Facade (GoF)
A facade is an object that provides a simplified interface to a larger body of code, such as a class library A facade can: make a software library easier to use, understand and test, since the facade has methods for common tasks. make the library more readable, reduce dependencies of outside code on the inner workings of a library, since most code are more flexibility in developing the system; wrap a poorly designed collection of APIs with a single well-designed API popo
23
GoF Patterns (GoF) Facade (GoF) popo
24
GoF Patterns (GoF) popo Facade (GoF) Pattern Name: Facade
Problem: A common, interfaces 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 facade object that wraps the subsystem. This facade object presents a single unified interface and is responsible for collaborating with the subsystem components. popo
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.