设计模式回顾之十:工厂方法模式(FactoryMethod)

设计模式回顾系列文章:主要针对工作中常用常见的设计模式进行整理、总结,同时分享以供大家拍砖。

------------------------------------------------

工厂方法模式(FactoryMethod)

定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。

适用于:

 

当一个类不知道它所必须创建的对象的类的时候,当一个类希望由它的子类来指定它所创建的对象的时候。 

 

程序实现:

接口:

public interface Fruit{
    public void grow();
    public void harvest();
    public void plant();
}

实现:

public class BigApple implements Fruit{
	private int treeAge;

	public void grow(){
		System.out.println("BigApple is growing!");
	}

	public void harvest(){
		System.out.println("BigApple has been harvested!");
	}

	public void plant(){
		System.out.println("BigApple has been planted!");
	}

	public int getTreeAge() {
		return treeAge;
	}

	public void setTreeAge(int treeAge) {
		this.treeAge = treeAge;
	}
	
}
public class BigOrange implements Fruit {
	private int number;

	public void grow() {
		System.out.println("BigOrange is growing!");
	}

	public void harvest() {
		System.out.println("BigOrange has been harvest!");
	}

	public void plant() {
		System.out.println("BigOrange has been harvest!");
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}
public class SmallApple implements Fruit{
	private int treeAge;

	public void grow(){
		System.out.println("SmallApple is growing!");
	}

	public void harvest(){
		System.out.println("SmallApple has been harvested!");
	}

	public void plant(){
		System.out.println("SmallApple has been planted!");
	}

	public int getTreeAge() {
		return treeAge;
	}

	public void setTreeAge(int treeAge) {
		this.treeAge = treeAge;
	}
	
}
public class SmallOrange implements Fruit {
	private int number;

	public void grow() {
		System.out.println("SmallOrange is growing!");
	}

	public void harvest() {
		System.out.println("SmallOrange has been harvest!");
	}

	public void plant() {
		System.out.println("SmallOrange has been harvest!");
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}

一共四种水果:大苹果、大橙子、小苹果、小橙子,下面是抽象水果工厂:

public abstract class FruitFactory {
	public abstract Fruit factory(String factory);
	
	public Fruit order(String type){
		Fruit fruit=factory(type);
		fruit.plant();
		fruit.grow();
		fruit.harvest();
		return fruit;
	}
}

具体水果工厂:

public class AppleFactory extends FruitFactory{
	public Fruit factory(String color){
		if("big".equals(color)){
			return new BigApple();
		}else if("small".equals(color)){
			return new SmallApple();
		}else{
			return null;
		}
	}
}
public class OrangeFactory extends FruitFactory{
	public Fruit factory(String type){
		if("big".equals(type)){
			return new BigOrange();
		}else if("small".equals(type)){
			return new SmallOrange();
		}else{
			return null;
		}
	}
}

最后是客户端调用代码:

public static void main(String[] args){
		FruitFactory appleFactory=new AppleFactory();
		FruitFactory orangeFactory=new OrangeFactory();
		
		Fruit fruit=appleFactory.order("big");
		System.out.println(fruit.getClass().getName()+"\n");
		
		fruit=appleFactory.order("small");
		System.out.println(fruit.getClass().getName()+"\n");
		
		fruit=orangeFactory.order("big");
		System.out.println(fruit.getClass().getName()+"\n");
		
		fruit=orangeFactory.order("small");
		System.out.println(fruit.getClass().getName()+"\n");
}

其实像IoC容器里面就大量使用了工厂模式。另外,由于现在依赖注入概念和容器的普及,需要工厂模式实现的工作其实都可以交给容器去处理了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值