设计模式--工厂方法模式

设计模式--工厂方法模式


工厂方法模式是在简单工厂模式上都优化,不用再去写Factoty 接口,新建一个appleFactory 即可, 实现开放(可以扩展)封闭(不需要再去改原先都代码)原则:

具体看代码:

基类:

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


共同接口

package FactoryMethod;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年2月7日    Create this file
 * </pre>
 * 
 */

public interface FruitFactory {
    public Fruit getFruit();
}

实现香蕉工厂

package FactoryMethod;


/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年2月7日    Create this file
 * </pre>
 * 
 */

public class BananaFactory implements FruitFactory{

    /**
     * @see FactoryMethod.FruitFactory#getFruit()
     */
    @Override
    public Fruit getFruit() {
        return (Fruit) new Banana();
    }

}

package FactoryMethod;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年2月7日    Create this file
 * </pre>
 * 
 */

public class Banana implements Fruit{

    /**
     * @see FactoryMethod.Fruit#get()
     */
    @Override
    public void get() {
        System.out.println("采集香蕉");
    }

}
同理香蕉 梨 都一样:


package FactoryMethod;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年2月7日    Create this file
 * </pre>
 * 
 */

public class AppleFactory implements FruitFactory {
    

    /**
     * @see FactoryMethod.FruitFactory#getFruit()
     */
    @Override
    public  Fruit getFruit() {
        return (Fruit) new Apple();
    }

}


package FactoryMethod;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年2月7日    Create this file
 * </pre>
 * 
 */

public class Apple  implements Fruit{

       /* 
        * 实现开放封闭,创建产品都工厂接口,将实际工作放到子类中
        */
    public void get(){
        System.out.println("采集苹果");
    }
   
}


测试类:

public class MainClass {
	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();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiaoming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值