标签:
来源: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 }
标签:
原文地址:http://www.cnblogs.com/erbing/p/5802425.html