目录
1.面向对象设计的基本原则
2.简单工厂模式
3.工厂方法模式
4.抽象工厂模式
工厂设计模式的核心本质:
1、实例化对象,用工厂方法代替new操作。
2、将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。
一、面向对象设计的基本原则
1.OCP(开闭原则,Open-Closed Principle):一个软件的实体应当对扩展开放,对修改关闭。
2.DIP(依赖倒转原则,Dependence Inversion Principle):要针对接口编程不要针对实现编程。
3.LoD(迪米特法则,Law of Demeter):只与你直接的朋友通信,而避免和陌生人通信。
二、简单工厂模式
如果不使用工厂设计模式来创建对象,那么我们的程序将与多个类打交道。但是如果我们使用工厂设计模式的话,程序则不会与多个类有交集。
一下是不使用工厂设计模式类的关系图与Demo:
package com.demo.simpleFactory; /** *动物接口 */ public interface Animal { void sleep(); }
1 package com.demo.simpleFactory; 2 3 public class Cat implements Animal{ 4 5 @Override 6 public void sleep() { 7 System.out.println("猫在睡觉"); 8 } 9 10 }
1 package com.demo.simpleFactory; 2 3 public class Dog implements Animal{ 4 5 @Override 6 public void sleep() { 7 System.out.println("狗在睡觉"); 8 } 9 10 }
1 package com.demo.simpleFactory; 2 3 public class Client { 4 public static void main(String[] args) { 5 Animal a = new Dog(); 6 Animal a1 = new Cat(); 7 a.sleep(); 8 a1.sleep(); 9 } 10 }
使用简单工厂模式则是创建一个工厂,使用者使用的时候只需要在工厂类中拿对象。
1 package com.demo.simpleFactory; 2 3 /** 4 * 工厂类 5 * @author Administrator 6 * 7 */ 8 public class SimpleFactory { 9 public static Animal createAnimal(String animal){ 10 if("狗".equals(animal)){ 11 return new Dog(); 12 }else if("猫".equals(animal)){ 13 return new Cat(); 14 }else{ 15 return null; 16 } 17 } 18 }
另一种写法:
1 package com.demo.simpleFactory; 2 3 /** 4 * 工厂类 5 * @author Administrator 6 * 7 */ 8 public class SimpleFactory { 9 public static Animal createDog(){ 10 return new Dog(); 11 } 12 public static Animal createCat(){ 13 return new Cat(); 14 } 15 }
使用者则可以通过如下方法创建对象:
package com.demo.simpleFactory; /** * 使用者通过调用工厂创建对象 * @author Administrator * */ public class Client { public static void main(String[] args) { Animal a1 = SimpleFactory.createAnimal("狗"); Animal a2 = SimpleFactory.createAnimal("猫"); a1.sleep(); a2.sleep(); } }
使用工厂模式创建对象的类依赖图如下:
三、工厂方法模式
在以上的工厂模式中,设计原理是违反面向对象基本原则的OCP原则的,我们需要将重新设计这个工厂方法模式。
以下是重新设计的工厂模式图解与Demo:
1 package com.demo.simpleFactory; 2 /** 3 * 工厂方法模式抽象类 4 * @author Administrator 5 * 6 */ 7 public interface AnimalFactory { 8 Animal createAnimal(); 9 }
1 package com.demo.simpleFactory; 2 3 4 public class CatFactory implements AnimalFactory{ 5 6 @Override 7 public Animal createAnimal() { 8 return new Cat(); 9 } 10 11 }
1 package com.demo.simpleFactory; 2 3 public class Dog implements Animal{ 4 5 @Override 6 public void sleep() { 7 System.out.println("狗在睡觉"); 8 } 9 10 }
1 package com.demo.simpleFactory; 2 /** 3 * 使用者通过调用工厂创建对象 4 * @author Administrator 5 * 6 */ 7 public class Client { 8 public static void main(String[] args) { 9 Animal cat = new CatFactory().createAnimal(); 10 Animal dog = new DogFactory().createAnimal(); 11 cat.sleep(); 12 dog.sleep(); 13 } 14 }
四、抽象工厂模式
抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式。
作用:用来生产不同产品族的全部产品,不能增加新的产品,但支持产品族。
抽象工厂模式的简单实现:
软件包结构如下:
package com.demo.abstractFactory.spare; /** * 引擎接口 * @author Administrator * */ public interface Engine { void run(); }
package com.demo.abstractFactory.spare; /** * 座椅接口 * @author Administrator * */ public interface Seat { void feel(); }
package com.demo.abstractFactory.spare; /** * 轮胎接口 * @author Administrator * */ public interface Tyre { void quality(); }
package com.demo.abstractFactory.factory; import com.demo.abstractFactory.spare.Engine; import com.demo.abstractFactory.spare.Seat; import com.demo.abstractFactory.spare.Tyre; /** * 汽车工厂 * @author Administrator * */ public interface CarFactory { Engine createEngine(); Seat createSeat(); Tyre createTyre(); }
package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Engine; public class LowEngine implements Engine{ @Override public void run() { System.out.println("汽车跑得慢"); } } package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Seat; public class LowSeat implements Seat{ @Override public void feel() { System.out.println("座椅没按摩功能"); } } package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Tyre; public class LowTyre implements Tyre{ @Override public void quality() { System.out.println("轮胎易磨损"); } } package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Engine; public class LuxuryEngine implements Engine{ @Override public void run() { System.out.println("汽车跑得快"); } } package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Seat; public class LuxurySeat implements Seat{ @Override public void feel() { System.out.println("座椅有按摩功能"); } } package com.demo.abstractFactory.product; import com.demo.abstractFactory.spare.Tyre; public class LuxuryTyre implements Tyre{ @Override public void quality() { System.out.println("轮胎耐磨损"); } }
package com.demo.abstractFactory.factory; import com.demo.abstractFactory.product.LuxuryEngine; import com.demo.abstractFactory.product.LuxurySeat; import com.demo.abstractFactory.product.LuxuryTyre; import com.demo.abstractFactory.spare.Engine; import com.demo.abstractFactory.spare.Seat; import com.demo.abstractFactory.spare.Tyre; public class LuxuryCarFactory implements CarFactory{ @Override public Engine createEngine() { return new LuxuryEngine(); } @Override public Seat createSeat() { return new LuxurySeat(); } @Override public Tyre createTyre() { return new LuxuryTyre(); } }
package com.demo.abstractFactory.factory; import com.demo.abstractFactory.product.LowEngine; import com.demo.abstractFactory.product.LowSeat; import com.demo.abstractFactory.product.LowTyre; import com.demo.abstractFactory.spare.Engine; import com.demo.abstractFactory.spare.Seat; import com.demo.abstractFactory.spare.Tyre; public class LowCarFactory implements CarFactory{ @Override public Engine createEngine() { return new LowEngine(); } @Override public Seat createSeat() { return new LowSeat(); } @Override public Tyre createTyre() { return new LowTyre(); } }
package com.demo.abstractFactory; import com.demo.abstractFactory.factory.CarFactory; import com.demo.abstractFactory.factory.LowCarFactory; import com.demo.abstractFactory.factory.LuxuryCarFactory; public class Client { public static void main(String[] args) { CarFactory lowCarFactory = new LowCarFactory(); CarFactory luxuryCarFactory = new LuxuryCarFactory(); lowCarFactory.createEngine().run(); lowCarFactory.createSeat().feel(); luxuryCarFactory.createTyre().quality(); } }