简单工厂模式 工厂方法模式 抽象工厂模式

简单工厂模式

产品类的共同接口



public interface Product {
    //声明类所需继承的共同接口,也可以是抽象类
}

产品类A


/**
 * 
 * @author CIACs
 *
 */
public class ProductA implements Product {
    public ProductA() {
        System.out.println("ProductA");
    }
}

产品类B

public class ProductB implements Product {
    public ProductB() {
        System.out.println("ProductB");
    }
}

工厂类

public class Factory {
    //可以在工厂类中添加任何你所需要的逻辑
    public static Product create(String str)
    {
        //生成ProductA
        if(str.equalsIgnoreCase("ProductA"))
        {
            return new ProductA();
        }
        else
            //生成ProductB
            if(str.equalsIgnoreCase("ProductB"))
            {
                return new ProductB();
            }
        return null;
    }

}

客户端

public class Client {
    public static void main(String[] args) {
        //调用Factory的静态方法生成所要的类
        Factory.create("productA");
        Factory.create("ProductB");
    }
}

结果:

工厂方法模式



public class ProductC implements Product {
    public ProductC() {
        System.out.println("productC");
    }
}

声明工厂接口


public interface Factory {
    //声明产生产品类的方法
    public Product createProduct();
}

产生ProductA的FactoryA


public class FactoryA implements Factory {
    //实现工厂类的方法生成产品类A
    public Product createProduct()
    {
        return new ProductA();
    }

}

产生ProductB的FactoryB

public class FactoryB implements Factory {
    //实现工厂类的方法生成产品类B
    public Product createProduct()
    {
        return new ProductB();
    }
}

产生ProductC的FactoryC


public class FactoryC implements Factory {
    //实现工厂类的方法生成产品类C
    public Product createProduct()
    {
        return new ProductC();
    }
}

客户端


public class Client {
    public static void main(String[] args) {
        Factory factory;
        factory = new FactoryA();
        factory.createProduct();
        factory = new FactoryB();
        factory.createProduct();
        factory = new FactoryC();
        factory.createProduct();
    }
}

结果:

工厂方法模式中我们把生成产品类的时间延迟,就是通过对应的工厂类来生成对应的产品类,在这里我们就可以实现“开发-封闭”原则,无论加多少产品类,我们都不用修改原来类中的代码,而是通过增加工厂类来实现。但是这还是有缺点的,如果产品类过多,我们就要生成很多的工厂类。假如我们要实现的产品接口不止一个,也就是有多个产品接口,不同产品接口有对应的产品族。什么是产品族呢?简单的理解就是,不同牌子产的车里面会有跑车类型,家庭类型,商用类型等的车,不同牌子的车的跑车类型的车可以组成一个产品族。对于这种情况我们可以采用抽象工厂模式。

抽象工厂模式

增加的Gift接口


public interface Gift {
    //声明产品赠品的接口,当然也可以是抽象类,同样为了简单就不声明方法了
}

GiftA类


public class GiftA implements Gift {
    public GiftA()
    {
        System.out.println("GiftA");
    }
}

GiftB类


public class GiftB implements Gift {
    public GiftB()
    {
        System.out.println("GiftB");
    }
}

Factory接口


/**
 * 
 * 
 *声明Product类工厂和Gift类工厂的工同工厂接口
 */
public interface Factory {
    public Product createProduct();
    public Gift createGift();

}

生成ProductA和GiftA的FactoryA


/**
 * 
 * 
 *FactoryA可以生成ProductA和GiftA
 */
public class FactoryA implements Factory {
    @Override
    public Product createProduct()
    {
        return new ProductA();
    }
    @Override
    public Gift createGift()
    {
        return new GiftA();
    }
}

生成ProductB和GiftB的FactoryB


/**
 * 
 * 
 *FactoryB可以生成ProductB和GiftB
 */
public class FactoryB implements Factory {
    @Override
    public Product createProduct() {
        return new ProductB();
    }
    @Override
    public Gift createGift() {
        return new GiftB();
    }

}

客户端


public class Client {
    public static void main(String[] args) {
        Factory factory;
        factory = new FactoryA();
        factory.createProduct();
        factory.createGift();
        factory = new FactoryB();
        factory.createProduct();
        factory.createGift();
    }
}

抽象工厂模式中我们可以定义实现不止一个接口,一个工厂也可以生成不止一个产品类,抽象工厂模式较好的实现了“开放-封闭”原则,是三个模式中较为抽象,并具一般性的模式。我们在使用中要注意使用抽象工厂模式的条件。

git:https://github.com/limeng6868/factory.git

猜你喜欢

转载自blog.csdn.net/weixin_40403930/article/details/88312999