Factory Pattern
The 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.
Design Principle
Depend upon abstractions. Do not depend upon concrete classes
The following guidelines can help you avoid OO designs that violate
the Dependency Inversion Principle:
No variable should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base classes.
==========
Example
Design Principle
Depend upon abstractions. Do not depend upon concrete classes
The following guidelines can help you avoid OO designs that violate
the Dependency Inversion Principle:
No variable should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base classes.
==========
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | public abstract class Product { public String name; Product(){ name = "No Name"; } public abstract void prepare(); public void polishIt(){ System.out.println("Polishing it."); } public void makeIt(){ System.out.println("Make it."); } public void boxIt(){ System.out.println("Box it."); } public void name(){ System.out.println("\nName: "+name); } } ========== public abstract class Factory { Product product ; public void orderProduct(String type){ product = createProduct(type); product.prepare(); } public abstract Product createProduct(String type); } ========== public class ToysFactory extends Factory{ @Override public Product createProduct(String type) { if("soft".equalsIgnoreCase(type)){ return new TeddyBear(); } else if("cars".equalsIgnoreCase(type)){ return new Ferrari(); } return null; } } ========== public class PhoneFactory extends Factory{ @Override public Product createProduct(String type) { if("nokia".equalsIgnoreCase(type)){ return new Nokia(); } else if("sony".equalsIgnoreCase(type)){ return new Sony(); } return null; } } ========== public class Ferrari extends Product { public Ferrari() { name = "Ferrari car"; } @Override public void prepare() { name(); makeIt(); polishIt(); boxIt(); } } ========== public class Nokia extends Product { Nokia(){ name = "Nokia 3310 Phone"; } @Override public void prepare() { name(); makeIt(); polishIt(); boxIt(); } } ========== public class Sony extends Product { Sony(){ name = "Sony Xperia"; } @Override public void prepare() { name(); makeIt(); polishIt(); boxIt(); } } ========== public class TeddyBear extends Product { public TeddyBear() { name = "TeddyBear"; } @Override public void prepare() { name(); makeIt(); polishIt(); boxIt(); } } ========== public class FactoryFlight { public static void main(String[] dataBag){ Factory factory = new ToysFactory(); factory.orderProduct("soft"); factory.orderProduct("cars"); factory = new PhoneFactory(); factory.orderProduct("nokia"); factory.orderProduct("sony"); } } |