【设计模式】抽象工厂模式

【设计模式】抽象工厂模式,第1张

抽象工厂模式

  抽象工厂模式是对工厂的抽象化,而不只是制造方法。我们知道,为满足多样化的需求,工厂不会只限于生产一类产品,但是系统如果按照工厂方法那样为每种产品新增一个工厂又会造成工厂泛滥。所以为了调和这种矛盾,抽象工厂模式提供了另一种思路:将各类产品分别门类,基于此来规划各种工厂的制造借口,最终确立产品制造的顶级规范,使其与具体产品彻底脱钩。抽象工厂是建立在制造复杂产品体系需求基础之上的一种设计模式,在某种意义上来讲,我们可以将抽象工厂模式理解为工厂方法模式的高度集群化升级版。

下面拿汽车生产举例:

现在有两家汽车公司,分别是丰田Toyota与本田Honda,他们旗下都有轿车生产线与跑车生产线,所以我们可以抽象出两个产品抽象类,分别是轿车Car类与跑车SportCar类:

public abstract class Car {
    private double price;
    private String name;

    public Car(double price, String name) {
        this.price = price;
        this.name = name;
    }

    public abstract void run();
}



public abstract class SportCar {
    private double price;
    private String name;

    public SportCar(double price, String name) {
        this.price = price;
        this.name = name;
    }

    public abstract void run();
}

丰田旗下的轿车有卡罗拉Carola,跑车有牛魔王Supar,本田旗下有轿车雅阁Accord,跑车NSX,所以这两个轿车继承抽象类Car,跑车继承抽象类SportCar

public class Carola extends Car {
    public Carola(double price, String name) {
        super(price, name);
    }

    @Override
    public void run() {
        System.out.println("Carola is running");
    }
}




public class Accord extends Car {
    public Accord(double price, String name) {
        super(price, name);
    }

    @Override
    public void run() {
        System.out.println("Accord is running");
    }
}
public class Supar extends SportCar {
    public Supar(double price, String name) {
        super(price, name);
    }

    @Override
    public void run() {
        System.out.println("Supar is running");
    }
}


public class NSX extends SportCar {
    public NSX(double price, String name) {
        super(price, name);
    }

    @Override
    public void run() {
        System.out.println("NSX is running");
    }
}

丰田与本田都有自己的汽车工厂,并且都有轿车与跑车两条生产线,这样我们就可以抽象出一个汽车生产工厂,这个工厂有两条生产线:

public interface VehicleFactory {
    public Car createCar(double price, String name);
    public SportCar createSportCar(double price, String name);
}

汽车工厂告诉丰田与本田的怎么生产轿车与跑车,接着两个品牌的汽车工厂创建相应车型的生产线:

public class ToyotaFactory implements VehicleFactory {
    @Override
    public Car createCar(double price, String name) {
        return new Carola(price, name);
    }

    @Override
    public SportCar createSportCar(double price, String name) {
        return new Supar(price, name);
    }
}



public class HondaFactory implements VehicleFactory {
    @Override
    public Car createCar(double price, String name) {
        return new Accord(price, name);
    }

    @Override
    public SportCar createSportCar(double price, String name) {
        return new NSX(price, name);
    }
}

最后,客户提车只需要到相应品牌的汽车工厂直接提车就可以了:

public class Client {
    public static void main(String[] args) {
        VehicleFactory Toyota = new ToyotaFactory();
        Toyota.createCar(90000, "Carola").run();
        Toyota.createSportCar(600000, "Supar").run();
        VehicleFactory Honda = new HondaFactory();
        Honda.createCar(80000, "Accord").run();
        Honda.createSportCar(700000, "NSX").run();
    }
}

执行结果

Carola is running
Supar is running
Accord is running
NSX is running

这是抽象工厂模式案例的类图:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/721220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存