设计模式-工厂模式

简单工厂模式

别名:静态工厂模式,静态工厂方法模式

简单工厂模式是创建/获取型模式,它关注对象创建或提供。

它可以解耦及减少代码冗余,当增加或修改某些实现的时候可以只增加/修改实现类和工厂类。

假如你现在要举行宴会,按照不同的客人分发不同的礼物
先看类图
Class Diagram
这里Gift就是抽象的礼物接口

public interface Gift {
    String getName();
}

Belt, Lipstick, Candy 是具体的实现

public class Belt implements Gift {
    @Override
    public String getName() {
        return "皮带";
    }
}
public class Lipstick implements Gift {
    @Override
    public String getName() {
        return "口红";
    }
}
public class Candy implements Gift {
    @Override
    public String getName() {
        return "糖果";
    }
}

我们使用一个简单工厂封装分发礼物的复杂性

public class GiftFactory {

    public static Gift allotGift(String type) {
        switch (type) {
            case "Child":
                return new Candy();
            case "Man":
                return new Belt();
            case "Woman":
                return new Lipstick();
            default:
                throw new RuntimeException("No gift for " + type);
        }
    }
}

接下来我们就可以快乐的发礼物了

public class AllotGifts {
    public static void main(String[] args) {
        System.out.println("现在开始分发礼物");
        System.out.println("首先是一位男士");
        System.out.println("分给他一件" + GiftFactory.allotGift("Man").getName());
        System.out.println("然后是一位女士");
        System.out.println("分给她一件" + GiftFactory.allotGift("Woman").getName());
        System.out.println("还有一个孩子");
        System.out.println("分给他一件" + GiftFactory.allotGift("Child").getName());
    }
}

结果就是这样
result
这个时候如果想对老人赠送特殊的礼物,只需要增加一个Gift的实现类并修改工厂类就可以了。
简单工厂如果需要扩展的话,不仅需要新增实现,还要修改工厂类,这在一定程度上违背了开闭原则。

工厂方法模式

现实中的宴会当然会有很多男士、女士和孩子,这个时候我们也可以考虑工厂方法模式,它更加符合开闭原则。
首先要有一个抽象的工厂接口

public interface IFactory {
    Gift newGift();
}

然后实现各自的工厂

public class BeltFactory implements IFactory {
    @Override
    public Gift newGift() {
        return new Belt();
    }
}

public class LipstickFactory implements IFactory {
    @Override
    public Gift newGift() {
        return new Lipstick();
    }
}

发放礼物的时候就可以这样

public class AllotGifts {
    public static void main(String[] args) {
        System.out.println("现在给男士发礼物");
        IFactory factory = new BeltFactory();
        System.out.println("首先是第一位男士");
        System.out.println("分给他一件" + factory.newGift().getName());
        System.out.println("第二位男士");
        System.out.println("分给他一件" + factory.newGift().getName());

        System.out.println("现在给女士发礼物");
        IFactory anotherFactory = new LipstickFactory();
        System.out.println("首先是第一位女士");
        System.out.println("分给她一件" + anotherFactory.newGift().getName());
        System.out.println("第二位女士");
        System.out.println("分给她一件" + anotherFactory.newGift().getName());
    }
}

抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

class diagram
例如现在的手机厂商大多还会生产平板电脑
先定义手机和平板

public abstract class Mobile {
    abstract String getBrand();

    @Override
    public String toString() {
        return getBrand()+"的手机";
    }
}

public abstract class Pad {
    abstract String getBrand();

    @Override
    public String toString() {
        return getBrand()+"的平板";
    }
}

根据品牌不同又分为华为的和苹果的

public class AppleMobile extends Mobile {
    @Override
    String getBrand() {
        return "苹果";
    }
}

public class ApplePad extends Pad {
    @Override
    String getBrand() {
        return "苹果";
    }
}

public class HuaweiMobile extends Mobile {
    @Override
    String getBrand() {
        return "华为";
    }
}

public class HuaweiPad extends Pad {
    @Override
    String getBrand() {
        return "华为";
    }
}

定义抽象的工厂

public interface DeviceFactory {
    Mobile getMobile();
    Pad getPad();
}

具体的工厂实现

public class AppleDeviceFactory implements DeviceFactory {
    @Override
    public Mobile getMobile() {
        return new AppleMobile();
    }

    @Override
    public Pad getPad() {
        return new ApplePad();
    }
}

public class HuaweiDeviceFactory implements DeviceFactory {
    @Override
    public Mobile getMobile() {
        return new HuaweiMobile();
    }

    @Override
    public Pad getPad() {
        return new HuaweiPad();
    }
}

生产设备

public class ProduceDevice {
    public static void main(String[] args) {
        DeviceFactory factory = new AppleDeviceFactory();
        System.out.println(factory.getMobile());
        System.out.println(factory.getPad());

        factory = new HuaweiDeviceFactory();
        System.out.println(factory.getMobile());
        System.out.println(factory.getPad());
    }
}

这样当换品牌生产的时候,只需要变更工厂实现,就可以改变所有的产品

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值