工厂方法模式
工厂方法模式(Factory Method Pattern),是简单工厂模式的扩展,其英文原话是"Define an interface for creating an object,but let the subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses",翻译是:定义一个创建对象的借口,但是让子类来觉得该实例化那个类。工厂方法让一个类推迟实例化至它的子类中。工厂方法模式有四个角色:
抽象工厂(Creator)角色:这是工厂方法模式的核心,定义了创建产品的方法。与应用系统无关。具体工厂角色必须实现这个接口才能创建对象。
具体工厂(Concrete Creator)角色:实现了抽象工厂角色的接口,含有与应用密切相关的逻辑,并且接受应用程序的调用以创建产品对象。
抽象产品(Product)角色:定义了产品的共性,实现对产品最抽象的定义。
具体产品(Concrete Product)角色:实现抽象产品角色的接口。工厂方法所创建的对象就是该角色的实例。
工厂方法模其类图如下:
抽象工厂的代码模式:
- public interface Creator{
- //这就是工厂方法,提供具体产品的类对象即可创建产品;
- public <T extends Product> T factory(Class<T> c);
- }
抽象产品的代码模式:
- public interface Product{
- //产品的公共方法
- public void method1();
- public void method2();
- }
具体工厂的代码模式:
- public class ConcreteCreator implements Creator{
- //具体工厂生产的具体产品
- Product product = null;
- @Override
- public <T extends Product> T factory(Class<T> c) {
- // TODO Auto-generated method stub
- //生产出产品
- product = (Product) Class.forName(c.getName()).newInstance();
- //或这样
- //product = c.newInstance();
- return (T)product;
- }
- }
具体产品的代码模式:
- public class ConcreteProduct implements Product{
- @Override
- public void method1() {
- //do somthing
- }
- @Override
- public void method2() {
- //do something
- }
- }
下面通过一个demo,来展示下工厂方法模式的应用;该例子是工厂生产水果的例子,首先需要一个抽象工厂Factory和抽象水果Fruit,Factory定义了生产水果的方法,Fruit定义了水果的共性;然后再由具体的工厂生产具体的水果,如AppleFactory(implements Factory)生产Apple(implements Fruit);GrapeFactory(implements Factory)生产Grape(implements Fruit),下面上代码。
Factory类:
- //抽象工厂,定义了生产产品的接口;
- public interface Factory {
- /**
- * @param c 具体产品的Class对象;
- * @return 返回产品的父类,具体工厂调用得到产品时需要转型;
- */
- public Fruit factory(Class c);
- }
Fruit类:
- //抽象产品,定义了产品的共性;
- public interface Fruit {
- public void plant();
- public void grow();
- public void harvest();
- }
AppleFactory类:
- //具体工厂,生产具体的产品,AppleFactory生产Apple
- public class AppleFactory implements Factory {
- @Override
- public Apple factory(Class c) {
- // TODO Auto-generated method stub
- try {
- //生产苹果
- Apple apple = (Apple)Class.forName(c.getName()).newInstance();
- //或这样 Apple apple = (Apple) c.newInstance();
- apple.setTreeAge(0);
- return apple;
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
GrapeFactory类:
- public class GrapeFactory implements Factory {
- @Override
- public Grape factory(Class c) {
- // TODO Auto-generated method stub
- try {
- Grape grape = (Grape)Class.forName(c.getName()).newInstance();
- //或这样 Grape grape = (Grape) c.newInstance();
- grape.setSeedless(true);
- grape.setTreeAge(0);
- return grape;
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
Apple类:
- //具体产品
- public class Apple implements Fruit {
- private int treeAge;
- @Override
- public void plant() {
- // TODO Auto-generated method stub
- System.out.println("Apple Tree is planted");
- }
- @Override
- public void grow() {
- // TODO Auto-generated method stub
- System.out.println("Apple Tree is growing");
- }
- @Override
- public void harvest() {
- // TODO Auto-generated method stub
- System.out.println("Apple Tree is harvesting");
- }
- /**
- * @return the treeAge
- */
- public int getTreeAge() {
- return treeAge;
- }
- /**
- * @param treeAge the treeAge to set
- */
- public void setTreeAge(int treeAge) {
- this.treeAge = treeAge;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return "Apple [treeAge=" + treeAge + "]";
- }
- }
Grape类:
- //具体产品
- public class Grape implements Fruit {
- boolean seedless;
- private int treeAge;
- @Override
- public void plant() {
- // TODO Auto-generated method stub
- System.out.println("Grape is palnted");
- }
- @Override
- public void grow() {
- // TODO Auto-generated method stub
- System.out.println("Grape is growing");
- }
- @Override
- public void harvest() {
- // TODO Auto-generated method stub
- System.out.println("Grape is harvesting");
- }
- /**
- * @return the seedless
- */
- public boolean isSeedless() {
- return seedless;
- }
- /**
- * @return the treeAge
- */
- public int getTreeAge() {
- return treeAge;
- }
- /**
- * @param seedless the seedless to set
- */
- public void setSeedless(boolean seedless) {
- this.seedless = seedless;
- }
- /**
- * @param treeAge the treeAge to set
- */
- public void setTreeAge(int treeAge) {
- this.treeAge = treeAge;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return "Grape [seedless=" + seedless + ", treeAge=" + treeAge + "]";
- }
- }
主方法入口类FactoryMethodTest:
- public class FactoryMethodTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //生产产品的工厂,以父类引用标示;或者也可以分别定义GrapeFactory和AppleFactory,这样fac.factory()这句就不用转型了;
- Factory fac ;
- //具体产品
- Apple apple;
- Grape grape;
- fac = new AppleFactory();
- //Apple的Class对象传给factory方法;
- apple = (Apple) fac.factory(Apple.class);
- System.out.println(apple);
- apple.plant();
- apple.grow();
- apple.harvest();
- fac = new GrapeFactory();
- grape = (Grape)fac.factory(Grape.class);
- System.out.println(grape);
- grape.plant();
- grape.grow();
- grape.harvest();
- }
- }
运行结果:
Apple [treeAge=0]
Apple Tree is planted
Apple Tree is growing
Apple Tree is harvesting
Grape [seedless=true, treeAge=0]
Grape is palnted
Grape is growing
Grape is harvesting
总结:
工厂方法模式就是由具体的工厂生产具体的产品,当增加新产品时,只需增加一个新工厂即可。
转自:http://blog.csdn.net/liruikqn/article/details/12885491
posted on 2017-07-21 08:34 WenjieWangFlyToWorld 阅读(140) 评论(0) 编辑 收藏 举报