來源:http://www.bjsxt.com/ 一、【GOF23設計模式】_簡單工廠模式詳解、面向對象設計原則、開閉原則、依賴反轉原則、迪米特法則 沒有工廠模式的情況 簡單工廠模式的情況 或者 二、【GOF23設計模式】_工廠方法模式詳解 三、【GOF23設計模式】_抽象工廠模式詳解 ...
來源:http://www.bjsxt.com/
一、【GOF23設計模式】_簡單工廠模式詳解、面向對象設計原則、開閉原則、依賴反轉原則、迪米特法則
沒有工廠模式的情況
1 package com.test.factory.simple; 2 3 public interface Car { 4 public void run(); 5 } 6 7 package com.test.factory.simple; 8 9 public class Audi implements Car{ 10 @Override 11 public void run() { 12 System.out.println("奧迪在跑"); 13 } 14 } 15 16 package com.test.factory.simple; 17 18 public class Byd implements Car{ 19 @Override 20 public void run() { 21 System.out.println("比亞迪在跑"); 22 } 23 }
1 package com.test.factory.simple; 2 /** 3 * 測試沒有工廠模式的情況下 4 */ 5 public class Client01 { //調用者 依賴Audi、Byd 6 public static void main(String[] args) { 7 Car c1 = new Audi(); 8 Car c2 = new Byd(); 9 10 c1.run(); 11 c2.run(); 12 } 13 }
簡單工廠模式的情況
1 package com.test.factory.simple; 2 3 public class CarFactory { 4 public static Car createCar(String type){ 5 if("奧迪".equals(type)){ 6 return new Audi(); 7 }else if("比亞迪".equals(type)){ 8 return new Byd(); 9 }else{ 10 return null; 11 } 12 } 13 }
1 package com.test.factory.simple; 2 /** 3 * 簡單工廠情況下 4 */ 5 public class Client02 { //調用者 不依賴Audi、Byd 6 public static void main(String[] args) { 7 Car c1 = CarFactory.createCar("奧迪"); 8 Car c2 = CarFactory.createCar("比亞迪"); 9 10 c1.run(); 11 c2.run(); 12 } 13 }
或者
1 package com.test.factory.simple; 2 3 public class CarFactory2 { 4 public static Car createAudi(){ 5 return new Audi(); 6 } 7 public static Car createByd(){ 8 return new Byd(); 9 } 10 }
1 package com.test.factory.simple; 2 /** 3 * 簡單工廠情況下 4 */ 5 public class Client03 { //調用者 不依賴Audi、Byd 6 public static void main(String[] args) { 7 Car c1 = CarFactory2.createAudi(); 8 Car c2 = CarFactory2.createByd(); 9 10 c1.run(); 11 c2.run(); 12 } 13 }
二、【GOF23設計模式】_工廠方法模式詳解
1 package com.test.factory.factorymethod; 2 3 public interface Car { 4 public void run(); 5 } 6 7 package com.test.factory.factorymethod; 8 9 public class Audi implements Car{ 10 @Override 11 public void run() { 12 System.out.println("奧迪在跑"); 13 } 14 } 15 16 package com.test.factory.factorymethod; 17 18 public class Byd implements Car{ 19 @Override 20 public void run() { 21 System.out.println("比亞迪在跑"); 22 } 23 }
1 package com.test.factory.factorymethod; 2 3 public interface CarFactory { 4 Car createCar(); 5 } 6 7 package com.test.factory.factorymethod; 8 9 public class AudiFactory implements CarFactory{ 10 @Override 11 public Car createCar() { 12 return new Audi(); 13 } 14 } 15 16 package com.test.factory.factorymethod; 17 18 public class BydFactory implements CarFactory{ 19 @Override 20 public Car createCar() { 21 return new Byd(); 22 } 23 }
1 package com.test.factory.factorymethod; 2 3 public class Client { 4 public static void main(String[] args) { 5 Car c1 = new AudiFactory().createCar(); 6 Car c2 = new BydFactory().createCar(); 7 8 c1.run(); 9 c2.run(); 10 } 11 }
三、【GOF23設計模式】_抽象工廠模式詳解
1 package com.test.factory.abstractfactory; 2 3 public interface Engine { 4 void run(); 5 void start(); 6 } 7 8 class LuxuryEngine implements Engine{ 9 @Override 10 public void run() { 11 System.out.println("轉得快"); 12 } 13 14 @Override 15 public void start() { 16 System.out.println("啟動快!可以自動啟停"); 17 } 18 } 19 20 class LowEngine implements Engine{ 21 @Override 22 public void run() { 23 System.out.println("轉得慢"); 24 } 25 26 @Override 27 public void start() { 28 System.out.println("啟動慢!"); 29 } 30 }
1 package com.test.factory.abstractfactory; 2 3 public interface Seat { 4 void massage(); 5 } 6 7 class LuxurySeat implements Seat{ 8 @Override 9 public void massage() { 10 System.out.println("可以自動按摩"); 11 } 12 } 13 14 class LowSeat implements Seat{ 15 @Override 16 public void massage() { 17 System.out.println("不能按摩"); 18 } 19 }
1 package com.test.factory.abstractfactory; 2 3 public interface Tyre { 4 void revolve(); 5 } 6 7 class LuxuryTyre implements Tyre{ 8 @Override 9 public void revolve() { 10 System.out.println("旋轉不磨損"); 11 } 12 } 13 14 class LowTyre implements Tyre{ 15 @Override 16 public void revolve() { 17 System.out.println("旋轉磨損快"); 18 } 19 }
1 package com.test.factory.abstractfactory; 2 3 public interface CarFactory { 4 Engine createEngine(); 5 Seat createSeat(); 6 Tyre createTyre(); 7 }
1 package com.test.factory.abstractfactory; 2 3 public class LuxuryFactory implements CarFactory{ 4 5 @Override 6 public Engine createEngine() { 7 return new LuxuryEngine(); 8 } 9 10 @Override 11 public Seat createSeat() { 12 return new LuxurySeat(); 13 } 14 15 @Override 16 public Tyre createTyre() { 17 return new LuxuryTyre(); 18 } 19 }
1 package com.test.factory.abstractfactory; 2 3 public class LowFactory implements CarFactory{ 4 5 @Override 6 public Engine createEngine() { 7 return new LowEngine(); 8 } 9 10 @Override 11 public Seat createSeat() { 12 return new LowSeat(); 13 } 14 15 @Override 16 public Tyre createTyre() { 17 return new LowTyre(); 18 } 19 }
1 package com.test.factory.abstractfactory; 2 3 public class Client { 4 public static void main(String[] args) { 5 CarFactory factory = new LuxuryFactory(); 6 Engine e = factory.createEngine(); 7 e.run(); 8 e.start(); 9 } 10 }