一、简单工厂模式:比较简单,produceProduct方法最好是静态的。它是根据参数来决定返回哪一种类型的product。
1 public interface Product 2 { 3 public void getName(); 4 } 5 6 public class ProductA implements Product 7 { 8 public void getName() 9 { 10 System.out.println("I am A."); 11 } 12 } 13 14 public class ProductB implements Product 15 { 16 public void getName() 17 { 18 System.out.println("I am B."); 19 } 20 } 21 22 public class Factory 23 { 24 public Product produceProduct(String type) 25 { 26 if(type.equals("A")) 27 { 28 return new ProductA(); 29 } 30 else if(type.equals("B")) 31 { 32 return new ProductB(); 33 } 34 else 35 { 36 return null; 37 } 38 } 39 40 public static void main(String[] args) 41 { 42 Factory factory = new Factory(); 43 Product product = factory.produceProduct("A"); 44 product.getName(); 45 product = factory.produceProduct("B"); 46 product.getName(); 47 } 48 }
二、工厂方法模式:不同于简单工厂,它将不同的产品放在实现了工厂接口的不同工厂类中,这样就算其中一个工厂出了问题,其他工厂也还可以正常工作。
1 public interface Product 2 { 3 public void getName(); 4 } 5 6 public class ProductA implements Product 7 { 8 public void getName() 9 { 10 System.out.println("I am A."); 11 } 12 } 13 14 public class ProductB implements Product 15 { 16 public void getName() 17 { 18 System.out.println("I am B."); 19 } 20 } 21 22 public interface Produce 23 { 24 public Product produce(); 25 } 26 27 public class FactoryA implements Produce 28 { 29 public Product produce() 30 { 31 return new ProductA(); 32 } 33 } 34 35 public class FactoryB implements Produce 36 { 37 public Product produce() 38 { 39 return new ProductB(); 40 } 41 } 42 43 public class Test 44 { 45 public static void main(String[] args) 46 { 47 FactoryA fa = new FactoryA(); 48 Product p = fa.produce(); 49 p.getName(); 50 FactoryB fb = new FactoryB(); 51 p = fb.produce(); 52 p.getName(); 53 } 54 }
三、抽象工厂模式:用于创建一系列产品
1 public interface Fruit 2 { 3 public void getFruit(); 4 } 5 6 public class FruitA implements Fruit 7 { 8 public void getFruit() 9 { 10 System.out.println("FruitA"); 11 } 12 } 13 14 public class FruitB implements Fruit 15 { 16 public void getFruit() 17 { 18 System.out.println("FruitB"); 19 } 20 } 21 22 public interface Vegetable 23 { 24 public void getVegetable(); 25 } 26 27 public class VegetableA implements Vegetable 28 { 29 public void getVegetable() 30 { 31 System.out.println("VegetableA"); 32 } 33 } 34 35 public class VegetableB implements Vegetable 36 { 37 public void getVegetable() 38 { 39 System.out.println("VegetableB"); 40 } 41 } 42 43 public interface AbstractFactory 44 { 45 public Fruit produceFruit(); 46 public Vegetable produceVegetable(); 47 } 48 49 public class FactoryA implements AbstractFactory 50 { 51 public Fruit produceFruit() 52 { 53 return new FruitA(); 54 } 55 56 public Vegetable produceVegetable() 57 { 58 return new VegetableA(); 59 } 60 } 61 62 public class FactoryB implements AbstractFactory 63 { 64 public Fruit produceFruit() 65 { 66 return new FruitB(); 67 } 68 69 public Vegetable produceVegetable() 70 { 71 return new VegetableB(); 72 } 73 }
说明:也可以有FactoryAB是return new FruitA()和return new VegetableB()的,或者是FactoryBA是return new FruitB()和return new VegetableA()的。其目的在于创建一系列产品。