design patterns sanjeeb kumar nanda 30-aug-2012. 2 what is a pattern? pattern is a recurring...

36
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012

Upload: denis-houston

Post on 05-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

DESIGN PATTERNSSanjeeb Kumar Nanda

30-Aug-2012

Page 2: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

2

What is a pattern?

• Pattern is a recurring solution to a standard problem

• Each Pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice

• Not code reuse

‒ Instead, solution/strategy reuse

‒ Sometimes, interface reuse

Page 3: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

3

How patterns arise

ProblemProblem

Context

SolutionSolution

Benefits

Related Patterns

Consequences

Forces

Page 4: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

4

Description of Design Pattern

• Pattern name and classification

• Intent

• Motivation

• Applicability

• Structure (diagram)

• Sample Code

• Known Uses

Page 5: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

5

Four essential elements of pattern

• Pattern Name

‒ Naming a pattern immediately increases our design vocabulary. It let's design at a higher level of abstraction.

• Problem

‒ It explains the problem and its context. It might describe specific design problems such as how to represent algorithms as objects.

• Solution

‒ The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations.

• Consequences

‒ The consequences are the results and trade-offs of applying the pattern

Page 6: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

6

Pattern Categories

Categories are based on granularity and level of abstraction

• Creational

‒ It helps in the process of object creation

‒ Factory Method, Abstract Factory, Singleton., etc.

• Structural

‒ Defines composition of objects and classes

‒ Uses inheritance to compose classes, while the Structural object patterns describe ways to assemble objects.

‒ Adapter, Façade, Proxy., etc.

• Behavioral

‒ Characterizes the way in which classes or objects interact

‒ The Behavioral class patterns use inheritance to describe algorithms and flow of control, whereas the Behavioral object patterns describe how a group of objects

co-operate to perform a task that no single object can carry out alone .

‒ Observer, Iterator, Visitor, etc.

Page 7: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

7

Design Patterns Catalog

Purpose

Creational Structural Behavioral

Scope Class Factory Method Adapter InterpreterTemplate Method

Object Abstract Factory Builder Prototype Singleton

Adapter BridgeComposite Decorator Facade Proxy

Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor

Page 8: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

8

Page 9: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

9

Singleton Pattern

• Intent

‒ Ensure a class only has one instance, and provide a global point of access to it.

• Frequency of use

‒ Medium high

• Applicability

‒ Use the Singleton pattern when

• There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.

• When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Page 10: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

10

Singleton - Structure

Page 11: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

11

Singleton - Participants

• Singleton

‒ Defines an Instance operation that lets clients access its unique instance. Instance is a class operation (that is, a class method in Smalltalk and a static member function in C++).

‒ May be responsible for creating its own unique instance.

Page 12: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

12

Singleton Pattern

• Related Patterns

‒ Many patterns can be implemented using the Singleton pattern like Abstract Factory, Builder and Prototype.

Real time examples:

• Logger Classes - The Singleton pattern is used in the design of logger classes. These classes are usually implemented as a singleton and provides a global logging access point in all the application components without being necessary to create an object each time a logging operation is performed.

• Load balancing - Singleton pattern is used in case of Load balancing the servers. Only a single instance of the class can be created because servers may dynamically come on- or off-line and every request must go through the one object that has knowledge about the state of the instance.

Page 13: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

13

Factory Method Pattern

• Intent‒ Define an interface for creating an object, but let subclasses

decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

• Also known as‒ Virtual Constructor

• Frequency of use: High

• Applicability‒ A class can't anticipate the class of objects it must create.

‒ A class wants its subclasses to specify the objects it creates.

‒ Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Page 14: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

14

Factory Method - Structure

• Structure

Page 15: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

15

Factory Method - Participants

• Product(Book)‒ defines the interface of objects the factory method creates.

• ConcreteProduct (ComicBook, NoteBook, FictionalBook)‒ implements the Product interface.

• Creator (BookFactory) ‒ declares the factory method, which returns an object of type Product.

Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.

‒ may call the factory method to create a Product object.

• ConcreteCreator (ComicBookFactory, NoteBookFactory, FictionalBookFactory) ‒ overrides the factory method to return an instance of a

ConcreteProduct.

Page 16: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

16

Factory Method – Related Patterns

• Related Patterns

‒ Abstract Factory

• Abstract Factory often implemented with factory methods

‒ Template

• Factory methods are often called usually within template methods

Real time Examples

• IEnumerable - The Framework Class Library uses factory method in IEnumerable

collection.

By specifying a GetEnumerator factory method in the IEnumerable interface and by having all collection classes implement this interface, the framework exposes one interface for obtaining an enumerator for a collection. IEnumerable (Abstract Creator) defines the interface by which an IEnumerator (Abstract Product) is returned to the client. A ConcreteCreator GetEnumerable can then be called to return a ConcreteProduct.

Page 17: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

17

Factory Method – Examples

• WebRequest- The Framework Class Library uses factory method in

System.Net.WebRequest.

WebRequest serves as a convenient base class for the .NET Framework's request/response model for accessing data from the Internet. This class encapsulates the details of connecting to the server, sending the request, and receiving the response. The static factory method WebRequest.Create creates protocol-specific descendants of WebRequest using the value of the URI passed in as argument. For example, when a URI beginning with "http://" is passed, an HttpWebRequest object is returned; when a URI beginning with "file://" is passed, a FileWebRequest object is returned. WebRequest.Create is a parameterized factory method -- it takes a parameter that identifies the type of object to create, and all objects created are of type WebRequest

Page 18: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

18

Factory Method – Examples

• HttpApplicationFactory - The entry point in the pipeline is the HttpRuntime object,

which creates an instance of HttpContext for the particular request. HttpRuntime does not try to determine the type of HttpApplication object that will handle the request. Instead, it calls the static factory method HttpApplicationFactory.GetApplicationInstance, passing it the HttpContext instance just created. HttpApplicationFactory is an undocumented class that contains the logic required to determine the HttpApplication object capable of handling an incoming request. The below figure shows the class diagram for instantiating an HttpApplication object capable of handling the request. HttpApplicationFactory.GetApplicationInstance is a parameterized factory method. It takes a parameter that identifies the type of product to create, and all products created are of type HttpApplication.

Page 19: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

19

Abstract Factory

• Intent

‒ Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

• Also Known As

‒ Kit

• Frequency of use

‒ High

• Applicability

‒ A system should be independent of how its products are created, composed, and represented.

‒ A system should be configured with one of multiple families of products.

‒ A family of related product objects is designed to be used together, and you need to enforce this constraint.

‒ You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Page 20: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

20

Abstract Factory - Structure

Page 21: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

21

Abstract Factory - Participants

• AbstractFactory (GUIFactory)

‒ declares an interface for operations that create abstract product objects.

• ConcreteFactory (WindowsGUIFactory, LinuxGUIFactory)

‒ implements the operations to create concrete product objects.

• AbstractProduct (Button, Textbox, Checkbox)

‒ declares an interface for a type of product object.

• ConcreteProduct (WindowButton, WindowTextbox, WindowCheckbox, LinuxButton, LinuxTextbox, LinuxCheckbox)

‒ defines a product object to be created by the corresponding concrete factory.

‒ implements the AbstractProduct interface.

• Client

‒ uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Page 22: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

22

Abstract Factory – Related Patterns

• Factory Method

‒ Abstract Factory classes are often implemented with factory methods

• Prototype

‒ Abstract Factory classes are often implemented with Prototype

• Singleton

‒ The concrete factory in an abstract factory is often a singleton.

Page 23: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

23

Abstract Factory

Real time example

ADO.NET defines new set of abstract products such as DbConnection, DbCommand, DbParameter etc. At the same time, each product has a concrete product class, like SqlConnection and OracleConnection etc. SqlCommand, OracleCommand etc respectively.

The abstract factory -DbProviderFactory- declares set of CreateConnection, CreateCommand, CreateParameter etc... Each of these methods return a specific abstract product, for example CreateConnection returns DbConnection, CreateCommand return DbCommand etc...

DbProviderFactory has set of concrete factory classes such as SqlClientFactory, OracleClientFactory etc...These concrete factories implements the Create methods to return specific products -provider specific classes.

Page 24: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

24

Prototype Pattern

• Intent

‒ Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

• Frequency of use: Medium

• Applicability

‒ Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and

‒ When the classes to instantiate are specified at run-time, for example, by dynamic loading; or

‒ To avoid building a class hierarchy of factories that parallels the class hierarchy of products; or

‒ When instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

Page 25: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

25

Prototype - Structure

Page 26: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

26

Prototype - Participants

• Prototype

‒ Declares an interface for cloning itself.

• ConcretePrototype

‒ Implements an operation for cloning itself.

• Client

‒ Creates a new object by asking a prototype to clone itself.

Page 27: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

27

Prototype – Related Patterns

• Related Patterns

‒ An Abstract Factory might store a set of prototypes from which to clone and return product objects.

‒ Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.

Page 28: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

28

Builder Pattern

• Intent

‒ Separate the construction of a complex object from its representation so that the same construction process can create different representations.

• Frequency of use: Medium - low

• Applicability

Use the Builder pattern when

‒ The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.

‒ The construction process must allow different representations for the object that's constructed.

Page 29: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

29

Builder Pattern - Structure

Page 30: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

30

Builder Pattern - Participants

Builder

‒ specifies an abstract interface for creating parts of a Product object.

ConcreteBuilder

‒ constructs and assembles parts of the product by implementing the Builder interface.

‒ defines and keeps track of the representation it creates.

‒ provides an interface for retrieving the product

Director

‒ constructs an object using the Builder interface.

Product

‒ represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled.

‒ includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.

Page 31: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

31

Builder Pattern – Related Patterns

• Abstract factory is similar to Builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory pattern is concerned, the product gets returned immediately.

• A composite is what the builder often builds.

Page 32: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

32

Design Principles

• Encapsulate what varies

• Favor composition over inheritance

• Program to an interface not to an implementation

• Strive for loosely coupled designs between objects that interact

• Classes should be open for extension but closed for modification

• Depend on abstractions. Do not depend on concrete classes.

• Only talk to your friends

• Don’t call us, we will call you

• A class should have only one reason to change

Page 33: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

33

Mindset on Patterns

Page 34: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

34

Design Pattern Questions

Page 35: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

35

Concluding remarks

• Design Patterns (GoF) provide a foundation for further understanding of:

‒ Object-Oriented design

‒ Software Architecture

• Understanding patterns can take some time

‒ Re-reading them over time helps

‒ As does applying them in your own designs

• Online URL

‒ www.dofactory.com

Page 36: DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug-2012. 2 What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem

36

Sanjeeb Kumar Nanda