简单理解工厂模式

一、简单工厂模式

提供一个工厂,负责对象的创建。当需要使用对象时,直接从工厂获取。

public interface Fruit {
    void get();
}
public class Apple implements Fruit{
    /*
    采集
     */
    public void get(){
        System.out.println("采集苹果");
    }

}


public class Banana implements Fruit{
    /*
    采集香蕉
     */
    public void get(){
        System.out.println("采集香蕉");
    }
}

public class FruitFactory {
    /*
    第一种写法
    // 获得Apple实例
    public static Fruit getApple(){
        return new Apple();
    }
  
    // 获得Banana实例
    public static Fruit getBanana(){
        return new Banana();
    }
    
     */
    
  
    public static Fruit getFruit(String type) throws IllegalAccessException, InstantiationException {
     /* 第二种写法
        if(type.equals("apple")){
            return Apple.class.newInstance();
        }else if(type.equals("banana")){
            return Banana.class.newInstance();
        }else {
            System.out.println("找不到实例");
            return null;
        }     
    */
    
    /* 第三种写法
        try {
            Class fruit = Class.forName(type);
            return (Fruit) fruit.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    */
    }
}
public class MainClass {
    public static void main(String args[]) throws InstantiationException, IllegalAccessException {
//        Fruit apple = new Apple();
//        apple.get();
//        Fruit banana = new Banana();
//        banana.get();

//        Fruit apple = FruitFactory.getApple();
//        Fruit banana = FruitFactory.getBanana();
//        apple.get();
//        banana.get();

        Fruit apple = FruitFactory.getFruit("Apple");
        Fruit banana = FruitFactory.getFruit("Banana");
        apple.get();
        banana.get();
    }
}

优点:工厂可以隐藏创建的对象的 “加工”细节 。当我们需要获取对象的时候,不需要自己实现对象的创建过程,避免了麻烦和难以维护的问题。
缺点:当对象类型很多时,简单工厂类很庞大臃肿、耦合性高。而且增加、删除某个子类对象的创建都需要修改简单工厂类代码,,不符合开闭原则。

二、工厂方法模式

public interface FruitFactory {
    /*

     */
    Fruit getFruit();
}

public class AppleFactory implements FruitFactory {

    public Fruit getFruit(){
        return new Apple();
    }
}
public class BananaFactory implements FruitFactory {
    public Fruit getFruit(){
        return new Banana();
    }
}

public class MainClass {
    public static void main(String args[]) throws InstantiationException, IllegalAccessException {
       // 获得AppleFactory
       FruitFactory applefactory = new AppleFactory();
       // 通过AppleFactory来获得Apple实例对象
       Fruit apple = applefactory.getFruit();
       apple.get();

        // 获得BananaFactory
        FruitFactory bananafactory = new BananaFactory();
        // 通过BananaFactory来获得Banana实例对象
        Fruit banana = bananafactory.getFruit();
        banana.get();
    }
}

工厂方法模式中一个子类对应一个工厂类。它把原来臃肿庞大的简单工厂类,拆分成了一个个的工厂类,对简单工厂模式进行了解耦。而这些工厂类都实现于一个抽象接口。
优点:增加功能只需新增类和相应工厂类,不需要修改代码。符合开闭原则。
缺点:增加较多的代码量。

三、抽象工厂模式

使用场景:提供一个工厂,创建一系列相关的或相互依赖的对象,而无需指定指定具体的类。
比如GreenHouseApple创建所有温室生产的水果,NorthFruitFactory 创建所有北方生产的水果…

public class GreenHouseApple extends Apple {
    public void get(){
        System.out.println("采集温室苹果");
    }
}

public class GreenHouseBanana extends Banana {
    public void get(){
        System.out.println("采集温室香蕉");
    }
}

public class GreenHouseFruitFactory implements FruitFactory {
    public Fruit getApple(){
        return new GreenHouseApple();
    }

    public Fruit getBanana(){
        return new GreenHouseBanana();
    }
}

public class NorthApple extends Apple {
    public void get(){
        System.out.println("采集北方苹果");
    }
}

public class NorthBanana extends Banana {
    public void get(){
        System.out.println("采集北方香蕉");
    }
}

public class NorthFruitFactory implements FruitFactory {
    public Fruit getApple(){
        return new NorthApple();
    }

    public Fruit getBanana(){
        return new NorthBanana();
    }
}

public class SouthApple extends Apple {
    public void get(){
        System.out.println("采集南方苹果");
    }
}

public class SouthBanana extends Banana {
    public void get(){
        System.out.println("采集南方香蕉");
    }
}

public class SouthFruitFactory implements FruitFactory {
    public Fruit getApple(){
        return new SouthApple();
    }

    public Fruit getBanana(){
        return new SouthBanana();
    }
}

public class MainClass {
    public static void main(String args[]){
        FruitFactory northFactory = new NorthFruitFactory();
        Fruit northApple = northFactory.getApple();
        northApple.get();
        Fruit northBanana = northFactory.getBanana();
        northBanana.get();

        FruitFactory southFactory = new SouthFruitFactory();
        Fruit southApple = southFactory.getApple();
        southApple.get();
        Fruit southBanana = southFactory.getBanana();
        southBanana.get();

        FruitFactory greenFactory = new GreenHouseFruitFactory();
        Fruit greenHouseApple = greenFactory.getApple();
        greenHouseApple.get();
        Fruit greenHouseBanana = greenFactory.getBanana();
        greenHouseBanana.get();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它通过一个工厂类来创建不同类型的对象,而无需暴露对象创建的逻辑给客户端。在Python中,简单工厂模式可以通过一个工厂类来创建不同的产品对象。下面是一个简单的示例: ```python class Product: def operation(self): pass class ConcreteProductA(Product): def operation(self): print("Performing operation A.") class ConcreteProductB(Product): def operation(self): print("Performing operation B.") class SimpleFactory: @staticmethod def create_product(product_type): if product_type == "A": return ConcreteProductA() elif product_type == "B": return ConcreteProductB() else: raise ValueError("Invalid product type.") # 使用简单工厂创建产品对象 factory = SimpleFactory() product_a = factory.create_product("A") product_a.operation() product_b = factory.create_product("B") product_b.operation() ``` 在上述示例中,`Product` 是一个抽象产品类,`ConcreteProductA` 和 `ConcreteProductB` 是具体产品类。`SimpleFactory` 是工厂类,通过 `create_product` 方法根据不同的产品类型创建相应的产品对象。 通过简单工厂模式,客户端无需知道具体的产品类,只需要通过工厂类来创建产品对象。这样可以降低客户端与具体产品类的耦合度,并且当需要新增产品时,只需要修改工厂类即可。 希望这个简单的示例能帮助你理解简单工厂模式在Python中的应用。如果有任何进一步的问题,请随时提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值