GoF design pattern

advertisement
GOF DESIGN PATTERNS
1.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.
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.
Using an Adapter
Related Patterns A resource adapter that hides an external system may also be
considered a Facade object as it wraps access to the subsystem or system with a
single object (which is the essence of Facade).
2. Singleton pattern:
It is a design pattern used to implement the mathematical concept of a
singleton, by restricting the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the
system. The concept is sometimes generalized to systems that operate more
efficiently when only one object exists, or that restrict the instantiation to a
certain number of objects.
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.
Since visibility to public classes is global in scope (in most languages), at any point
in the code, in any method of any class, one can write
SingletonClass.get-InstanceO
in order to obtain visibility to the singleton instance, and then send it a message,
such as
SingletonClass.getlnstanceO.doFooO
For example, Following Figure shows an implementation of the Singleton pattern.
Implementation of Singleton pattern in the ServicesFactory class
3.Factory pattern:
It is a design pattern used to implement the concept of factories. This is not a
GoF design pattern but a GoF abstract factory pattern. This is also called as
simple factory or concrete factory.
Separation of concerns
This is a design principle to modularize or separate distinct concerns into
different areas, so that each has a cohesive purpose. For example, 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. Fundamentally, it is an application of
the GRASP high cohesion principle.
Advantages of factory objects
• Separate the responsibility of complex creation into cohesive helper objects.
• Hide potentially complex creation logic.
• Allow introduction of performance-enhancing memory management strategies,
such as object caching or recycling.
The Factory pattern
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 forth?
Solution
Create a Pure Fabrication object called a Factory that handles the creation.
Related Patterns
Factories are often accessed with the Singleton pattern.
4.Observer pattern:
It is a software design pattern in which an object called the subject, maintains a
list of its dependents, called observers , and notifies them automatically of any
state changes usually by calling one of their methods. It is mainly used to
implement distributed event handling systems.
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.
Example
The SaleFrame1 object is the observer/subscriber/listener, it subscribes to
interest in property events of the Sale, which is a publisher of property events
The observer SaleFrame1 subscribes to the publisher Sale
Who is the observer, listener, subscriber, and publisher?
Observer Is Not Only for Connecting Uls and Model Objects
The most prevalent use of this pattern is for GUI widget event handling, in both
Java technologies (AWT and Swing) and in Microsoft's .NET. Each widget is a
publisher of GUI-related events, and other objects can subscribe to interest in
these.
Observer applied to alarm events, with different subscribers
Download