工厂模式
一、工厂方法模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
-
意图
定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。 -
主要解决
主要解决接口选择的问题。 -
何时使用
我们明确地计划不同条件下创建不同实例时。 -
使用场景
吃饭:大熊猫吃竹子、羊吃草、狗吃屎
实现
- 动物接口
public interface Animal{
void eat();
}
- 创建实现接口的实现类
public class Doy implements Animal{
@Override
public void eat() {
System.out.println("dog eat baba");
}
}
public class Panda implements Animal{
@Override
public void eat() {
System.out.println("Panda eat 竹子");
}
}
public class Sheep implements Animal{
@Override
public void eat() {
System.out.println("Sheep eat 草");
}
}
- 工厂类、用来创建具体动物对象
public class AnimalFactory {
//使用 getShape 方法获取形状类型的对象
public Animal createAnimal(String type){
if(type== null){
return null;
}
if(type.equalsIgnoreCase("dog")){
return new Dog();
} else if(type.equalsIgnoreCase("sheep")){
return new Sheep();
} else if(type.equalsIgnoreCase("panda")){
return new Panda();
}
return null;
}
}
- 调用
public class Client{
public static void main(String[] args) {
AnimalFactory animalFactory = new AnimalFactory();
//获取 Animal 的对象,并调用它的 eat方法
Animal animal1 = animalFactory.createAnimal("dog");
//调用 Circle 的 draw 方法
animal1.eat();
//获取 Animal 的对象,并调用它的 eat方法
Animal animal2 = animalFactory.createAnimal("sheep");
//调用 Rectangle 的 draw 方法
animal2.eat();
//获取 Animal 的对象,并调用它的 eat方法
Animal animal3 = animalFactory.createAnimal("panda");
//调用 Square 的 draw 方法
animal3.eat();
}
}
- 输出
dog eat baba
Sheep eat 草
Panda eat 竹子
二、抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
实现
- 创建动物、植物接口
public interface Animal{
void eat();
}
public interface ZhiWu{
void photosynthesis();
}
- 创建具体动物实现类
public class Doy implements Animal{
@Override
public void eat() {
System.out.println("dog eat baba");
}
}
public class Panda implements Animal{
@Override
public void eat() {
System.out.println("Panda eat 竹子");
}
}
public class Sheep implements Animal{
@Override
public void eat() {
System.out.println("Sheep eat 草");
}
}
- 具体植物实现类
public class Sunflower implements ZhiWu{
@Override
public void photosynthesis() {
System.out.println("Sunflower 光合作用");
}
}
- 动植物类工厂抽象类
// 动植物工厂的抽象类
public abstract class Factory {
}
- 动植物类工厂
//创建动物工厂
public class AnimalFactory extend Factory {
//使用 getShape 方法获取形状类型的对象
public Animal createAnimal(String type){
if(type== null){
return null;
}
if(type.equalsIgnoreCase("dog")){
return new Dog();
} else if(type.equalsIgnoreCase("sheep")){
return new Sheep();
} else if(type.equalsIgnoreCase("panda")){
return new Panda();
}
return null;
}
}
//创建植物工厂
public class ZhiWuFactory extend Factory{
//使用 getShape 方法获取形状类型的对象
public Animal createZhiWu(String type){
if(type== null){
return null;
}
if(type.equalsIgnoreCase("sunflower")){
return new Sunflower();
} else{
return null;
}
}
}
- 创建动植物工厂的工厂
public class ZhongZuFactory {
//使用 createFactroy 方法创建动植物工厂类
public Factory createFactroy(String type){
if(type== null){
return null;
}
if(type.equalsIgnoreCase("animal")){
return new AnimalFactory();
} else if (type.equalsIgnoreCase("zhiwu") {
return new ZhiWuFactory();
}
}
}
- 调用
ZhongZuFactory.createFactroy("animal").createAnimal("dog").eat();
ZhongZuFactory.createFactroy("animal").createAnimal("sheep").eat();
ZhongZuFactory.createFactroy("zhiwu").createAnimal("sunflower").photosynthesis();
- 输出
dog eat baba
Sheep eat 草
Sunflower 光合作用