DesignPattern_Java:Factory Method Pattern

工厂方法模式 Factory Method :(虚拟构造函数模式 Virtual Constructor,多态性工厂模式 Ploymorphic Facoty)

Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses.

定义一个创建产品对象的工厂接口。将实际创建性工作推迟到子类中。

抽象工厂角色(Creator):该角色是工厂方法模式的核心。与应用系统无关,不论什么在创建对象的工厂类必须实现这个接口。

package com.DesignPattern.Creational.FactoryMethod;

public interface Creator {
        //工厂方法
        //创建一个产品对象,其输入參数类型能够自行设置
	public <T extends Product> T factory(Class<T> c);
}

详细工厂角色(Concrete Creator):该角色实现了抽象工厂接口。含有与应用密切相关的逻辑。而且受到应用程序的调用以创建产品对象。

package com.DesignPattern.Creational.FactoryMethod;

public class ConcreteCreator implements Creator {

	@Override
	public <T extends Product> T factory(Class<T> c) {
		Product product = null;
		try {
			product = (Product) Class.forName(c.getName()).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (T) product;
	}

}

抽象产品角色(Product):该角色负责定义产品的共性。实现对产品最抽象的定义。

package com.DesignPattern.Creational.FactoryMethod;

public interface Product {

	//产品类的公共方法
	public void method1();
	public void method2();
}

详细产品角色(Concrete Product):该角色实现抽象产品角色所声明的接口,工厂方法模式所创建的每个对象都是某个详细产品角色的实例。

package com.DesignPattern.Creational.FactoryMethod;

public class ConcreteProduct implements Product {

	@Override
	public void method1() {
		System.out.println("handler ...");
		//业务逻辑处理代码
	}

	@Override
	public void method2() {
		System.out.println("handler ...");
		//业务逻辑处理代码
	}

}

应用代码FactoryMethodDemo.java

package com.DesignPattern.Creational.FactoryMethod;

public class FactoryMethodDemo {

	public static void main(String[] args) {
		Creator creator = new ConcreteCreator();
		Product product = creator.factory(ConcreteProduct.class);
		/*
		 * 继续业务处理
		 */
	}
}

工厂方法模式的实例

抽象工厂FruitGardener.java

package com.DesignPattern.Creational.FactoryMethod;

public interface FruitGardener {

	public Fruit factory();
}

抽象产品Fruit.java

package com.DesignPattern.Creational.FactoryMethod;

public interface Fruit {

	//生长
	public void grow();
	//收获
	public void harvest();
	//栽种
	public void plant();
}

详细工厂FruitAppleGardener.java


package com.DesignPattern.Creational.FactoryMethod;

public class FruitAppleGardener implements FruitGardener {

	@Override
	public Fruit factory() {
		return new FruitApple();
	}

}

详细工厂FruitGrapeGardener.java

package com.DesignPattern.Creational.FactoryMethod;

public class FruitGrapeGardener implements FruitGardener {

	@Override
	public Fruit factory() {
		return new FruitGrape();
	}

}


详细产品FruitApple.java

package com.DesignPattern.Creational.FactoryMethod;

public class FruitApple implements Fruit {

	private int treeAge;

	public int getTreeAge() {
		return treeAge;
	}

	public void setTreeAge(int treeAge) {
		this.treeAge = treeAge;
	}

	@Override
	public void grow() {
		System.out.println("apple grow...");
	}

	@Override
	public void harvest() {
		System.out.println("apple harvest...");
	}

	@Override
	public void plant() {
		System.out.println("apple plant...");
	}

}


详细产品FruitGrape.java

package com.DesignPattern.Creational.FactoryMethod;

public class FruitGrape implements Fruit {

	private boolean seedless;

	public boolean isSeedless() {
		return seedless;
	}

	public void setSeedless(boolean seedless) {
		this.seedless = seedless;
	}

	@Override
	public void grow() {
		System.out.println("Grape grow...");
	}

	@Override
	public void harvest() {
		System.out.println("Grape harvest...");
	}

	@Override
	public void plant() {
		System.out.println("Grape plant...");
	}

}

FruitClientDemo.java

package com.DesignPattern.Creational.FactoryMethod;

public class FruitClientDemo {

	public static void main(String[] args) {
		// 苹果园丁工厂
		FruitGardener fruitGardener = new FruitAppleGardener();
		// 通过工厂生产苹果
		Fruit apple = fruitGardener.factory();
		apple.plant();
		apple.grow();
		apple.harvest();
		// 葡萄园丁工厂
		fruitGardener = new FruitGrapeGardener();
		// 通过工厂生产葡萄
		Fruit grape = fruitGardener.factory();
		grape.plant();
		grape.grow();
		grape.harvest();
	}
}



posted @ 2017-06-23 19:30  yxysuanfa  阅读(160)  评论(0编辑  收藏  举报