Gang of Four GOF Erich Gamma Richard Helm

  • Slides: 115
Download presentation
Gang of Four (GOF) Erich Gamma Richard Helm Ralph Johnson John Vlissides Design Patterns

Gang of Four (GOF) Erich Gamma Richard Helm Ralph Johnson John Vlissides Design Patterns (DP) By P. S. Suryateja Asst. Professor CSE Dept Vishnu Institute of Technology

Objectives • • Know what are design patterns? Know about GOF patterns. How to

Objectives • • Know what are design patterns? Know about GOF patterns. How to select and use a pattern? How to apply a pattern?

Outline • Introduction to design patterns – What is a design pattern? – Need

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?

Outline (cont…) • Creational patterns – Introduction – Singleton – Abstract Factory – Factory

Outline (cont…) • Creational patterns – Introduction – Singleton – Abstract Factory – Factory Method – Builder – Prototype

Outline (cont…) • Structural Patterns

Outline (cont…) • Structural Patterns

Introduction to Design Patterns

Introduction to Design Patterns

What is a design pattern (DP)? “Each pattern describes a problem which occurs over

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.

What is a design pattern (DP)? (cont…) Four essential elements of a pattern: •

What is a design pattern (DP)? (cont…) Four essential elements of a pattern: • Pattern name • Problem • Solution • Consequences

Need for design patterns • Designing reusable object-oriented software (API’s) is hard. • Experienced

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?

Use of design patterns • Make it easier to reuse successful designs and architectures.

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.

Why should we use DP’s? These are already tested and proven solutions used by

Why should we use DP’s? These are already tested and proven solutions used by many experienced designers.

MVC Architecture

MVC Architecture

Design Patterns in Smalltalk MVC

Design Patterns in Smalltalk MVC

Describing design patterns 1. 2. 3. 4. 5. 6. 7. 8. Pattern name and

Describing design patterns 1. 2. 3. 4. 5. 6. 7. 8. Pattern name and classification Intent Also known as Motivation Applicability Structure Participants Collaborations

Describing design patterns (cont…) 9. Consequences 10. Implementation 11. Sample code 12. Known uses

Describing design patterns (cont…) 9. Consequences 10. Implementation 11. Sample code 12. Known uses 13. Related patterns

Catalog of design patterns 1. Abstract Factory 2. Adapter 3. Bridge 4. Builder 5.

Catalog of design patterns 1. Abstract Factory 2. Adapter 3. Bridge 4. Builder 5. Chain of Responsibility 6. Command 7. Composite 8. Decorator 9. Façade 10. Factory Method

Catalog of design patterns (cont…) 11. Flyweight 12. Interpreter 13. Iterator 14. Mediator 15.

Catalog of design patterns (cont…) 11. Flyweight 12. Interpreter 13. Iterator 14. Mediator 15. Memento 16. Observer 17. Prototype 18. Proxy 19. Singleton 20. State

Catalog of design patterns (cont…) 21. Strategy 22. Template Method 23. Visitor

Catalog of design patterns (cont…) 21. Strategy 22. Template Method 23. Visitor

Abstract Factory

Abstract Factory

Adapter

Adapter

Bridge

Bridge

Builder

Builder

Chain of Responsibility

Chain of Responsibility

Command

Command

Composite

Composite

Decorator

Decorator

Façade

Façade

Factory Method

Factory Method

Flyweight

Flyweight

Interpreter

Interpreter

Iterator

Iterator

Mediator

Mediator

Memento

Memento

Observer

Observer

Prototype

Prototype

Proxy

Proxy

Singleton

Singleton

State

State

Strategy

Strategy

Template Method

Template Method

Visitor

Visitor

Organizing the catalog

Organizing the catalog

How to select a design pattern • Consider how design patterns solve design problems

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

Relationships between patterns

Relationships between patterns

How to use a design pattern 1. Read the pattern once through for a

How to use a design pattern 1. Read the pattern once through for a overview. 2. Study the structure, participants and collaborations sections. 3. Look at the sample code section to see a concrete example of the pattern in action. 4. Choose names for pattern participants that are meaningful in the application context. 5. Define the classes. 6. Define application-specific names for the operations in the pattern. 7. Implement the operations to carry out the responsibilities and collaborations in the pattern.

Creational Patterns

Creational Patterns

Introduction • Deals with how objects are created. • Increase the system’s flexibility in

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: 1. 2. Class-creational patterns Object-creational patterns • Five creational patterns: 1. 2. 3. 4. 5. Abstract Factory Builder Factory Method Prototype Singleton

Singleton Pattern • To create exactly one object of a class and to provide

Singleton Pattern • To create exactly one object of a class and to provide a global point of access to it. Structure

Singleton Pattern (cont…) Non-Software Example

Singleton Pattern (cont…) Non-Software Example

Singleton Pattern (cont…) Motivation: 1) Need to create exactly one user interface object. 2)

Singleton Pattern (cont…) Motivation: 1) Need to create exactly one user interface object. 2) Need to create only one print spooler.

Singleton Pattern (cont…) Applicability: Singleton Pattern can be used when: 1)There must be exactly

Singleton Pattern (cont…) Applicability: Singleton Pattern can be used when: 1)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.

Singleton Pattern (cont…) Participants: Consists of a “Singleton” class with the following members: 1)A

Singleton Pattern (cont…) Participants: Consists of a “Singleton” class with the following members: 1)A static data member 2)A private constructor 3)A static public method that returns a reference to the static data member.

Singleton Pattern (cont…) Collaborations: Clients access the singleton instance solely through the singleton’s class

Singleton Pattern (cont…) Collaborations: Clients access the singleton instance solely through the singleton’s class operation.

Singleton Pattern (cont…) Consequences: • Controlled access to sole instance. • Reduced name space.

Singleton Pattern (cont…) Consequences: • Controlled access to sole instance. • Reduced name space. • Permits refinement of operations and representation. • Permits a variable number of instances.

Singleton Pattern (cont…) Implementation: class Singleton { private static Singleton instance; private Singleton() {.

Singleton Pattern (cont…) Implementation: class Singleton { private static Singleton instance; private Singleton() {. . . } public static synchronized Singleton get. Instance() { if (instance == null) instance = new Singleton(); return instance; }. . . public void do. Operation() {. . . } }

Singleton Pattern (cont…) Sample code: Design a small ATM printing application which can generate

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.

Singleton Pattern (cont…) Real World Example

Singleton Pattern (cont…) Real World Example

Singleton Pattern (cont…) Code for Statement. Factory class is as shown below: public class

Singleton Pattern (cont…) Code for Statement. Factory class is as shown below: public class Statement. Factory extends Factory { private static Statement. Factory unique. Instance; private Statement. Factory() {} public static Statement. Factory get. Unique. Instance() { if (unique. Instance == null) { unique. Instance = new Statement. Factory(); System. out. println("Creating a new Statement. Factory instance"); } return unique. Instance; }

Singleton Pattern (cont…) public Statement. Type create. Statements(String selection) { if (selection. equals. Ignore.

Singleton Pattern (cont…) public Statement. Type create. Statements(String selection) { if (selection. equals. Ignore. Case("detailed. Stmt")) { return new Detailed. Statement(); } else if (selection. equals. Ignore. Case("mini. Stmt")) { return new Mini. Statement(); } throw new Illegal. Argument. Exception("Selection doesnot exist"); } }

Singleton Pattern (cont…) Related Patterns: Other patterns like Abstract Factory, Builder and Prototype can

Singleton Pattern (cont…) Related Patterns: Other patterns like Abstract Factory, Builder and Prototype can be implemented using the Singleton pattern.

Abstract Factory Pattern • Provides an interface to create a family of related objects,

Abstract Factory Pattern • Provides an interface to create a family of related objects, without explicitly specifying their concrete class.

Abstract Factory Pattern (cont…) Non-Software Example

Abstract Factory Pattern (cont…) Non-Software Example

Abstract Factory Pattern (cont…) Also known as: Kit

Abstract Factory Pattern (cont…) Also known as: Kit

Abstract Factory Pattern (cont…) Motivation: Creating interface components for different look and feels.

Abstract Factory Pattern (cont…) Motivation: Creating interface components for different look and feels.

Abstract Factory Pattern (cont…) Applicability: We should use Abstract Factory design pattern when: •

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.

Abstract Factory Pattern (cont…) Participants: The classes that participate in the Abstract Factory are:

Abstract Factory Pattern (cont…) Participants: The classes that participate in the Abstract Factory are: • Abstract. Factory: Declares an interface of operations that create abstract products. • Concrete. Factory: Implements operations to create concrete products. • Abstract. Product: Declares an interface for a type of product objects. • Product: Defines a product to be created by the corresponding Concrete. Factory. It implements the Abstract. Product interface. • Client: Uses the interfaces declared by the Abstract. Factory and Abstract. Product classes.

Abstract Factory Pattern (cont…) Collaborations: • The Concrete. Factory class creates products objects having

Abstract Factory Pattern (cont…) Collaborations: • The Concrete. Factory class creates products objects having a particular implementation. To create different product, the client should use a different concrete factory. • Abstract. Factory defers creation of product objects to its Concrete. Factory subclass.

Abstract Factory Pattern (cont…) Consequences: Following are the benefits and liabilities of Abstract Factory

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.

Abstract Factory Pattern (cont…) Implementation: abstract class Abstract. Product. A { public abstract void

Abstract Factory Pattern (cont…) Implementation: abstract class Abstract. Product. A { public abstract void operation. A 1(); public abstract void operation. A 2(); }

Abstract Factory Pattern (cont…) class Product. A 1 extends Abstract. Product. A { Product.

Abstract Factory Pattern (cont…) class Product. A 1 extends Abstract. Product. A { Product. A 1(String arg) { System. out. println("Hello "+arg); } // Implement the code here public void operation. A 1() { }; public void operation. A 2() { }; } class Product. A 2 extends Abstract. Product. A { Product. A 2(String arg) { System. out. println("Hello "+arg); } // Implement the code here public void operation. A 1() { }; public void operation. A 2() { }; }

Abstract Factory Pattern (cont…) abstract class Abstract. Product. B { public abstract void operation.

Abstract Factory Pattern (cont…) abstract class Abstract. Product. B { public abstract void operation. B 1(); public abstract void operation. B 2(); }

Abstract Factory Pattern (cont…) class Product. B 1 extends Abstract. Product. B { Product.

Abstract Factory Pattern (cont…) class Product. B 1 extends Abstract. Product. B { Product. B 1(String arg) { System. out. println("Hello "+arg); } // Implement the code here } class Product. B 2 extends Abstract. Product. B { Product. B 2(String arg) { System. out. println("Hello "+arg); } // Implement the code here }

Abstract Factory Pattern (cont…) abstract class Abstract. Factory { abstract Abstract. Product. A create.

Abstract Factory Pattern (cont…) abstract class Abstract. Factory { abstract Abstract. Product. A create. Product. A(); abstract Abstract. Product. B create. Product. B(); }

Abstract Factory Pattern (cont…) class Concrete. Factory 1 extends Abstract. Factory { Abstract. Product.

Abstract Factory Pattern (cont…) class Concrete. Factory 1 extends Abstract. Factory { Abstract. Product. A create. Product. A() { return new Product. A 1("Product. A 1"); } Abstract. Product. B create. Product. B() { return new Product. B 1("Product. B 1"); } } class Concrete. Factory 2 extends Abstract. Factory { Abstract. Product. A create. Product. A() { return new Product. A 2("Product. A 2"); } Abstract. Product. B create. Product. B() { return new Product. B 2("Product. B 2"); } }

Abstract Factory Pattern (cont…) class Factory. Maker { private static Abstract. Factory pf=null; static

Abstract Factory Pattern (cont…) class Factory. Maker { private static Abstract. Factory pf=null; static Abstract. Factory get. Factory(String choice) { if(choice. equals("a")) { pf=new Concrete. Factory 1(); } else if(choice. equals("b")) { pf=new Concrete. Factory 2(); } return pf; } }

Abstract Factory Pattern (cont…) public class Client { public static void main(String args[ ])

Abstract Factory Pattern (cont…) public class Client { public static void main(String args[ ]) { Abstract. Factory pf=Factory. Maker. get. Factory("a"); Abstract. Product. A product=pf. create. Product. A(); //more function calls on product } }

Abstract Factory Pattern (cont…) Real World Example

Abstract Factory Pattern (cont…) Real World Example

Abstract Factory Pattern (cont…) Sample Code: public interface Window { public void set. Title(String

Abstract Factory Pattern (cont…) Sample Code: public interface Window { public void set. Title(String text); public void repaint(); }

Abstract Factory Pattern (cont…) //Concrete. Product. A 1 public class MSWindow implements Window {

Abstract Factory Pattern (cont…) //Concrete. Product. A 1 public class MSWindow implements Window { public void set. Title() { //MS Windows specific behaviour } public void repaint() { //MS Windows specific behaviour } }

Abstract Factory Pattern (cont…) //Concrete. Product. A 2 public class Mac. OSXWindow implements Window

Abstract Factory Pattern (cont…) //Concrete. Product. A 2 public class Mac. OSXWindow implements Window { public void set. Title() { //Mac OSX specific behaviour } public void repaint() { //Mac OSX specific behaviour } }

Abstract Factory Pattern (cont…) public interface Abstract. Widget. Factory { public Window create. Window();

Abstract Factory Pattern (cont…) public interface Abstract. Widget. Factory { public Window create. Window(); }

Abstract Factory Pattern (cont…) //Concrete. Factory 1 public class Ms. Windows. Widget. Factory {

Abstract Factory Pattern (cont…) //Concrete. Factory 1 public class Ms. Windows. Widget. Factory { //create an MSWindow public Window create. Window() { MSWindow window = new MSWindow(); return window; } } //Concrete. Factory 2 public class Mac. OSXWidget. Factory { //create a Mac. OSXWindow public Window create. Window() { Mac. OSXWindow window = new Mac. OSXWindow(); return window; } }

Abstract Factory Pattern (cont…) public class GUIBuilder { public void build. Window(Abstract. Widget. Factory

Abstract Factory Pattern (cont…) public class GUIBuilder { public void build. Window(Abstract. Widget. Factory widget. Factory) { Window window = widget. Factory. create. Window(); window. set. Title("New Window"); } }

Abstract Factory Pattern (cont…) public class Main { public static void main(String[] args) {

Abstract Factory Pattern (cont…) public class Main { public static void main(String[] args) { GUIBuilder builder = new GUIBuilder(); Abstract. Widget. Factory widget. Factory = null; //check what platform we're on if(Platform. current. Platform()=="MACOSX") { widget. Factory = new Mac. OSXWidget. Factory(); } else { widget. Factory = new Ms. Windows. Widget. Factory(); } builder. build. Window(widget. Factory); } }

Abstract Factory Pattern (cont…) Related Patterns: Abstract. Factory classes are often implemented with factory

Abstract Factory Pattern (cont…) Related Patterns: Abstract. Factory classes are often implemented with factory methods but they can also be implemented using Prototype. A concrete factory is often a Singleton.

Factory Method Pattern • Defines an interface for creating an object, but lets subclasses

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.

Factory Method Pattern (cont…) Non-Software Example

Factory Method Pattern (cont…) Non-Software Example

Factory Method Pattern (cont…) Also known as: Virtual constructor

Factory Method Pattern (cont…) Also known as: Virtual constructor

Factory Method Pattern (cont…) Motivation: Document application

Factory Method Pattern (cont…) Motivation: Document application

Factory Method Pattern (cont…) Applicability: • A class can’t anticipate the class of objects

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.

Factory Method Pattern (cont…) Participants: • Product: Defines the interface of objects the factory

Factory Method Pattern (cont…) Participants: • Product: Defines the interface of objects the factory method creates. • Concrete. Product: Implements the Product interface. • Creator: Declares the factory method, which returns an object of the type Product. • Concrete. Creator: Overrides the factory method to return an instance of a Concrete. Product.

Factory Method Pattern (cont…) Collaborations: Creator relies on its subclasses to provide an implementation

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 Concrete. Product class.

Factory Method Pattern (cont…) Consequences: • The main reason for which the factory pattern

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.

Factory Method Pattern (cont…) Implementation: public interface Product { } public class Concrete. Product

Factory Method Pattern (cont…) Implementation: public interface Product { } public class Concrete. Product implements Product { } public abstract class Creator { public void an. Operation() { Product product = factory. Method(); } protected abstract Product factory. Method(); } public class Concrete. Creator extends Creator { protected Product factory. Method() { return new Concrete. Product(); } }

Factory Method Pattern (cont…) Real World Example

Factory Method Pattern (cont…) Real World Example

Factory Method Pattern (cont…) Related patterns: • Abstract Factory is often implemented with factory

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, New. Document() 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.

Builder Pattern • Separate the construction of a complex object from its representation so

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

Builder Pattern (cont…) Non-Software Example

Builder Pattern (cont…) Non-Software Example

Builder Pattern (cont…) Motivation: Text Converter

Builder Pattern (cont…) Motivation: Text Converter

Builder Pattern (cont…) Applicability: Use the Builder pattern when: • The algorithm for creating

Builder Pattern (cont…) 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 are assembled. • The construction process must allow different representations for the object that is constructed.

Builder Pattern (cont…) Participants: • Builder: Provides an abstract interface for creating parts of

Builder Pattern (cont…) Participants: • Builder: Provides an abstract interface for creating parts of a Product object. • Concrete. Builder: 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.

Builder Pattern (cont…) Consequences: The benefits and pitfalls of Builder pattern are: • It

Builder Pattern (cont…) Consequences: 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.

Builder Pattern (cont…) Implementation: //Abstract Builder class abstract class Text. Converter { abstract void

Builder Pattern (cont…) Implementation: //Abstract Builder class abstract class Text. Converter { abstract void convert. Character(char c); abstract void convert. Paragraph(); } // Product class ASCIIText { public void append(char c) { //Implement the code here } }

Builder Pattern (cont…) //Concrete Builder class ASCIIConverter extends Text. Converter { ASCIIText ascii. Text.

Builder Pattern (cont…) //Concrete Builder class ASCIIConverter extends Text. Converter { ASCIIText ascii. Text. Obj; //resulting product /*converts a character to target representation and appends to the resulting object*/ void convert. Character(char c) { char ascii. Char = new Character(c). char. Value(); //gets the ascii character ascii. Text. Obj. append(ascii. Char); } void convert. Paragraph() {} ASCIIText get. Result() { return ascii. Text. Obj; } }

Builder Pattern (cont…) //This class abstracts the document object class Document { static int

Builder Pattern (cont…) //This class abstracts the document object class Document { static int value; char token; public char get. Next. Token() { //Get the next token return token; } }

Builder Pattern (cont…) //Director class RTFReader { private static final char EOF='0'; //Delimitor for

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; Text. Converter builder; RTFReader(Text. Converter obj) { builder=obj; }

Builder Pattern (cont…) void parse. RTF(Document doc) { while ((t=doc. get. Next. Token())!= EOF)

Builder Pattern (cont…) void parse. RTF(Document doc) { while ((t=doc. get. Next. Token())!= EOF) { switch (t) { case CHAR: builder. convert. Character(t); case PARA: builder. convert. Paragraph(); } }

Builder Pattern (cont…) //Client public class Client { void create. ASCIIText(Document doc) { ASCIIConverter

Builder Pattern (cont…) //Client public class Client { void create. ASCIIText(Document doc) { ASCIIConverter ascii. Builder = new ASCIIConverter(); RTFReader rtf. Reader = new RTFReader(ascii. Builder); rtf. Reader. parse. RTF(doc); ASCIIText ascii. Text = ascii. Builder. get. Result(); } public static void main(String args[]) { Client client=new Client(); Document doc=new Document(); client. create. ASCIIText(doc); system. out. println("This is an example of Builder Pattern"); } }

Builder Pattern (cont…) Real World Example Waiter

Builder Pattern (cont…) Real World Example Waiter

Prototype Pattern • To specify the kinds of objects to create using a prototypical

Prototype Pattern • To specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Prototype Pattern (cont…)

Prototype Pattern (cont…)

Prototype Pattern (cont…) Participants: • Client - creates a new object by asking a

Prototype Pattern (cont…) Participants: • Client - creates a new object by asking a prototype to clone itself. • Prototype - declares an interface for cloning itself. • Concrete. Prototype - implements the operation for cloning itself.

Prototype Pattern (cont…) Implementation abstract class Abstract. Product implements Cloneable { public static Abstract.

Prototype Pattern (cont…) Implementation abstract class Abstract. Product implements Cloneable { public static Abstract. Product the. Prototype; public static Abstract. Product make. Product() { try { return (Abstract. Product) the. Prototype. clone(); } catch(Clone. Not. Supported. Exception e) { return null; } } }

Prototype Pattern (cont…) class Concrete. Product. A extends Abstract. Product { } class Concrete.

Prototype Pattern (cont…) class Concrete. Product. A extends Abstract. Product { } class Concrete. Product. B extends Abstract. Product { } public class Prototype. Demo { public static void main(String[] args) { Abstract. Product. the. Prototype = new Concrete. Product. A(); Abstract. Product product = Abstract. Product. make. Product(); System. out. println(product); } }

Prototype Pattern (cont…) Real World Example

Prototype Pattern (cont…) Real World Example