设计模式 之 工厂模式

1.简单工厂模式,只是介绍使用,实际不实用

package com.test.pattern;

/**
 * @author Ash
 * @date: 2016年8月8日 下午12:54:35 
 * @func: 简单工厂模式
 * @email 408657544@qq.com
 * @Copyright: 2016 Ash. All rights reserved.
 */
//产品类
abstract class BMW1 {
    public BMW1() {}
}
class BMW1320 extends BMW1 {
    public BMW1320() {
        System.out.println("制造 --> BMW320");
    }
}
class BMW1523 extends BMW1 {
    public BMW1523() {
        System.out.println("制造 --> BMW523");
    }
}
//工厂类
class Factory {
    public BMW1 createBMW1(int type) {
        switch (type) {
        case 1320:
            return new BMW1320();
        case 1523:
            return new BMW1523();
        default:
            return null;
        }
    }
}
//客户类
public class Customer1 {
    public static void main(String[] args) {
        Factory factory = new Factory();
        BMW1 bmw1320 = factory.createBMW1(1320);
    }
}

2.工厂方法模式

package com.test.pattern;

/**
 * @author Ash
 * @date: 2016年8月8日 下午1:07:40 
 * @func: 工厂方法模式
 * @email 408657544@qq.com
 * @Copyright: 2016 Ash. All rights reserved.
 */
//产品类
abstract class BMW2 {
    public BMW2() {}
}
class BMW2320 extends BMW2 {
    public BMW2320() {
        System.out.println("制造 --> BMW320");
    }
}
class BMW2523 extends BMW2 {
    public BMW2523() {
        System.out.println("制造 --> BMW2523");
    }
}
//工厂类
interface FactoryBMW2 {
    BMW2 creaBMW2();
}
class FactoryBMW2320 implements FactoryBMW2 {

    @Override
    public BMW2 creaBMW2() {
        return new BMW2320();
    }
    
}
class FactoryBMW2523 implements FactoryBMW2 {

    @Override
    public BMW2 creaBMW2() {
        return new BMW2523();
    }
}
//客户类
public class Custormer2 {
    public static void main(String[] args) {
        FactoryBMW2 factory = new FactoryBMW2320();
        factory.creaBMW2();
    }
}

3.抽象工厂模式

package com.test.pattern;

/**
 * @author Ash
 * @date: 2016年8月8日 下午1:25:07 
 * @func: 抽象工厂模式 
 * @email 408657544@qq.com
 * @Copyright: 2016 Ash. All rights reserved.
 */
//发动机
interface Engine {}
class EngineA implements Engine {
    public EngineA() {
        System.out.println("制造 EngineA");
    }
}
class EngineB implements Engine {
    public EngineB() {
        System.out.println("制造EngineB");
    }
}
//空调
interface Airconditioner {}
class AirconditionerA implements Airconditioner{
    public AirconditionerA() {
        System.out.println("制造AirconditionerA");
    }
}
class AirconditionerB implements Airconditioner{
    public AirconditionerB() {
        System.out.println("制造AirconditionerB");
    }
}
//工厂类
interface AbstractFactory {
    public Engine createEngine();
    public Airconditioner createAirconditioner();
}
class FactoryBMW3320 implements AbstractFactory{

    @Override
    public Engine createEngine() {
        return new EngineA();
    }

    @Override
    public Airconditioner createAirconditioner() {
        return new AirconditionerA();
    }
}
class FactoryBMW3523 implements AbstractFactory {

    @Override
    public Engine createEngine() {
        return new EngineB();
    }

    @Override
    public Airconditioner createAirconditioner() {
        return new AirconditionerB();
    }
}
public class Customer3 {
    public static void main(String[] args) {
        AbstractFactory factory = new FactoryBMW3320();
        factory.createAirconditioner();
        factory.createEngine();
    }
}

抽象工厂模式是工厂方法模式的升级版,蛋实际上我觉得没有多大区别,只不过前者的工厂类中有多个生产不同产品的方法而已

转载于:https://www.cnblogs.com/heben/p/5749003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值