`

设计模式回顾之十:工厂方法模式(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容器里面就大量使用了工厂模式。另外,由于现在依赖注入概念和容器的普及,需要工厂模式实现的工作其实都可以交给容器去处理了。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics