Gof design patterns
-
Upload
shrikant-vaka -
Category
Documents
-
view
3.525 -
download
1
Transcript of Gof design patterns
GoF Design patterns
- SRIKANTH VAKA
ACTIVATE
Agenda
Introduction to GoF Design patterns Why
What
Where
Types of problems in OOPS
OOD principles
UML notations
Agenda.,
Creational Factory
Abstract Factory
Singleton
Object Pool
Structural Adapter
Proxy
Façade
Behavioral Iterator
Observer
Strategy
Introduction
Each pattern describes a problem which occurs over and over again in software programming environment and then describes the core of solution to that problem.
Introduction
Recipes for the common OO problems.
Code reuse
Open for extension but not for modification
Encapsulate what varies
Single responsibility principle
Program against interface but not against implementation
Avoid Tight coupling
OOD principles
OOD principles to design a class: SRP: The single responsibility principle
A class should have only one and only one reason to change.
OCP – The open closed principle Should be able to extend the behavior but not modify the existing behavior.
LSP – The Liskov Substitution principle Derived classes must be substantial to their base classes.
DIP – The dependency inversion principle Depend on abstractions, not on concretions.
ISP – The interface Segregation principle Make fine grained interfaces that are specific to client.
Uses of Design Patterns
Finding appropriate objects.
Determining object granularity.
Specifying object interfaces.
Specifying object implementations.
Programming to an interface not to an implementation.
Question
What is difference or use of below code snippets:
Interface ICreditCard{…}
Class HDFCCreditCard : ICreditCard {….}
ICreditCard _cc = new HDFCCreditCard();
HDFCCreditCard cc = new HDFCCreditCard();
When to go for Interfaces
APIs
Tightly coupled code
Future proofing
Code clarity
UML Introduction
Class representation:
UML Keywords
Encapsulation
Association
Aggregation
Composition
Abstraction
Generalization
Association Vs. Aggregation Vs. Composition
Association
Composition Association is a relationship between two classes.
Aggregation is the relationship between two classes.
Composition is the special type of Aggregation/Association.
UML Introduction
Scope of Pattern
Object level• Deal with object relations.• Relationships are dynamic.
Class level• Deal with relationships between
classes and their subclasses.• Relationships are static.
Types of Design Patterns
•Which can be used while creating objects.•Factory, Abstract Factory, Singleton, Prototype, Builder
Creational
•Which can be used to combine objects and classes in order to build structured objects.•Adapter, Façade, Proxy, Composite, etc.
Structural
•Which can be used to build a computation and to control data flows.•Iterator, Observer, Strategy, State, etc.
Behavioral
Creational patterns
Creational
Factory
Abstract Factory
Singleton
Factory
Defines an interface for creating objects but let sub-classes decide which of those instantiate.
Enables the creator to defer Product creation to a sub-class.
Factory diagram
Factory Example
Kiosk
Factory Pros & Cons.
Pros:
Shields clients from concrete classes.
If a framework follows a Factory pattern, it enables the third party developers to plug-in new products.
Cons:
Coding a new product means, writing two classes one for concrete factory and concrete product.
Inheritance based.
ABSTRACT FACTORY
Create instances of classes belonging to different families.
Factory method often used in abstract factory pattern to implement the create methods.
Abstract factory UML
Abstract Factory Facts
Use when a system should be independent of how its products are created, composed and represented.
When a system should be configured with one of multiple families of products.
When a family of objects is designed to be worked together, and you need to enforce this constraint.
If you want to provide a class library of products, and you want to reveal just their interfaces but not implementations.
Example
Check
Deposit
Book order
Payment
Pros./cons.
Pros:
Shields clients from concrete classes.
Easy to switch product family at run-time, just change concrete factory.
Cons:
Adding a new product means changing factory interface and all concrete factories.
Factory Vs. Abstract Factory
Factory : Use the Factory Method pattern when there is a need to decouple a
client from a particular product that it uses.
Use the Factory Method to relieve a client of responsibility for creating and configuring instances of a product.
Abstract Factory: Use the Abstract Factory pattern when clients must be decoupled
from product classes.
Especially useful for program configuration and modification The Abstract Factory pattern can also enforce constraints about which classes must be used with others. It may be a lot of work to make new concrete factories.
Singleton
A class with only one single possible instance. Private constructor
Global access
Singleton UML
Example/code
Run-time configuration manager
Private static ClassConfigurator GetInstance()
{
If(instance==null) return new ClassConfigurator();
Else return instance;
}
More about Singleton
Thread-safe implementation for multi-threading use. Lazy instantiation
Early instantiation
Object Pool Pattern
Caching the instances that are costly to create every time.
Also called Object Cache or Resource Cache design pattern.
It is advised to keep all reusable expensive objects that are not currently in use in the container so that they can be managed by one rational policy. To achieve this, the reusable Pool class is designed to be a singleton class.
Object Pool UML
Examples:
Database connection pools.
End of session 1
Structural Patterns
Adapter•Translates one interface for a class into a compatible interface.
Proxy•Provides a class which limits the access to the original class.
Facade•A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
Adapter Pattern
Translates one interface for a class into a compatible interface.
An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface.
Adapter UML
Adapter Example – Data translator
Proxy pattern
Provides a class which limits the access to the original class.
It might be for Security reasons
To reduce memory foot print
To avoid complex object usage
Proxy UML Diag.
Proxy Example
Admin @Bank
Proxy
ATM in field
Proxy Facts
Introduces a level of indirection when accessing an object.
Can hide the fact that the object resides in different address space.
Can perform optimizations such as creating an object on demand.
Facade Pattern
A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
Make a software library easier to use, understand and test, since the facade has convenient methods for common tasks.
Make the library more readable.
Reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system.
Wraps a poorly designed collection of APIs with a single well-designed API.
Façade UML
Façade Example
End of Session 2
Behavioral Patterns
Iterator• An iterator is used to traverse through the container and access the
container’s elements.
Observer• The observer pattern 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.
Strategy• The strategy pattern (also known as the policy pattern) is a particular
software design pattern, whereby algorithms can be selected at runtime.
Iterator pattern
An iterator is used to traverse through the container and access the container’s elements.
The essence of the Iterator Factory method Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation."
Iterator UML
More about Iterator
Robust iterators
Who defines the traversal algorithms?
Observer Pattern
Motivation
Intent
Definition - The observer pattern 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.
Observer UML
More about Observer pattern..
Many Subjects to Many Observers
Who triggers the update
Making sure Subject state is self-consistent before notification
Observer Example
Strategy Pattern
Motivation
Intent
Strategy Vs. Creation Pattern
Strategy UML
Strategy Example
More about Strategy..
Passing Data to and from Strategy object.
The strategy design pattern splits the behaviour (there are many behaviours) of a class from the class itself.
This has some advantages, but the main draw back is that a client must understand how the Strategies differ.
Since clients get exposed to implementation issues the strategy design pattern should be used only when the variation in behaviour is relevant to them.
Questions
Thanks
Thanks for listening, if you have any questions mail me at [email protected].
MSN Id: [email protected]
Feedback: https://www.recognition4me.com/recognition4me