DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a...

39
DESIGN PATTERNS COMMONLY USED PATTERNS

Transcript of DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a...

Page 1: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

DESIGN PATTERNS

COMMONLY USED PATTERNS

Page 2: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

What is a design pattern ?

Defining certain rules to tackle a particular kind of problem in software development environment.

A pattern addresses a recurring design problem that arises in a specific design

situation and presents a solution to it.

Page 3: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Types Of Design Patterns

The Gang Of Four Design PatternsThe EJB Design PatternsThe Sun J2EE Design patternsThe Application Design patterns

Page 4: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Elements of a Design Pattern

NameProblemSolutionConsequences

Page 5: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Classification

Creational Patterns – are ones that create objects for you,rather than having you instantiate objects directly

Structural Patterns – help you compose groups of objects into larger structures.

Behavioral Patterns – help you define the communication between objects in your system and how the flow is controlled in a complex program.

Page 6: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

GoF Design Patterns

Page 7: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Key Rules

1. Program to an interface and not to an implementation

2. Favor Object Composition over Inheritance.

Page 8: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Creational Patterns

FactoryAbstract FactoryBuilderPrototypeSingleton

Page 9: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Factory

Returns an instance of one of several possible classes depending on the data provided to it.

Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of

data.

Page 10: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Abstract Factory

Abstraction over the Factory Design Pattern

Abstract factory is the factory that returns one of several factories.

One of the example to this could be a look-

and-feel in Java.

Page 11: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 12: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Pros & Cons

Pros Cons1. Shields clients from

concrete classes.2. Easy to switch product

family at runtime – just change concrete factory

3. “keep it in the family” – enforces product family grouping

1. Adding a new product means changing factory interface + all concrete factories

Page 13: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Builder

Separates the construction process of a complex object from its representation.

Used when the construction process is same but the representations may differ.

Page 14: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 15: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Example -- UMLRTFReader

ParseRTF()

while(t=get the next token){

switch t.Type{

CHAR: builder->ConvertCharacter(t.Char)FONT: builder->ConventFontCharnge(t.Font)PARA: Builder->ConventParagraph() }}

TextConverter

ConvertCharacter(char)

ConvertFontChange(Font)

ConvertParagraph()

ASCIIConverter

ConvertCharacter(char)

GetASCIIText()

TextConverter

ConvertCharacter(char)

ConvertFontChange(Font)

ConvertParagraph()

GetTeXText()

TextWidgestConverter

ConvertCharacter(char)

ConvertFontChange(Font)

ConvertParagraph()

GetTextWidget()

ASCIIText TeXText TextWidget

builders

Page 16: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

How is it different from Abstract Factory ?

Builder Abstract FactoryIt focuses on constructing a complex object step-by-step

Returns a complex object as a final result

AF emphasizes on creating a family of related objects

Product gets returned immediately.

Page 17: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Prototype

Used when creation of an object is time consuming or very complex.In java you can use this by implementing Cloneable interface.

Pros. Cons.

1. Shields clients from concrete classes

2. The object is the factory - i.e. Product and Creator combined (saves coding a Creator for every Product)

3. Pre-configured object instances – instead of create/set member every time.

1. Requires Memory to hold prototype.

2. Clone() is hard to Implement.3. Many prototypes must be

passed.

Page 18: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Singleton

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

Page 19: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Structural Patterns

Describes how classes and objects can be combined to make larger structures.

Class Patterns – Inheritance

Object Patterns – Composition

Page 20: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Structural Patterns

AdapterBridgeCompositeFaçadeProxyFlyweightDecorator

Page 21: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Adaptor Pattern

Converting an interface into another interface that its client is expected to see.

Two flavor of this pattern come as: Class Adaptor Object Adaptor

Java Adaptors

Page 22: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Class Adaptor

Page 23: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Object Adaptor

Page 24: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Bridge Pattern

Separates Interface from its implementation so that both can be changed independently.

Page 25: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 26: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Composite Pattern

Allows clients to treat both single components and collection of components identically.

Represents recursive data structures.

Compose objects into tree structures to represent part-whole hierarchies.

Page 27: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 28: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Façade Pattern

Defines a high level interface that makes a sub system easier to use.

Does not prevent an advanced user to use low level functionality.

J2EE – Session Façade & Message Façade

Page 29: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 30: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Proxy

Provide a surrogate or placeholder for another object to control access to it.

Provides identical interface as the original object has.

Controls access to the original object and may be responsible for creating & destroying it

Page 31: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 32: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Decorator

Adding responsibilities to objects dynamically and without having to create a new class.

Also known as “Wrapper”

Used when sub-classing is impractical.

Java I/O Streams is a good example.

Page 33: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 34: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Flyweight

Use sharing to support large numbers of fine-grained objects efficiently.

Reusability of existing objects.

Flyweight objects have two states intrinsic state & extrinsic state, that make them reusable.

Page 35: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

Page 36: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Examplepublic class StringTest {

   public static void main(String[] args) {      String fly = "fly", weight = "weight";      String fly2 = "fly", weight2 = "weight";

      System.out.println(fly == fly2);       System.out.println(weight == weight2);

      String distinctString = fly + weight;      System.out.println(distinctString == "flyweight");

      String flyweight = (fly + weight).intern();      System.out.println(flyweight == distinctString );    }}

 // TRUE // TRUE

 // FALSE

 // TRUE

Page 37: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Strategy Pattern

an object and its behavior are separated and put into two different classes.

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Page 38: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

UML Diagram

                                                

                 

Page 39: DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Thank You !