Top Banner
Design Patterns Factory Flyweight Singleton Observer Proxy Decorator
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java design patterns

Design Patterns

FactoryFlyweightSingletonObserverProxyDecorator

Page 2: Java design patterns

What Are Design Patterns?

A solution to a language independent OO Problem.

If the solution is a reoccurring over time, it becomes a “Pattern”.

.net

Java

C++C#

VB.net

Page 3: Java design patterns

Factory PatternDefinition

The Factory pattern provides a way to use an instance as a object factory. The factory can return an instance of one of several possible classes (in a subclass hierarchy), depending on the data provided to it.

Where to use

1) When a class can't anticipate which kind of class of object it must create.

2) You want to localize the knowledge of which class gets created.

3) When you have classes that is derived from the same subclasses, or they may in fact be unrelated classes that just share the same interface. Either way, the methods in these class instances are the same and can be used interchangeably.

4) When you want to insulate the client from the actual type that is being instantiated.

Page 4: Java design patterns

Factory PatternClass Diagram

Page 5: Java design patterns

Factory Patternpublic abstract class Product { public void writeName(String name) { System.out.println("My name is "+name); }}

public class ProductA extends Product { }

public class ProductB extends Product { public void writeName(String name) { StringBuilder tempName = new StringBuilder().append(name); System.out.println("My reversed name is" + tempName.reverse()); }}

public class ProductFactory { public Product createProduct(String type) { if(type.equals("B")) return new ProductB(); else return new ProductA(); }}

ProductFactory pf = new ProductFactory(); Product prod;

prod = pf.createProduct("A"); prod.writeName("John Doe"); prod = pf.createProduct("B"); prod.writeName("John Doe");

Connection, Statement, ResultSet

Page 6: Java design patterns

Proxy PatternDefinitionDefinition

A Proxy is a structural pattern that provides a stand-in for another object in order to control access to it.

Where to useWhere to use

1) When the creation of one object is relatively expensive it can be a good idea to replace it with a proxy that can make sure that instantiation of the expensive object is kept to a minimum.

2) Proxy pattern implementation allows for login and authority checking before one reaches the actual object that's requested.

3) Can provide a local representation for an object in a remote location.

Page 7: Java design patterns

Proxy PatternClass Diagram

Page 8: Java design patterns

Proxy Patterninterface IExploded {public void pumpPetrol(String arg);

}class ProxyExploded implements IExploded{

public void pumpPetrol(String arg){System.out.println("Pumping. ");

}}

public class Pattern_Proxy implements IExploded{

StringBuffer strbuf = new StringBuffer();public void pumpPetrol(String arg){

strbuf.append(arg);System.out.println("Pumping. ");if(strbuf.length() >70){

explosion2(); strbuf.delete(0, strbuf.length()); System.exit(0);}else if(strbuf.length()>50){

explosion1(); }

}

public void explosion1(){ System.out.println("System is about to Explode"); }

public void explosion2(){System.out.println("System has exploded... Kaboom!!.. \r\n”+”To Much Petrol.."+strbuf.length()+" L");

}}

public static void main(String arg[]){ IExploded prox = new Pattern_Proxy();

for(int y=0; y<50; y++){prox.pumpPetrol("Petrol.");

}}

Page 9: Java design patterns

Singleton PatternDefinition

The Singleton pattern provides the possibility to control the number of instances (mostly one) that are allowed to be made. We also receive a global point of access to it (them).

Where to use

When only one instance or a specific number of instances of a class are allowed. Facade objects are often Singletons because only one Facade object is required.

Benefits

•Controlled access to unique instance. •Reduced name space. •Allows refinement of operations and representations.

Page 10: Java design patterns

Class Diagram Singleton Pattern

Page 11: Java design patterns

Singleton Patternpublic class MapLogger{ private static MapLogger mapper=null; // Prevent clients from using the constructor private MapLogger() { /* Nothing Here :-) */ }

//Control the accessible (allowed) instances public static MapLogger getMapLogger() { if (mapper == null) { mapper = new MapLogger(); } return mapper; }

public synchronized void put(String key, String val) { // Write to Map/Hashtable... }}

Page 12: Java design patterns

Flyweight Pattern

Definition The Flyweight pattern provides a mechanism by which you can avoid creating a large number of 'expensive' objects and instead reuse existing instances to represent new ones.

Where to use 1) When there is a very large number of objects that may not fit in memory. 2) When most of an objects state can be stored on disk or calculated at Runtime. 3) When there are groups of objects that share state. 4) When the remaining state can be factored into a much smaller number of objects with shared state.

BenefitsReduce the number of objects created, decrease memory footprint and increase performance.

Page 13: Java design patterns

Flyweight Pattern

Page 14: Java design patterns

Flyweight Patternclass Godzilla { private int row; public Godzilla( int theRow ) { row = theRow; System.out.println( "ctor: " + row ); } void report( int theCol ) { System.out.print( " " + row + theCol ); } } class Factory {

private Godzilla[] pool; public Factory( int maxRows ) { pool = new Godzilla[maxRows]; } public Godzilla getFlyweight( int theRow ) { if (pool[theRow] == null) pool[theRow] = new Godzilla( theRow ); return pool[theRow]; } }

public class FlyweightDemo { public static final int ROWS = 6, COLS = 10; public static void main( String[] args ) { Factory theFactory = new Factory( ROWS ); for (int i=0; i < ROWS; i++) { for (int j=0; j < COLS; j++) theFactory.getFlyweight( i ).report( j ); System.out.println();} } }

Page 15: Java design patterns

Observer PatternDefinition

Proxy pattern is a behavioral pattern. It defines a one-to-many dependency between objects, so that when one object changes its state, all its dependent objects are notified and updated.

Where to use

1) When state changes of a one object must be reflected in other objects without depending on the state changing process of the object (reduce the coupling between the objects).

Page 16: Java design patterns

Observer Pattern

Page 17: Java design patterns

Observer Patternpublic class WeatherStation extends Observable implements Runnable { public void run() { String report = this.getCurrentReport(); notifyObservers( report ); }}

public class WeatherObserver implements Observer { private String response; public void update (Observable obj, Object arg) { response = (String)arg; }}

Page 18: Java design patterns

Observer Patternpublic class WeatherClient { public static void main(String args[]) { // create a WheatherStation instance final WeatherStation station = new WeatherStation(); // create an observer final WeatherObserver weatherObserver = new WeatherObserver(); // subscribe the observer to the event source station.addObserver( weatherObserver ); // starts the event thread Thread thread = new Thread(station); thread.start(); }}

Page 19: Java design patterns

Decorator PatternDefinition

Decorator pattern is a structural pattern. Intent of the pattern is to add additional responsibilities dynamically to an object.

Where to use

1) When you want the responsibilities added to or removed from an object at runtime.

2) When using inheritance results in a large number of subclasses.

Page 20: Java design patterns

Decorator Pattern

Page 21: Java design patterns

Decorator Pattern// The Coffee Interface defines the functionality of Coffee implemented by decoratorpublic interface Coffee { // returns the cost of the coffee public double getCost(); // returns the ingredients of the coffee public String getIngredients(); } // implementation of a simple coffee without any extra ingredientspublic class SimpleCoffee implements Coffee { public double getCost() { return 1; } public String getIngredients() { return "Coffee"; }}

Page 22: Java design patterns

Decorator Pattern// abstract decorator class - note that it implements Coffee interfaceabstract public class CoffeeDecorator implements Coffee {

protected final Coffee decoratedCoffee; protected String ingredientSeparator = ", "; public CoffeeDecorator(Coffee decoratedCoffee) { this.decoratedCoffee = decoratedCoffee; } public double getCost() { // implementing methods of the interface return decoratedCoffee.getCost(); } public String getIngredients() { return decoratedCoffee.getIngredients(); }}

Page 23: Java design patterns

Decorator Pattern// Decorator Milk that mixes milk with coffee// note it extends CoffeeDecoratorpublic class Milk extends CoffeeDecorator { public Milk(Coffee decoratedCoffee) { super(decoratedCoffee); } public double getCost() { // overriding methods defined in the abstract superclass return super.getCost() + 0.5; } public String getIngredients() { return super.getIngredients() + ingredientSeparator + "Milk"; }}

Page 24: Java design patterns

Decorator Patternpublic class Whip extends CoffeeDecorator { public Whip(Coffee decoratedCoffee) { super(decoratedCoffee); } public double getCost() { return super.getCost() + 0.7; } public String getIngredients() { return super.getIngredients() + ingredientSeparator + "Whip"; }}

Page 25: Java design patterns

Decorator Patternpublic class CoffeMaker { public static void main(String[] args) { Coffee c = new SimpleCoffee(); System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients()); c = new Milk(c); System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients()); }}

Page 26: Java design patterns

Decorator Patternpublic class Whip extends CoffeeDecorator { public Whip(Coffee decoratedCoffee) { super(decoratedCoffee); } public double getCost() { return super.getCost() + 0.7; } public String getIngredients() { return super.getIngredients() + ingredientSeparator + "Whip"; }}