java设计模式创建篇------抽象工厂

开篇

这一篇我们介绍的抽象工厂,可以与上一篇java设计模式创建篇------工厂模式进行对比服用效果更佳。

核心思想

将工厂模式进一步进行抽象化,使其具有更广泛的抽象性与一般性,可以通过一种工厂模式来实现对多种抽象对象的创建。在创建时这些对象依赖于同一接口,我们不必关心抽象对象的具体类,而将这一过程交由具体出来的工厂进行控制。

相关实现

抽象产品

这是对多种产品之间的进一步抽象,使得产品之间的共性得以体现,通过对其一般性的提取来实现封装。

public abstract class Clothes {
    public abstract void color();
    public abstract void sex();
}

还是继续我们的服装厂大业,在之前我们只完成了三种颜色服装的设计,但是我们忽略了对于男女款式之间的设计。

//蓝的
public abstract class BlueClothes extends Clothes {
    @Override
    public void color() {
        System.out.println("blue");
    }
    public abstract void sex();
}
//绿的
public abstract class GreenClothes extends Clothes {
    @Override
    public void color() {
        System.out.println("green");
    }
    public abstract void sex();
}
//红的
public abstract class RedClothes extends Clothes {
    @Override
    public void color() {
        System.out.println("red");
    }
    public abstract void sex();
}

具体产品

每种颜色在分别对应男女款式,创建具体产品

public class FemaleBlueClothes extends BlueClothes{
    @Override
    public void sex() {
        System.out.println("女款蓝色");
    }
}
public class MaleBlueClothes extends BlueClothes{
    @Override
    public void sex() {
        System.out.println("男款蓝色");
    }
}
public class FemaleGreenClothes extends GreenClothes{
    @Override
    public void sex() {
        System.out.println("女款绿色");
    }
}
public class MaleGreenClothes extends GreenClothes{
    @Override
    public void sex() {
        System.out.println("男款绿色");
    }
}
public class FemaleRedClothes extends RedClothes{
    @Override
    public void sex() {
        System.out.println("女款红色");
    }
}
public class MaleRedClothes extends RedClothes{
    @Override
    public void sex() {
        System.out.println("男款红色");
    }
}

具体产品则是我们要生成的具体的目标类,我们通过封装将其特点与共性统一,之后通过继承的实现产生的多样具体的目标品种。除了使用抽象类以外还可以使用接口实现,具体的实现也很简单,大家可以根据需求选择,在这里就不在赘述了。

抽象工厂

这里的理解可以与产品的抽象进行对比,我们对整个工厂的操作使我们的产品逐步的由抽象转向具体,使生产的一般性到具体每个类的具体化生产,而在抽象工厂这一部分我们还是提取它们的共性,进行一些一般性的处理,并促使其逐步具体。我们先处理的是服装生产的颜色处理。

public abstract class ClothesFactory {
    public abstract Clothes createGreenClothes();
    public abstract Clothes createRedClothes();
    public abstract Clothes createBlueClothes();
}

具体工厂

这里的具体工厂是我们最后使用来创建实例的工厂,在这里我们的产品彻底的由抽象化变为具体。

//女款生产线
public class FemaleClothesFactory extends ClothesFactory {
    @Override
    public Clothes createGreenClothes() {
        return new FemaleGreenClothes();
    }

    @Override
    public Clothes createRedClothes() {
		return new FemaleRedClothes();
    }

    @Override
    public Clothes createBlueClothes() {
		return new FemaleBlueClothes();
    }
}
//男款生产线
public class MaleClothesFactory extends ClothesFactory {
    @Override
    public Clothes createGreenClothes() {
        return new MaleGreenClothes();
    }

    @Override
    public Clothes createRedClothes() {
		return new MaleRedClothes();
    }

    @Override
    public Clothes createBlueClothes() {
		return new MaleBlueClothes();
    }
}

实际生产

还是通过简答的使用我们来看看其具体运作。

public class Product{
    public static void main(String[]args) {
    	//女款生产线
        ClothesFactory femaleClothesFactory = new FemaleClothesFactory();
        //男款生产线
        ClothesFactory maleClothesFactory = new MaleClothesFactory();
        Clothes maleBlueClothes=maleClothesFactory.createBlueClothes();
        maleBlueClothes.sex();
        Clothes maleGreenClothes=maleClothesFactory.createGreenClothes();
        maleGreenClothes.sex();
        Clothes maleRedClothes=maleClothesFactory.createRedClothes();
        maleRedClothes.sex();
        Clothes femaleBlueClothes=femaleClothesFactory.createBlueClothes();
        femaleBlueClothes.sex();
        Clothes femaleGreenClothes=femaleClothesFactory.createGreenClothes();
        femaleGreenClothes.sex();
        Clothes femaleRedClothes=femaleClothesFactory.createRedClothes();
        femaleRedClothes.sex();
    }
}

特点

高度的封装性既是缺点又是优点,优点是给我们省了不少事,我们不必关心每个具体类的具体实现,而是只要关心生成产品的抽象接口就行了,而具体关心的是实现接口的具体工厂类,知道了类就知道了接口函数,就可以生成相应的目标实体。缺点也很明显,每当我们要增加一个新的类到工厂中去就得改很多东西,从抽象的实现到具体的生成,相关的函数都得进行修改,扩展性很差。大家可以根据项目需求进行使用。

原文地址:https://www.cnblogs.com/yanzs/p/13788287.html