Design Patterns (DP) By P. S. Suryateja Asst.

Published  . 0 views
↓ Download
Design Patterns (DP) By P. S. Suryateja Asst.
1 / 1
Design Patterns (DP) By P. S. Suryateja Asst. - slide 1 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 2 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 3 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 4 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 5 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 6 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 7 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 8 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 9 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 10 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 11 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 12 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 13 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 14 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 15 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 16 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 17 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 18 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 19 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 20 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 21 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 22 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 23 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 24 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 25 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 26 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 27 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 28 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 29 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 30 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 31 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 32 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 33 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 34 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 35 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 36 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 37 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 38 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 39 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 40 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 41 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 42 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 43 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 44 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 45 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 46 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 47 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 48 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 49 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 50 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 51 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 52 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 53 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 54 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 55 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 56 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 57 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 58 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 59 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 60 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 61 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 62 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 63 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 64 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 65 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 66 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 67 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 68 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 69 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 70 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 71 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 72 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 73 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 74 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 75 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 76 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 77 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 78 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 79 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 80 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 81 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 82 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 83 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 84 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 85 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 86 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 87 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 88 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 89 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 90 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 91 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 92 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 93 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 94 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 95 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 96 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 97 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 98 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 99 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 100 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 101 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 102 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 103 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 104 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 105 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 106 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 107 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 108 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 109 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 110 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 111 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 112 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 113 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 114 of 115 Design Patterns (DP) By P. S. Suryateja Asst. - slide 115 of 115
Description: Design Patterns (DP) By P. S. Suryateja Asst. Professor CSE Dept Vishnu Institute of Technology Erich Gamma Richard Helm Ralph Johnson John Vlissides Gang of Four (GOF) Objectives Know what are design patterns? Know about GOF patterns. How

Related Topics

Download Presentation

"Design Patterns (DP) By P. S. Suryateja Asst." is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. Design Patterns (DP) By P. S. Suryateja
Asst. Professor
CSE Dept
Vishnu Institute of Technology Erich
Gamma Richard
Helm Ralph
Johnson John
Vlissides Gang of Four (GOF)<br>
slide2. Objectives Know what are design patterns?
Know about GOF patterns.
How to select and use a pattern?
How to apply a pattern?<br>
slide3. Outline Introduction to design patterns
What is a design pattern?
Need of design patterns
Use of design patterns
Design patterns in Smalltalk MVC
Describing design patterns
Catalog of design patterns
Organizing the catalog
How to select a design pattern?
How to use a design pattern?<br>
slide4. Outline (cont…) Creational patterns
Introduction
Singleton
Abstract Factory
Factory Method
Builder
Prototype<br>
slide5. Outline (cont…) Structural Patterns<br>
slide6. Introduction to Design Patterns<br>
slide7. What is a design pattern (DP)? “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.” - Christopher Alexander Design patterns are repeatable / reusable solutions to commonly occurring problems in a certain context in software design.<br>
slide8. What is a design pattern (DP)? (cont…) Pattern name

Problem

Solution

Consequences Four essential elements of a pattern:<br>
slide9. Need for design patterns Designing reusable object-oriented software (API’s) is hard.

Experienced designers Vs novice designers.

Patterns make object-oriented software flexible, elegant and reusable.

Solved a problem previously but don’t remember when or how?<br>
slide10. Use of design patterns Make it easier to reuse successful designs and architectures.

Make it easy for the new developers to design software.

Allows to choose different design alternatives to make the software reusable.

Helps in documenting and maintaining the software. To get a design “right”, faster.<br>
slide11. Why should we use DP’s? These are already tested and
proven solutions used by many
experienced designers.<br>
slide12. MVC Architecture<br>
slide13. Design Patterns in Smalltalk MVC<br>
slide14. Describing design patterns Pattern name and classification
Intent
Also known as
Motivation
Applicability
Structure
Participants
Collaborations<br>
slide15. Describing design patterns (cont…) Consequences
Implementation
Sample code
Known uses
Related patterns<br>
slide16. Catalog of design patterns Abstract Factory
Adapter
Bridge
Builder
Chain of Responsibility
Command
Composite
Decorator
Façade
Factory Method<br>
slide17. Catalog of design patterns (cont…) Flyweight
Interpreter
Iterator
Mediator
Memento
Observer
Prototype
Proxy
Singleton
State<br>
slide18. Catalog of design patterns (cont…) Strategy
Template Method
Visitor<br>
slide19. Abstract Factory<br>
slide20. Adapter<br>
slide21. Bridge<br>
slide22. Builder<br>
slide23. Chain of Responsibility<br>
slide24. Command<br>
slide25. Composite<br>
slide26. Decorator<br>
slide27. Façade<br>
slide28. Factory Method<br>
slide29. Flyweight<br>
slide30. Interpreter<br>
slide31. Iterator<br>
slide32. Mediator<br>
slide33. Memento<br>
slide34. Observer<br>
slide35. Prototype<br>
slide36. Proxy<br>
slide37. Singleton<br>
slide38. State<br>
slide39. Strategy<br>
slide40. Template Method<br>
slide41. Visitor<br>
slide42. Organizing the catalog<br>
slide43. How to select a design pattern Consider how design patterns solve design problems

Scan intent sections

Study how patterns interrelate

Study patterns of like purpose

Examine a cause of redesign

Consider what should be variable in your design<br>
slide44. Relationships
between patterns<br>
slide45. How to use a design pattern Read the pattern once through for a overview.
Study the structure, participants and collaborations sections.
Look at the sample code section to see a concrete example of the pattern in action.
Choose names for pattern participants that are meaningful in the application context.
Define the classes.
Define application-specific names for the operations in the pattern.
Implement the operations to carry out the responsibilities and collaborations in the pattern.<br>
slide46. Creational Patterns<br>
slide47. Introduction Deals with how objects are created.

Increase the system’s flexibility in terms of what, who, how and when the object is created.

Further classified into two types:
Class-creational patterns
Object-creational patterns

Five creational patterns:
Abstract Factory
Builder
Factory Method
Prototype
Singleton<br>
slide48. Singleton Pattern To create exactly one object of a class and to provide a global point of access to it. Structure<br>
slide49. Singleton Pattern (cont…) Non-Software Example<br>
slide50. Singleton Pattern (cont…) Motivation:
Need to create exactly one user interface object.
Need to create only one print spooler.<br>
slide51. Singleton Pattern (cont…) Applicability:
Singleton Pattern can be used when:
There must be exactly one instance of a class and to provide a global point of access to it.

2)When the sole instance should be extensible by sub classing, and clients should be able to use the extended instance without modifying its code.<br>
slide52. Singleton Pattern (cont…) Participants:
Consists of a “Singleton” class with the following members:
A static data member
A private constructor
A static public method that returns a reference to the static data member.<br>
slide53. Singleton Pattern (cont…) Collaborations:
Clients access the singleton instance solely through the singleton’s class operation.<br>
slide54. Singleton Pattern (cont…) Consequences:
Controlled access to sole instance.
Reduced name space.
Permits refinement of operations and representation.
Permits a variable number of instances.<br>
slide55. Singleton Pattern (cont…) Implementation: class Singleton
{
private static Singleton instance;
private Singleton()
{
...
}
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
...
public void doOperation()
{
...
}
}<br>
slide56. Singleton Pattern (cont…) Sample code: Design a small ATM printing application which can generate multiple types of statements of the transaction including Mini Statement, Detailed statement etc. However the customer should be aware of the creation of these statements. Ensure that the memory consumption is minimized.<br>
slide57. Singleton Pattern (cont…) Real World Example<br>
slide58. Singleton Pattern (cont…) Code for StatementFactory class is as shown below: public class StatementFactory extends Factory
{
private static StatementFactory uniqueInstance;

private StatementFactory() {}

public static StatementFactory getUniqueInstance()
{
if (uniqueInstance == null)
{
uniqueInstance = new StatementFactory();
System.out.println("Creating a new StatementFactory instance");
}
return uniqueInstance;
}<br>
slide59. Singleton Pattern (cont…) public StatementType createStatements(String selection)
{
if (selection.equalsIgnoreCase("detailedStmt"))
{
return new DetailedStatement();
}
else if (selection.equalsIgnoreCase("miniStmt"))
{
return new MiniStatement();
}
throw new IllegalArgumentException("Selection doesnot exist");
}
}<br>
slide60. Singleton Pattern (cont…) Related Patterns:
Other patterns like Abstract Factory, Builder and Prototype can be implemented using the Singleton pattern.<br>
slide61. Abstract Factory Pattern Provides an interface to create a family of related objects, without explicitly specifying their concrete class.<br>
slide62. Abstract Factory Pattern (cont…) Non-Software Example<br>
slide63. Abstract Factory Pattern (cont…) Also known as:
Kit<br>
slide64. Abstract Factory Pattern (cont…) Motivation: Creating interface components for different look and feels.<br>
slide65. Abstract Factory Pattern (cont…) Applicability:
We should use Abstract Factory design pattern when:
The system needs to be independent of the products it works with are created.

The system should be configured to work with multiple families of products.

A family of products is designed to be used together and this constraint is needed to be enforced.

A library of products is to be provided and only their interfaces are to be revealed but not their implementations.<br>
slide66. Abstract Factory Pattern (cont…) Participants:
The classes that participate in the Abstract Factory are:
AbstractFactory: Declares an interface of operations that create abstract products.

ConcreteFactory: Implements operations to create concrete products.

AbstractProduct: Declares an interface for a type of product objects.

Product: Defines a product to be created by the corresponding ConcreteFactory. It implements the AbstractProduct interface.

Client: Uses the interfaces declared by the AbstractFactory and AbstractProduct classes.<br>
slide67. Abstract Factory Pattern (cont…) Collaborations:
The ConcreteFactory class creates products objects having a particular implementation. To create different product, the client should use a different concrete factory.

AbstractFactory defers creation of product objects to its ConcreteFactory subclass.<br>
slide68. Abstract Factory Pattern (cont…) Consequences:
Following are the benefits and liabilities of Abstract Factory pattern:

It isolates concrete classes.

It makes exchanging product families easy.

It creates consistency among products.

Supporting new kinds of products is difficult.<br>
slide69. Abstract Factory Pattern (cont…) Implementation: abstract class AbstractProductA
{
public abstract void operationA1();
public abstract void operationA2();
}<br>
slide70. Abstract Factory Pattern (cont…) class ProductA1 extends AbstractProductA
{
ProductA1(String arg)
{
System.out.println("Hello "+arg);
}
// Implement the code here
public void operationA1() { };
public void operationA2() { };
}
class ProductA2 extends AbstractProductA
{
ProductA2(String arg)
{
System.out.println("Hello "+arg);
}
// Implement the code here
public void operationA1() { };
public void operationA2() { };
}<br>
slide71. Abstract Factory Pattern (cont…) abstract class AbstractProductB
{
public abstract void operationB1();
public abstract void operationB2();
}<br>
slide72. Abstract Factory Pattern (cont…) class ProductB1 extends AbstractProductB
{
ProductB1(String arg)
{
System.out.println("Hello "+arg);
}
// Implement the code here
}
 
class ProductB2 extends AbstractProductB
{
ProductB2(String arg)
{
System.out.println("Hello "+arg);
}
// Implement the code here
}<br>
slide73. Abstract Factory Pattern (cont…) abstract class AbstractFactory
{
abstract AbstractProductA createProductA();
abstract AbstractProductB createProductB();
}<br>
slide74. Abstract Factory Pattern (cont…) class ConcreteFactory1 extends AbstractFactory
{
AbstractProductA createProductA()
{
return new ProductA1("ProductA1");
}
AbstractProductB createProductB()
{
return new ProductB1("ProductB1");
}
}
class ConcreteFactory2 extends AbstractFactory
{
AbstractProductA createProductA()
{
return new ProductA2("ProductA2");
}
AbstractProductB createProductB()
{
return new ProductB2("ProductB2");
}
}<br>
slide75. Abstract Factory Pattern (cont…) class FactoryMaker
{
private static AbstractFactory pf=null;
static AbstractFactory getFactory(String choice)
{
if(choice.equals("a"))
{
pf=new ConcreteFactory1();
}
else if(choice.equals("b"))
{
pf=new ConcreteFactory2();
}
return pf;
}
}<br>
slide76. Abstract Factory Pattern (cont…) public class Client
{
public static void main(String args[ ])
{
AbstractFactory pf=FactoryMaker.getFactory("a");
AbstractProductA product=pf.createProductA();
//more function calls on product
}
}<br>
slide77. Abstract Factory Pattern (cont…) Real World Example<br>
slide78. Abstract Factory Pattern (cont…) Sample Code: public interface Window
{
public void setTitle(String text);
public void repaint();
}<br>
slide79. Abstract Factory Pattern (cont…) //ConcreteProductA1
public class MSWindow implements Window
{
public void setTitle()
{
//MS Windows specific behaviour
}
public void repaint()
{
//MS Windows specific behaviour
}
}<br>
slide80. Abstract Factory Pattern (cont…) //ConcreteProductA2
public class MacOSXWindow implements Window
{
public void setTitle()
{
//Mac OSX specific behaviour
}
public void repaint()
{
//Mac OSX specific behaviour
}
}<br>
slide81. Abstract Factory Pattern (cont…) public interface AbstractWidgetFactory
{
public Window createWindow();
}<br>
slide82. Abstract Factory Pattern (cont…) //ConcreteFactory1
public class MsWindowsWidgetFactory
{
//create an MSWindow
public Window createWindow()
{
MSWindow window = new MSWindow();
return window;
}
}
 
//ConcreteFactory2
public class MacOSXWidgetFactory
{
//create a MacOSXWindow
public Window createWindow()
{
MacOSXWindow window = new MacOSXWindow();
return window;
}
}<br>
slide83. Abstract Factory Pattern (cont…) public class GUIBuilder
{
public void buildWindow(AbstractWidgetFactory widgetFactory)
{
Window window = widgetFactory.createWindow();
window.setTitle("New Window");
}
}<br>
slide84. Abstract Factory Pattern (cont…) public class Main
{
public static void main(String[] args)
{
GUIBuilder builder = new GUIBuilder();
AbstractWidgetFactory widgetFactory = null;
//check what platform we're on
if(Platform.currentPlatform()=="MACOSX")
{
widgetFactory = new MacOSXWidgetFactory();
}
else
{
widgetFactory = new MsWindowsWidgetFactory();
}
builder.buildWindow(widgetFactory);
}
}<br>
slide85. Abstract Factory Pattern (cont…) Related Patterns: AbstractFactory classes are often implemented with factory methods but they can also be implemented using Prototype. A concrete factory is often a Singleton.<br>
slide86. Factory Method Pattern Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.<br>
slide87. Factory Method Pattern (cont…) Non-Software Example<br>
slide88. Factory Method Pattern (cont…) Also known as:
Virtual constructor<br>
slide89. Factory Method Pattern (cont…) Motivation:
Document application<br>
slide90. Factory Method Pattern (cont…) Applicability:
A class can’t anticipate the class of objects it must create.

A class wants its sub classes to specify the objects it creates.

Classes delegate responsibility to one of several helper subclasses.<br>
slide91. Factory Method Pattern (cont…) Participants:
Product: Defines the interface of objects the factory method creates.

ConcreteProduct: Implements the Product interface.

Creator: Declares the factory method, which returns an object of the type Product.

ConcreteCreator: Overrides the factory method to return an instance of a ConcreteProduct.<br>
slide92. Factory Method Pattern (cont…) Collaborations:
Creator relies on its subclasses to provide an implementation for the factory method so that it returns an instance of the appropriate ConcreteProduct class.<br>
slide93. Factory Method Pattern (cont…) Consequences: The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes.

It provides customization hooks.

The factory has to be used for a family of objects.<br>
slide94. Factory Method Pattern (cont…) Implementation: public interface Product { }
 
public class ConcreteProduct implements Product { }
 
public abstract class Creator
{
public void anOperation()
{
Product product = factoryMethod();
}
protected abstract Product factoryMethod();
}
 
public class ConcreteCreator extends Creator
{
protected Product factoryMethod()
{
return new ConcreteProduct();
}
}<br>
slide95. Factory Method Pattern (cont…) Real World Example<br>
slide96. Factory Method Pattern (cont…) Related patterns:
Abstract Factory is often implemented with factory methods.

Factory methods are usually called within Template Methods. In the Document example above, NewDocument() is a template method.

Prototypes don’t require subclassing Creator. Instead, they often require an Initialize operation on the product class. Factory Method doesn’t require such operation.<br>
slide97. Builder Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations.<br>
slide98. Builder Pattern (cont…) Non-Software Example<br>
slide99. Builder Pattern (cont…) Motivation:
Text Converter<br>
slide100. Builder Pattern (cont…) 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 are assembled.

The construction process must allow different representations for the object that is constructed. Applicability:<br>
slide101. Builder Pattern (cont…) Builder: Provides an abstract interface for creating parts of a Product object.

ConcreteBuilder: Constructs and assembles the parts of the Product by implementing the Builder interface.

Director: Constructs an object using the Builder interface.

Product: Represents the complex object under construction. Participants:<br>
slide102. Builder Pattern (cont…) The benefits and pitfalls of Builder pattern are:
It let’s you vary the product’s internal representation.
It isolates code for construction and representation.
It gives you finer control over the construction process. Consequences:<br>
slide103. Builder Pattern (cont…) //Abstract Builder
class abstract class TextConverter
{
abstract void convertCharacter(char c);
abstract void convertParagraph();
}
 
// Product
class ASCIIText
{
public void append(char c)
{ //Implement the code here }
} Implementation:<br>
slide104. Builder Pattern (cont…) //Concrete Builder
class ASCIIConverter extends TextConverter
{
ASCIIText asciiTextObj; //resulting product
/*converts a character to target representation and appends to the resulting object*/
void convertCharacter(char c)
{
char asciiChar = new Character(c).charValue();
//gets the ascii character
asciiTextObj.append(asciiChar);
}
void convertParagraph() {}
ASCIIText getResult()
{
return asciiTextObj;
}
}<br>
slide105. Builder Pattern (cont…) //This class abstracts the document object
class Document
{
static int value;
char token;
public char getNextToken()
{
//Get the next token
return token;
}
}<br>
slide106. Builder Pattern (cont…) //Director
class RTFReader
{
private static final char EOF='0'; //Delimitor for End of File
final char CHAR='c';
final char PARA='p';
char t;
TextConverter builder;
RTFReader(TextConverter obj)
{
builder=obj;
}<br>
slide107. Builder Pattern (cont…) void parseRTF(Document doc)
{
while ((t=doc.getNextToken())!= EOF)
{
switch (t)
{
case CHAR: builder.convertCharacter(t);
case PARA: builder.convertParagraph();
}
}
}
}<br>
slide108. Builder Pattern (cont…) //Client
public class Client
{
void createASCIIText(Document doc)
{
ASCIIConverter asciiBuilder = new ASCIIConverter();
RTFReader rtfReader = new RTFReader(asciiBuilder);
rtfReader.parseRTF(doc);
ASCIIText asciiText = asciiBuilder.getResult();
}
public static void main(String args[])
{
Client client=new Client();
Document doc=new Document();
client.createASCIIText(doc);
system.out.println("This is an example of Builder Pattern");
}
}<br>
slide109. Builder Pattern (cont…) Real World Example Waiter<br>
slide110. Prototype Pattern To specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.<br>
slide111. Prototype Pattern (cont…)<br>
slide112. Prototype Pattern (cont…) Participants:

Client - creates a new object by asking a prototype to clone itself.

Prototype - declares an interface for cloning itself.

ConcretePrototype - implements the operation for cloning itself.<br>
slide113. Prototype Pattern (cont…) Implementation abstract class AbstractProduct implements Cloneable
{
public static AbstractProduct thePrototype;
public static AbstractProduct makeProduct()
{
try
{
return (AbstractProduct) thePrototype.clone();
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}<br>
slide114. Prototype Pattern (cont…) class ConcreteProductA extends AbstractProduct { }
 
class ConcreteProductB extends AbstractProduct { }
 
public class PrototypeDemo
{
public static void main(String[] args)
{
AbstractProduct.thePrototype = new ConcreteProductA();
AbstractProduct product = AbstractProduct.makeProduct();
System.out.println(product);
}
}<br>
slide115. Prototype Pattern (cont…) Real World Example<br>