2.工厂方法模式

1、是什么--概念

      工厂方法模式,又称工厂模式、多态工厂模式和虚拟构造器模式,通过定义工厂父类负责定义创建对象的公共接口,而子类则负责生成具体的对象。

      是一种常用的类创建型设计模式,此模式的核心精神是封装类中变化的部分,提取其中个性化善变的部分为独立类,通过依赖注入以达到解耦、复用和方便后期维护拓展的目的。

2、为什么--产生动机

    根据简单工厂模式可以看出,不同品牌的汽车是由不同的工厂生产的,貌似又是很完美的。但大家看一下测试类,当一个人想要去买一辆宝马汽车的时候(假设没有销售商),那么他就要去找宝马工厂给他生产一辆,过几天又想要买一辆奔驰汽车的时候,又得跑到奔驰工厂请人生产,这无疑就增加了用户的操作复杂性。所以有没有一种方便用户操作的方法呢?这个时候抽象工厂模式就出现了。

3、怎么做--功能说明和代码实现 

工厂方法模式组成:
         1)抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在java中它由抽象类或者接口来实现。
         2)具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
         3)抽象产品角色:它是具体产品继承的父类或者是实现的接口。在java中一般有抽象类或者接口来实现。
         4)具体产品角色:具体工厂角色所创建的对象就是此角色的实例。在java中由具体的类来实现。

业务逻辑:

业务描述: 

(1)苹果,香蕉,鸭梨属于具体的产品;

(2)苹果,香蕉等具体水果抽象出一个产品,是它们都属于水果;

(3)苹果工厂等属于具体工厂角色,用来指定生产的水果;

(4)同样,将不同的工厂抽象出一个水果工厂,所有的工厂都继承该工厂;

具体实现参照github库源码,地址如下:

https://github.com/zhaoyan1990/designModel

4、解决问题

      当一个类无法预料要创建哪种类的对象或是一个类需要由子类来指定,创建的对象时,就需要用到工厂模式。       

      工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。

5、使用场景

         (1)消费者不关心它所要创建对象的类(产品类)的时候。

         (2)消费者知道它所要创建对象的类(产品类),但不关心如何创建的时候。

例如:hibernate里通过sessionFactory创建session、通过代理方式生成ws客户端时,通过工厂构建报文中格式化数据的对象。

6、总结

           这种工厂方法,如果后期还有新的具体产品,只需要继承水果这个类(抽象产品),然后定义新的工厂, 同样是实现FactoryFruit这个接口(工厂接口),生产具体的产品就ok了。这样就符合开闭原则,对扩展开发,修改关闭。 而简单工厂方法:如果后期有新的产品,产品同样可以继承Fruit这个类(抽象产品), 但是,如果要得到这个具体的产品时,需要在原来工厂类中修改代码,添加一个新产品的方法, 这样就修改了原来的代码,这就不符合开闭原则了。

      当一个人想要苹果的时候,那么他就要去找苹果工厂给他生产一个,过几天又想要一个香蕉的时候,又得跑到香蕉工厂请人生产,这无疑就增加了用户的操作复杂性。所以有没有一种方便用户操作的方法呢?这个时候抽象工厂模式就出现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为人工智能,我无法编写和运行Java代码。但是,我可以提供一些关于创建型设计模式的解释和示例,帮助你更好地理解这些概念。 1. 简单工厂模式 简单工厂模式是一种通过一个工厂类来创建不同类型对象的设计模式。在这种模式中,客户端只需要知道所需产品的名称或类型,而不需要知道具体的实现细节。 示例代码: ``` public class AnimalFactory { public static Animal createAnimal(String type) { if (type.equals("dog")) { return new Dog(); } else if (type.equals("cat")) { return new Cat(); } else { return null; } } } public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public class Main { public static void main(String[] args) { Animal animal1 = AnimalFactory.createAnimal("dog"); Animal animal2 = AnimalFactory.createAnimal("cat"); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 2. 工厂方法模式 工厂方法模式是一种将对象的创建委托给子类的设计模式。在这种模式中,客户端只需要知道所需产品的抽象类型,而具体的实现由子类来决定。 示例代码: ``` public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public abstract class AnimalFactory { public abstract Animal createAnimal(); } public class DogFactory extends AnimalFactory { public Animal createAnimal() { return new Dog(); } } public class CatFactory extends AnimalFactory { public Animal createAnimal() { return new Cat(); } } public class Main { public static void main(String[] args) { AnimalFactory factory1 = new DogFactory(); Animal animal1 = factory1.createAnimal(); AnimalFactory factory2 = new CatFactory(); Animal animal2 = factory2.createAnimal(); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 3. 抽象工厂模式 抽象工厂模式是一种提供一个接口来创建一系列相关或相互依赖对象的设计模式。在这种模式中,客户端通过调用工厂接口的方法来创建不同类型的对象。 示例代码: ``` public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public interface AnimalFactory { public Animal createAnimal(); } public class DomesticAnimalFactory implements AnimalFactory { public Animal createAnimal() { return new Dog(); } } public class WildAnimalFactory implements AnimalFactory { public Animal createAnimal() { return new Cat(); } } public class Main { public static void main(String[] args) { AnimalFactory factory1 = new DomesticAnimalFactory(); Animal animal1 = factory1.createAnimal(); AnimalFactory factory2 = new WildAnimalFactory(); Animal animal2 = factory2.createAnimal(); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 4. 单例模式 单例模式是一种保证一个类只有一个实例,并提供全局访问点的设计模式。在这种模式中,构造函数是私有的,只能通过静态方法来获取实例。 示例代码: ``` public class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } public class Main { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2); // Output: true } } ``` 5. 原型模式 原型模式是一种通过复制现有对象来创建新对象的设计模式。在这种模式中,通过调用对象的克隆方法来创建新对象。 示例代码: ``` public class Sheep implements Cloneable { private String name; public Sheep(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public Sheep clone() { try { return (Sheep) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } } public class Main { public static void main(String[] args) { Sheep sheep1 = new Sheep("Dolly"); Sheep sheep2 = sheep1.clone(); sheep2.setName("Molly"); System.out.println(sheep1.getName()); // Output: Dolly System.out.println(sheep2.getName()); // Output: Molly } } ``` 6. 建造者模式 建造者模式是一种将复杂对象的构建过程分离出来的设计模式。在这种模式中,通过调用建造者类的方法来构建对象,可以灵活地定制对象的组成部分。 示例代码: ``` public class Computer { private String cpu; private String gpu; private String memory; private String storage; public Computer(String cpu, String gpu, String memory, String storage) { this.cpu = cpu; this.gpu = gpu; this.memory = memory; this.storage = storage; } public String getCpu() { return cpu; } public String getGpu() { return gpu; } public String getMemory() { return memory; } public String getStorage() { return storage; } } public interface ComputerBuilder { public void setCpu(String cpu); public void setGpu(String gpu); public void setMemory(String memory); public void setStorage(String storage); public Computer build(); } public class GamingComputerBuilder implements ComputerBuilder { private String cpu; private String gpu; private String memory; private String storage; public void setCpu(String cpu) { this.cpu = cpu; } public void setGpu(String gpu) { this.gpu = gpu; } public void setMemory(String memory) { this.memory = memory; } public void setStorage(String storage) { this.storage = storage; } public Computer build() { return new Computer(cpu, gpu, memory, storage); } } public class OfficeComputerBuilder implements ComputerBuilder { private String cpu; private String memory; private String storage; public void setCpu(String cpu) { this.cpu = cpu; } public void setGpu(String gpu) {} public void setMemory(String memory) { this.memory = memory; } public void setStorage(String storage) { this.storage = storage; } public Computer build() { return new Computer(cpu, null, memory, storage); } } public class ComputerDirector { private ComputerBuilder builder; public void setBuilder(ComputerBuilder builder) { this.builder = builder; } public Computer buildComputer() { builder.setCpu("Intel i7"); builder.setMemory("16GB"); builder.setStorage("512GB SSD"); return builder.build(); } } public class Main { public static void main(String[] args) { ComputerDirector director = new ComputerDirector(); ComputerBuilder builder1 = new GamingComputerBuilder(); ComputerBuilder builder2 = new OfficeComputerBuilder(); director.setBuilder(builder1); Computer gamingComputer = director.buildComputer(); director.setBuilder(builder2); Computer officeComputer = director.buildComputer(); System.out.println(gamingComputer.getCpu()); // Output: Intel i7 System.out.println(gamingComputer.getGpu()); // Output: NVIDIA GTX System.out.println(officeComputer.getCpu()); // Output: Intel i7 System.out.println(officeComputer.getGpu()); // Output: null } } ``` 以上是关于六种创建型设计模式的简要介绍和示例代码。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值