设计模式二(工厂模式)

本文介绍了简单工厂模式、工厂方法模式和抽象工厂模式在Java中的使用,强调了它们如何通过工厂类实现对象的实例化,遵循开闭原则和依赖倒置原则,以及它们在扩展性和代码维护性方面的优势和局限性。
摘要由CSDN通过智能技术生成

本质:实例化对象不用new,用工厂代替,实现了创建者和调用者分离

满足:

开闭原则:对拓展开放,对修改关闭

依赖倒置原则:要针对接口编程

迪米特原则:最少了解原则,只与自己直接相关的类有关系

简单工厂模式

也被称为静态工厂

public interface Car {
    void name();
}
public class BWM implements Car{
​
    @Override
    public void name() {
        System.out.println("宝马");
    }
}
public class DaZhong implements Car{
​
    @Override
    public void name() {
        System.out.println("大众");
    }
}

public class CarFactory{
    public static Car getCar(String name){
        if(name.equals("大众")){
            return new DaZhong();
        }else if (name.equals("宝马")){
            return new BWM();
        }else{
            return null;
        }
    }
}
public class consumer {
    public static void main(String[] args) {
​
        Car car = CarFactory.getCar("大众");
        car.name();
        Car car2 = CarFactory.getCar("宝马");
        car2.name();
    }
}

总结

将创建对象的任务交给工厂去完成

缺点

不满足开闭原则,如果我们新创建一个车,就需要修改CarFactory的源代码

工厂方法模式

多个工厂对应多个实现类

public interface CarFactory {
​
    Car getCar();
}
​


public class BMWFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new BWM();
    }
}
​
public class DaZhongFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new DaZhong();
    }
}

如果我们想要创建新的车对象,只要创建对应的车工厂即可,无需修改CarFactory的代码

public class Aodi implements Car {
    @Override
    public void name() {
        System.out.println("奥迪");
    }
}

public class AodiFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Aodi();
    }
}

public class consumer {
    public static void main(String[] args) {
        Car car = new DaZhongFactory().getCar();
        car.name();
        Car car1 = new BMWFactory().getCar();
        car1.name();

        Car car2 = new AodiFactory().getCar();
        car2.name();
    }
}

抽象工厂模式

围绕一个超级工厂生产工厂,该工厂又称为其他工厂的工厂 (抽象的抽象)

public interface IProductFactory {

    IPhoneProduct iphoneproduct();

    IRouterProduct irouterproduct();
}

具体的产品工厂

public class XiaomiFactory implements IProductFactory{
    @Override
    public IPhoneProduct iphoneproduct() {
        return new XiaomiPhone();
    }

    @Override
    public IRouterProduct irouterproduct() {
        return new XiaomiRouter();
    }
}
public class HuaweiFactory implements IProductFactory{
    @Override
    public IPhoneProduct iphoneproduct() {
        return new HuaweiPhone();
    }

    @Override
    public IRouterProduct irouterproduct() {
        return new HuaweiRouter();
    }
}

产品功能

public interface IPhoneProduct {

    void open();

    void close();
}
public interface IRouterProduct {

    void open();
    void close();
    
}

具体实现

public class XiaomiPhone implements IPhoneProduct{
    @Override
    public void open() {
        System.out.println("小米手机开机");
    }

    @Override
    public void close() {
        System.out.println("小米手机关机");
    }
}
public class HuaweiPhone implements IPhoneProduct{
    @Override
    public void open() {
        System.out.println("华为手机开机");
    }

    @Override
    public void close() {
        System.out.println("华为手机关机");
    }
}
public class XiaomiRouter implements IRouterProduct{
    @Override
    public void open() {
        System.out.println("小米路由器开机");
    }

    @Override
    public void close() {
        System.out.println("小米路由器关机");
    }
}
public class HuaweiRouter implements IRouterProduct{
    @Override
    public void open() {
        System.out.println("华为路由器开机");
    }

    @Override
    public void close() {
        System.out.println("华为路由器关机");
    }
}

测试

public class consumer {

    public static void main(String[] args) {
        //先创建工厂
        System.out.println("==========小米==========");
        IPhoneProduct product = new XiaomiFactory().iphoneproduct();
        product.open();
        product.close();
        IRouterProduct irouterproduct = new XiaomiFactory().irouterproduct();
        irouterproduct.open();
        irouterproduct.close();
        System.out.println("===========华为==========");
        IPhoneProduct iphoneproduct = new HuaweiFactory().iphoneproduct();
        iphoneproduct.open();
        iphoneproduct.close();
        IRouterProduct irouterproduct1 = new HuaweiFactory().irouterproduct();
        irouterproduct1.open();
        irouterproduct1.close();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值