2. 工厂方法模式

1.面对的问题

简单工厂模式当新增一个新的对象类型时,是需要修改工厂代码的,违反了开闭原则。

简单工厂模式:先写一个工厂类,再写一个抽象方法类,最后通过具体产品类创建产品

为了弥补这个问题,我们引入了工厂方法模式

 

2.工厂方法模式:定义一个用于创建对象的接口,但是让子类决定将哪一个类实例化。工厂方法模式让一个类的实例化延迟到其子类

先定义一个抽象的工厂类,再定义具体的工厂类来生产产品,这些产品实现了抽象工厂类中声明的方法。

当需要新增一个类型,只需要新增一个具体的工厂类就可以了。

工厂方法模式和简单工厂模式区别:工厂方法不再提供一个统一的工厂类创建所有产品对象,而是针对不同的产品提供不同的工厂

 

3.工厂方法模式结构

工厂方法模式提供了一个抽象工厂接口来声明抽象工厂方法,而由其子类来具体实现工厂方法,创建具体的产品对象

1)product(抽象产品)

它是定义产品的接口,是工厂方法模式所创建对象的超类型,也就是产品对象的公共父类

2)ConcreteProduct(具体产品)

它实现了抽象产品接口,某种类型的具体产品由专门的具体工厂创建,具体工厂和具体产品一一对应

3)Factory(抽象工厂)

在抽象工厂类中声明了工厂方法,用于返回一个产品抽象工厂是工厂方法模式的核心,所有创建对象的工厂类都必须实现该接口

4)ConcreteFactory(具体工厂)

它是抽象工厂的子类,实现了在抽象工厂中声明的工厂方法,并可由客户端调用,返回一个具体产品类的实例 

 

(具体产品需要实现抽象产品)

 

4.工厂方法模式的实现

1)先创建抽象产品类

package controller.factoryModule;

/**
 *抽象产品
 */
public interface Product {
    public void getProduct();
}

 

2)创建具体产品类A、B

package controller.factoryModule;


public class ConcreteProductA implements Product {

    @Override
    public void getProduct() {
        System.out.println("具体产品A");
    }
}
package controller.factoryModule;


public class ConcreteProductB implements Product {

    @Override
    public void getProduct() {
        System.out.println("具体产品B");
    }
}

 

3)创建抽象工厂类

package controller.factoryModule;

import controller.factoryModule.Product;

public interface Factory {
    public Product factoryMethod();
}

 

4)具体工厂A、B

package controller.factoryModule;

public class ConcreteFactoryA implements Factory {
    @Override
    public ConcreteProductA factoryMethod() {
        return new ConcreteProductA();
    }
}
package controller.factoryModule;

public class ConcreteFactoryB implements Factory {
    @Override
    public ConcreteProductB factoryMethod() {
        return new ConcreteProductB();
    }
}

 

5)客户端调用

package controller.factoryModule;

public class Client {
    public static void main(String[] args) {
        Factory factory;
        factory = new ConcreteFactoryA();

        Product product;
        product = factory.factoryMethod();
        product.getProduct();

    }
}

 

5.反射机制与配置文件

设计模式上述已经讲完,但是我们在代码里new了工厂,在生产时随着环境的切换,我们需要通过反射+配置文件来创建而不是通过new

1)反射创建类

//根据类名获取类
Class A = Class.forName("java.lang.String");
Object obj  = A.newInstance();

 

6.工厂方法的重载

工厂接口可以给出同样的接口名但是不一样的参数来实现重载

 

7.工厂模式优缺点

优:

弥补了简单工厂的新增产品类型需要动工厂代码的问题

 

缺:

增加复杂性

 

 

  • 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
发出的红包

打赏作者

鹏哥哥啊Aaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值