1 1.Introduction 2.Factory Method 3.Abstract Factory 4.Prototype 5.Builder 6.Singleton

  • View
    212

  • Download
    0

Embed Size (px)

Transcript

  • Slide 1
  • 1 1.Introduction 2.Factory Method 3.Abstract Factory 4.Prototype 5.Builder 6.Singleton
  • Slide 2
  • 2 The Gang Of Four catalogue Purpose Scope Class Object CreationalStructural Behavioral Factory Method Abstract Factory Builder Prototype Singleton Adapter Bridge Composite Decorator Faade Flyweight Proxy Interpreter Template Method Chain of resp. Command Iterator Mediator Memento Observer State Strategy Visitor
  • Slide 3
  • 3 Creational Patterns Common objective : - abstract object creation process (how, when, by whom) - avoid hard coding concrete types in client code Fundamental problem : weaknesses of constructors fixed return type (= DYNAMIC type) not inherited -> no polymorphism possible Solutions : - use inheritance to decide run time type (class creational factory method) - delegate creation process to object run time types depend on object type and behavior (object creational)
  • Slide 4
  • 4 Creational Patterns No constructor polymorphism Desired dependency Unwanted dependency Solutions : Factory method Abstract Factory Prototype // Common c = new A(); Common d = new B(); //
  • Slide 5
  • 5 Motivation support pluggable GUI look and feel abstract look and feel code from client (to promote cross-platform portability) BUT : client must hardcode concrete object types (can not program to top level classes !) product families can get mixed !
  • Slide 6
  • 6 Motivation class Client { public Client() { // Button b = new XPButton(); Window w = new XPWindow(); Button c = new AppleButton(); // } Constructor weaknesses : fixed return type (= DYNAMIC type) not inherited -> no polymorphism possible Solution : create constructor-like method (the factory method)
  • Slide 7
  • 7 The solution A Factory Method
  • Slide 8
  • 8 The solution abstract class Client { private Button b,c; private Window w; public Client() { // b = createButton(); w = createWindow(); c = createButton(); // } public abstract Button createButton();// factory method public abstract Window createWindow();// factory method } class AppleClient extends Client { public AppleClient() {super();} public Window createWindow() {return new AppleWindow();} public Button createButton() {return new AppleButton();} } class Test { public static void main(String[] args) { Client c=new AppleClient(); }
  • Slide 9
  • 9 The general solution return new ConcreteProduct();
  • Slide 10
  • 10 Example : A bike race class Bicycle { protected String trademark; protected int ID; public Bicycle(String s,int i) {trademark=s;ID=i;} public String toString() { return ""+trademark+" : "+ID; } class StandardBicycle extends Bicycle { public StandardBicycle(String t,int i) {super(t,i);} public String toString() { return "Standard Bike : "+super.toString(); } class Tandem extends Bicycle { public Tandem(String t,int i) {super(t,i);} public String toString() { return "Tandem : "+super.toString(); }
  • Slide 11
  • 11 Example : A bike race class Participant { protected String name; public Participant(String s) {name=s;} public String toString() { return name; } class StandardParticipant extends Participant{ public StandardParticipant(String s) {super(s);} public String toString() { return "Standard Participant : "+super.toString(); } class TandemParticipant extends Participant{ public TandemParticipant(String s) {super(s);} public String toString() { return "Tandem Participant : "+super.toString(); }
  • Slide 12 "+p[i]+"\n"; return r; } public Participant doRace() { return p[(int)(Math.random()*(b.length))]; } public abstract Bicycle createBicycle(String s,int i); public abstract Participant createParticipant(String s); } NO CONSTRUCTORS HERE ! Factory methods">
  • 12 Example : A bike race abstract class Race { Bicycle[] b; Participant[] p; public Race(String[] tm,String[] n) { b=new Bicycle[tm.length]; p=new Participant[tm.length]; for(int i=0;i
  • 20 Example : A bike race class Race { RaceFactory f; Bicycle[] b; Participant[] p; public Race(RaceFactory ff,String[] tm,String[] n) { f=ff; b=new Bicycle[tm.length]; p=new Participant[tm.length]; for(int i=0;i
  • 28 Example : A bike race class Race { private Bicycle[] b; private Participant[] p; private Bicycle pB; private Participant pP; public Race(Bicycle bb,Participant pp,String[] tm,String[] n) { pB=bb; pP=pp; b=new Bicycle[tm.length]; p=new Participant[tm.length]; for(int i=0;i Tandem Participant : Mary + Y Tandem : Wheels2Wheels : 2 -> Tandem Participant : Eddy + Y Tandem : Eddy M. : 3 -> Tandem Participant : Fred + Y START... The winner is : Tandem Participant : Eddy + Y">
  • 29 Example : A bike race class Test { public static void main(String[] args) { String[] tm={"Eddy M.","Bike Inc.","Wheels2Wheels","Eddy M."}; String[] n={"John","Mary","Eddy","Fred"}; // Race r=new Race(new StandardBicycle("Dummy",0),new StandardParticipant("Dummy"),tm,n); Race r=new Race(new Tandem("Dummy",0),new TandemParticipant("X","Y"),tm,n); System.out.println(r); System.out.println("START..."); System.out.println("The winner is : "+r.doRace()); } The race : Tandem : Eddy M. : 0 -> Tandem Participant : John + Y Tandem : Bike Inc. : 1 -> Tandem Participant : Mary + Y Tandem : Wheels2Wheels : 2 -> Tandem Participant : Eddy + Y Tandem : Eddy M. : 3 -> Tandem Participant : Fred + Y START... The winner is : Tandem Participant : Eddy + Y
  • Slide 30
  • 30 Java cloning JAVA root type Object declares : public Object clone() for copying purposes Java approach : - override clone() method - requires DOWNCAST to actual type ! class Tandem extends Bicycle { public Tandem(String t,int i) {super(t,i);} public String toString() {return "Tandem : "+super.toString(); } public Object clone() {return new Tandem(trademark,ID);} } class Race { private Bicycle[] b;private Participant[] p; private Bicycle pB;private Participant pP; public Race(Bicycle bb,Participant pp,String[] tm,String[] n) { pB=bb;pP=pp; b=new Bicycle[tm.length];p=new Participant[tm.length]; for(int i=0;i