抽象工厂模式,对比简单工厂模式,工厂方法模式


1、抽象工厂模式定义

接着上文说的工厂方法模式,由于创建的子类产品越来越多,不仅有手机,还有电脑,耳机等其他产品时,难道每意见产品都需要创建一个工厂子类嘛?这显然是不合理的,因此引入抽象工厂方法模式—对产品类进行分组,组内不同产品对应同一个工厂类的不同方法。
定义:为一组相关联的产品创建一个接口,定义各种产品,而无需关注实现。

2、抽象工厂模式代码举例

首先看一下产品类的代码,手机和电脑是两个抽象接口,分别拥有高端和低端两个实现类:

/**
 * 电脑接口
 */
public interface Computer {
    
    
    public void create();
}

public class HigeComputer implements  Computer{
    
    
    @Override
    public void create() {
    
    
        System.out.println("创建高端电脑--------");
    }
}

public class LowComputer implements Computer {
    
    
    @Override
    public void create() {
    
    
        System.out.println("创建低端电脑--------");
    }
}


/**
 * 手机接口
 */
public interface MobilePhone {
    
    
    public void create();
}

public class HigeMobilePhone implements MobilePhone {
    
    
    @Override
    public void create() {
    
    
        System.out.println("创建高端手机--------");
    }
}

public class LowMobilePhone implements MobilePhone {
    
    
    @Override
    public void create() {
    
    
        System.out.println("创建低端手机--------");
    }
}

接下来是工厂类,由于产品分成了高端和低端两大组,工厂也相应分成了高端工厂和低端工厂,各自负责组内产品的创建:

/**
 * 工厂接口
 */
public interface Factory {
    
    

    Computer iCreateComputer();
    MobilePhone iCreateMobilePhone();
}

/**
 * 低端工厂
 */
public class LowFactory implements Factory{
    
    
    @Override
    public Computer iCreateComputer() {
    
    
        Computer lowComputer = new LowComputer();
        return lowComputer;
    }

    @Override
    public MobilePhone iCreateMobilePhone() {
    
    
        MobilePhone lowMobilePhone = new LowMobilePhone();
        return lowMobilePhone;
    }
}


/**
 * 高端工厂
 */
public class HigeFactory implements Factory{
    
    
    @Override
    public Computer iCreateComputer() {
    
    
        HigeComputer higeComputer = new HigeComputer();
        return higeComputer;
    }

    @Override
    public MobilePhone iCreateMobilePhone() {
    
    
        HigeMobilePhone higeMobilePhone = new HigeMobilePhone();
        return higeMobilePhone;
    }
}


最后是客户端代码,通过实例化不同的工厂子类,然后调用不同的方法,创建不同的产品

/**
 * 客户端测试类
 */
public class AbstractFactoryTest {
    
    
    public static void main(String[] args) {
    
    
        HigeFactory higeFactory = new HigeFactory();
        LowFactory lowFactory = new LowFactory();

        Computer higeComputer = higeFactory.iCreateComputer();
        Computer lowComputer = lowFactory.iCreateComputer();

        MobilePhone highMobilePhone = higeFactory.iCreateMobilePhone();
        MobilePhone lowMobilePhone = lowFactory.iCreateMobilePhone();

       higeComputer.create();
       lowComputer.create();
       highMobilePhone.create();
       lowMobilePhone.create();
    }
}

3、抽象工厂模式优缺点

优点:客户端只需要调用抽象工厂的接口来创建实例,并不需要关心具体的实例创建过程。

缺点:如果新增或删掉产品实例,就需要修改抽象接口和具体的产品条线

4、抽象工厂模式使用场景

系统中有多条产品线,同时多条产品线下的多个产品符合同一规则,就可以尝试使用抽象工厂模式构建

5、抽象工厂模式对比简单工厂模式,工厂方法模式的区别

简单工厂模式
简单工厂模式只有一个工厂类,而在工厂类中通过if-else判断或switch语句(违背了开放-封闭规则),来决定最终创建的产品对象实例。

工厂方法模式
工厂方法模式解决了简单工厂模式中的工厂类的条件判断问题,以产品性质划分出多个工厂类来实现工厂接口,利用多态创建不同的产品对象实例。

抽象工厂模式
抽象工厂模式解决了工厂方法模式中如果存在多个产品的问题,将产品子类进行分组,以产品条线为维度创建工厂类,同一个工厂类中的不同方法负责创建不同的产品。

猜你喜欢

转载自blog.csdn.net/m0_37900506/article/details/114486907