• 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();
    	}
    }
    



  • 相关阅读:
    藕益大师示念佛法门
    【丁嘉丽老师】最新心得报告——生死无常,好好念佛(视频+文字)
    【视频+文字】刘素云老师:法雨惠群生--老实念佛不拐弯 今生一定到彼岸(附净空老法师点评)
    刘素云老师复讲《无量寿经》第16集
    《认识佛教》有声书-02-净空法师-初学佛课程(音)
    《认识佛教》有声书-01-净空法师-初学佛课程(音)
    【强烈推荐】科注学习班——自了法师分享:珍惜暇满人身宝(上)
    自了法师全集 | 无量寿经科注第四回学习班分享三十篇
    《2014净土大经科注》大汇总
    释自了法师:生死心不切,若真为生死之心不发,一切开示皆为戏论
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7071454.html
Copyright © 2020-2023  润新知