1 package AbstractFactory; 2 //抽象产品(Bmw和Audi同理) 3 abstract class BenzCar{ 4 private String name; 5 6 public abstract void drive(); 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 } 15 //具体产品(Bmw和Audi同理) 16 class BenzSportCar extends BenzCar{ 17 public void drive(){ 18 System.out.println(this.getName()+"----BenzSportCar-----------------------"); 19 } 20 } 21 class BenzBusinessCar extends BenzCar{ 22 public void drive(){ 23 System.out.println(this.getName()+"----BenzBusinessCar-----------------------"); 24 } 25 } 26 27 abstract class BmwCar{ 28 private String name; 29 30 public abstract void drive(); 31 32 public String getName() { 33 return name; 34 } 35 public void setName(String name) { 36 this.name = name; 37 } 38 } 39 class BmwSportCar extends BmwCar{ 40 public void drive(){ 41 System.out.println(this.getName()+"----BmwSportCar-----------------------"); 42 } 43 } 44 class BmwBusinessCar extends BmwCar{ 45 public void drive(){ 46 System.out.println(this.getName()+"----BmwBusinessCar-----------------------"); 47 } 48 } 49 50 abstract class AudiCar{ 51 private String name; 52 53 public abstract void drive(); 54 55 public String getName() { 56 return name; 57 } 58 public void setName(String name) { 59 this.name = name; 60 } 61 } 62 class AudiSportCar extends AudiCar{ 63 public void drive(){ 64 System.out.println(this.getName()+"----AudiSportCar-----------------------"); 65 } 66 } 67 class AudiBusinessCar extends AudiCar{ 68 public void drive(){ 69 System.out.println(this.getName()+"----AudiBusinessCar-----------------------"); 70 } 71 } 72 73 74 //抽象工厂 75 abstract class Factory { 76 public abstract BenzCar createBenzCar(String car) throws Exception; 77 78 public abstract BmwCar createBmwCar(String car) throws Exception; 79 80 public abstract AudiCar createAudiCar(String car) throws Exception; 81 } 82 //具体工厂 83 class SportDriver extends Factory { 84 public BenzCar createBenzCar(String car) throws Exception { 85 return new BenzSportCar(); 86 } 87 public BmwCar createBmwCar(String car) throws Exception { 88 return new BmwSportCar(); 89 } 90 public AudiCar createAudiCar(String car) throws Exception { 91 return new AudiSportCar(); 92 } 93 } 94 class RealFactory extends Factory { 95 public BenzCar createBenzCar(String car) throws Exception { 96 return new BenzBusinessCar(); 97 } 98 public BmwCar createBmwCar(String car) throws Exception { 99 return new BmwBusinessCar(); 100 } 101 public AudiCar createAudiCar(String car) throws Exception { 102 return new AudiBusinessCar(); 103 } 104 } 105 106 //老板 107 public class AbstractFactory { 108 109 public static void main(String[] args) throws Exception { 110 111 Factory d = new RealFactory(); 112 AudiCar car = d.createAudiCar(""); 113 car.drive(); 114 } 115 }
其中:BenzSportCar和BenzBusinessCar属于产品树;同理BmwSportCar和BmwBusinessCar。而BenzSportCar和BmwSportCar和AudiSportCar属于产品族。
1 abstract class BenzCar{ 2 private String name; 3 4 public abstract void drive(); 5 6 public String getName() { 7 return name; 8 } 9 public void setName(String name) { 10 this.name = name; 11 } 12 } 13 class BenzSportCar extends BenzCar{ 14 public void drive(){ 15 System.out.println(this.getName()+"----BenzSportCar-----------------------"); 16 } 17 } 18 class BenzBusinessCar extends BenzCar{ 19 public void drive(){ 20 System.out.println(this.getName()+"----BenzBusinessCar-----------------------"); 21 } 22 } 23 24 abstract class BmwCar{ 25 private String name; 26 27 public abstract void drive(); 28 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 } 36 class BmwSportCar extends BmwCar{ 37 public void drive(){ 38 System.out.println(this.getName()+"----BmwSportCar-----------------------"); 39 } 40 } 41 class BmwBusinessCar extends BmwCar{ 42 public void drive(){ 43 System.out.println(this.getName()+"----BmwBusinessCar-----------------------"); 44 } 45 } 46 47 abstract class AudiCar{ 48 private String name; 49 50 public abstract void drive(); 51 52 public String getName() { 53 return name; 54 } 55 public void setName(String name) { 56 this.name = name; 57 } 58 } 59 class AudiSportCar extends AudiCar{ 60 public void drive(){ 61 System.out.println(this.getName()+"----AudiSportCar-----------------------"); 62 } 63 } 64 class AudiBusinessCar extends AudiCar{ 65 public void drive(){ 66 System.out.println(this.getName()+"----AudiBusinessCar-----------------------"); 67 } 68 } 69 70 71 /** 72 * 简单工厂通过反射改进抽象工厂及其子工厂 73 * @author Administrator 74 * 75 */ 76 class Driver3{ 77 public static BenzCar createBenzCar(String car) throws Exception { 78 return (BenzCar) Class.forName(car).newInstance(); 79 } 80 81 public static BmwCar createBmwCar(String car) throws Exception { 82 return (BmwCar) Class.forName(car).newInstance(); 83 } 84 85 public static AudiCar createAudiCar(String car) throws Exception { 86 return (AudiCar) Class.forName(car).newInstance(); 87 } 88 } 89 //客户端 90 public class SimpleAndAbstractFactory { 91 92 public static void main(String[] args) throws Exception { 93 94 AudiCar car = Driver3.createAudiCar("com.java.pattendesign.factory.AudiSportCar"); 95 car.drive(); 96 } 97 } 98