工厂模式和抽象工厂模式

了解抽象工厂模式前,我们先来了解两个概念。

一种是元素;

一种是所含元素组成的成品;

比如我们的衣服 组成衣服的元素是布料和颜色。

布料和颜色就是元素,衣服是元素组成的成品。

同样裤子也是由布料和颜色组成的。

那么衣服和裤子就相当于两个不同的成品。

在普通工厂模式中,需要更多关注的是元素的工厂;

在抽象工厂中,需要关注的是成品的工厂。

下面我们先来看看普通工厂模式的代码。

首先来定义下两个元素颜色和布料的接口。

package com.yourcompany.shotTest;

public interface Color {
    public void whatColor();
}

package com.yourcompany.shotTest;

public interface Cloth {
    public void whatCloth();
}

裤子的布料和颜色

package com.yourcompany.shotTest;

public class PantsCloth implements Cloth {
    String cloth="";
    public PantsCloth(String cloth){
        this.cloth=cloth;
    }
    @Override
    public void whatCloth() {
        System.out.print("裤子的料子是"+cloth);
    }
}


package com.yourcompany.shotTest;

public class PantsColor implements Color {
    String color="";
    public  PantsColor(String color){
        this.color=color;
    }

    @Override
    public void whatColor() {
        System.out.print("裤子的颜色"+color);
    }
}

衣服的布料和颜色

package com.yourcompany.shotTest;

public class ClothesCloth implements Cloth {

    String cloth="";

    public ClothesCloth(String cloth){
        this.cloth=cloth;
    }
    @Override
    public void whatCloth() {
        System.out.print("衣服的料子是"+cloth);
    }
}


package com.yourcompany.shotTest;

public class ClothesColor implements Color {
    String color="";
    public ClothesColor(String color){
        this.color=color;
    }
    @Override
    public void whatColor() {
        System.out.print("衣服的颜色"+color);
    }
}


普通工厂

接下来我们定义一个颜色的生产制造厂


package com.yourcompany.shotTest;

public class ColorFactory {

    public static Color creatColor(int type,String mcolor){
        Color color=null;
        if (type==1){
            color=new ClothesColor(mcolor);
        }else {
            color=new PantsColor(mcolor);
        }
        return color;
    }
}

然后定义一个布料的生产制造厂

package com.yourcompany.shotTest;

public class ClothFactory {

    public static Cloth creatCloth(int type,String mcloth){
        Cloth cloth=null;
        if(type==1){
            cloth=new ClothesCloth(mcloth);
        }else {
            cloth=new PantsCloth(mcloth);
        }
        return cloth;
    }
}

定义一个制造方法

package com.yourcompany.shotTest;

public class nomalIwant {
    Color color;
    Cloth cloth;

    public void creat(int type,String color,String cloth){
        this.color=ColorFactory.creatColor(type,color);
        this.cloth=ClothFactory.creatCloth(type,cloth);
        this.color.whatColor();
        this.cloth.whatCloth();
    }

}

OK 我们现在测试一下

      nomalIwant iwant=new nomalIwant();
        iwant.creat(1,"蓝色","牛仔-------");

Log衣服的颜色蓝色衣服的料子是牛仔-------

抽象工厂

接下来我们定义一个抽象的衣裤制造厂

package com.yourcompany.shotTest;

public interface AbFactory {
    public Cloth setCloth(String cloth);

    public Color setColor(String color);
}


制造衣服的工厂类

package com.yourcompany.shotTest;

public class ClothesFactory implements AbFactory {
    @Override
    public Cloth setCloth(String cloth) {
        return new ClothesCloth(cloth);
    }

    @Override
    public Color setColor(String color) {
        return new ClothesColor(color);
    }
}



制造裤子的工厂类

package com.yourcompany.shotTest;

public class PantsFactory implements AbFactory {
    @Override
    public Cloth setCloth(String cloth) {
        return new PantsCloth(cloth);
    }

    @Override
    public Color setColor(String color) {
        return new PantsColor(color);
    }
}


接下来我们就该说出我们想要什么了

package com.yourcompany.shotTest;

public class Iwant {
    Color color;
    Cloth cloth;

    public void want(AbFactory fc,String color,String cloth){

       this.cloth= fc.setCloth(cloth);
        this.color=fc.setColor(color);
        this.color.whatColor();
        this.cloth.whatCloth();
    }
}


Test

package com.yourcompany.shotTest;

public class tesst {
    public static void main(String []args){

        Iwant i=new Iwant();
        i.want(new PantsFactory(),"红色","亚麻");
    }
}


这里就是我想要条红色亚麻的裤子

log: 裤子的颜色红色裤子的料子是亚麻

到现在这里,我们可以看出两种工厂模式并没有什么区别,但是如果给我们的衣服裤子再加一种元素,比如拉链。

那么我们的普通工厂模式中,还需要加一个拉链的工厂,而抽象工厂模式中只需要加一个拉链的方法就可以。

所以我们可以总结一下如何运用他们的优势和劣势:

1.如果组成元素大于元素组成的成品的数量,那么我们抽象工厂模式会更简单。

2.如果组成元素小于元素组成的成品的数量,那么我们用普通工厂模式会更简单,比如生产衣服和裤子,但是我们只需要一个布料生产厂就够了的话,那么我们用普通的工厂生产,更加简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值