Gof design patterns

60
GoF Design patterns SRIKANTH VAKA ACTIVATE

Transcript of Gof design patterns

Page 1: Gof design patterns

GoF Design patterns

- SRIKANTH VAKA

ACTIVATE

Page 2: Gof design patterns

Agenda

Introduction to GoF Design patterns Why

What

Where

Types of problems in OOPS

OOD principles

UML notations

Page 3: Gof design patterns

Agenda.,

Creational Factory

Abstract Factory

Singleton

Object Pool

Structural Adapter

Proxy

Façade

Behavioral Iterator

Observer

Strategy

Page 4: Gof design patterns

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.

Page 5: Gof design patterns

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

Page 6: Gof design patterns

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.

Page 7: Gof design patterns

Uses of Design Patterns

Finding appropriate objects.

Determining object granularity.

Specifying object interfaces.

Specifying object implementations.

Programming to an interface not to an implementation.

Page 8: Gof design patterns

Question

What is difference or use of below code snippets:

Interface ICreditCard{…}

Class HDFCCreditCard : ICreditCard {….}

ICreditCard _cc = new HDFCCreditCard();

HDFCCreditCard cc = new HDFCCreditCard();

Page 9: Gof design patterns

When to go for Interfaces

APIs

Tightly coupled code

Future proofing

Code clarity

Page 10: Gof design patterns

UML Introduction

Class representation:

Page 11: Gof design patterns

UML Keywords

Encapsulation

Association

Aggregation

Composition

Abstraction

Generalization

Page 12: Gof design patterns

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.

Page 13: Gof design patterns

UML Introduction

Page 14: Gof design patterns

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.

Page 15: Gof design patterns

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

Page 16: Gof design patterns

Creational patterns

Creational

Factory

Abstract Factory

Singleton

Page 17: Gof design patterns

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.

Page 18: Gof design patterns

Factory diagram

Page 19: Gof design patterns

Factory Example

Kiosk

Page 20: Gof design patterns

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.

Page 21: Gof design patterns

ABSTRACT FACTORY

Create instances of classes belonging to different families.

Factory method often used in abstract factory pattern to implement the create methods.

Page 22: Gof design patterns

Abstract factory UML

Page 23: Gof design patterns

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.

Page 24: Gof design patterns

Example

Check

Deposit

Book order

Payment

Page 25: Gof design patterns

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.

Page 26: Gof design patterns

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.

Page 27: Gof design patterns

Singleton

A class with only one single possible instance. Private constructor

Global access

Page 28: Gof design patterns

Singleton UML

Page 29: Gof design patterns

Example/code

Run-time configuration manager

Private static ClassConfigurator GetInstance()

{

If(instance==null) return new ClassConfigurator();

Else return instance;

}

Page 30: Gof design patterns

More about Singleton

Thread-safe implementation for multi-threading use. Lazy instantiation

Early instantiation

Page 31: Gof design patterns

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.

Page 32: Gof design patterns

Object Pool UML

Page 33: Gof design patterns

Examples:

Database connection pools.

Page 34: Gof design patterns

End of session 1

Page 35: Gof design patterns

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.

Page 36: Gof design patterns

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.

Page 37: Gof design patterns

Adapter UML

Page 38: Gof design patterns

Adapter Example – Data translator

Page 39: Gof design patterns

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

Page 40: Gof design patterns

Proxy UML Diag.

Page 41: Gof design patterns

Proxy Example

Admin @Bank

Proxy

ATM in field

Page 42: Gof design patterns

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.

Page 43: Gof design patterns

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.

Page 44: Gof design patterns

Façade UML

Page 45: Gof design patterns

Façade Example

Page 46: Gof design patterns

End of Session 2

Page 47: Gof design patterns

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.

Page 48: Gof design patterns

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."

Page 49: Gof design patterns

Iterator UML

Page 50: Gof design patterns

More about Iterator

Robust iterators

Who defines the traversal algorithms?

Page 51: Gof design patterns

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.

Page 52: Gof design patterns

Observer UML

Page 53: Gof design patterns

More about Observer pattern..

Many Subjects to Many Observers

Who triggers the update

Making sure Subject state is self-consistent before notification

Page 54: Gof design patterns

Observer Example

Page 55: Gof design patterns

Strategy Pattern

Motivation

Intent

Strategy Vs. Creation Pattern

Page 56: Gof design patterns

Strategy UML

Page 57: Gof design patterns

Strategy Example

Page 58: Gof design patterns

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.

Page 59: Gof design patterns

Questions

Page 60: Gof design patterns

Thanks

Thanks for listening, if you have any questions mail me at [email protected].

MSN Id: [email protected]

Feedback: https://www.recognition4me.com/recognition4me