设计模式笔记_02_工厂方法模式

package com.maom.design.D_02_FactoryMethod;

/**
 * @Title: D_02_FactoryMethod
 * @Description: 02_工厂方法模式
 * @Author maohb
 * @Version 1.0
 */
public class D_02_FactoryMethod {

    public static void main(String[] args) {

        //获得AppleFactory
        FruitFactory ff = new AppleFactory();
        //通过AppleFactory来获得Apple实例对象
        Fruit apple = ff.getFruit();
        apple.get();

        //获得BananaFactory
        FruitFactory ff2 = new BananaFactory();
        Fruit banana = ff2.getFruit();
        banana.get();

        //获得PearFactory
        FruitFactory ff3 = new PearFactory();
        Fruit pear = ff3.getFruit();
        pear.get();

    }

}


interface Fruit {
    /* 采集 */
    public void get();
}

interface FruitFactory {
    public Fruit getFruit();
}


class Banana implements Fruit {
    public void get(){
        System.out.println("采集香蕉");
    }
}

class BananaFactory implements FruitFactory {
    public Fruit getFruit() {
        return new Banana();
    }
}


class Apple implements Fruit {
    public void get(){
        System.out.println("采集苹果");
    }
}

class AppleFactory implements FruitFactory {
    public Fruit getFruit() {
        return new Apple();
    }
}

class Pear implements Fruit {
    public void get() {
        System.out.println("采集梨子");
    }
}

class PearFactory implements FruitFactory {
    public Fruit getFruit() {
        return new Pear();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值