码迷,mamicode.com
首页 > 其他好文 > 详细

04.创建型————创建者模式

时间:2016-01-02 22:21:59      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

package Create.d.Builder;

//业务接口
public interface Tax {
	void calcTax();
}

  

package Create.d.Builder;

//业务接口
public interface Salary {
	void calcSalary();
}

  

package Create.d.Builder;

//业务实现类
public class HeBeiTax implements Tax {

	@Override
	public void calcTax() {
		// TODO Auto-generated method stub
		System.out.println("开始计算河北子公司的税收!");
	}

}

  

package Create.d.Builder;

//业务实现类
public class HeBeiSalary implements Salary {

	@Override
	public void calcSalary() {
		// TODO Auto-generated method stub
		System.out.println("开始计算河北子公司的薪资!");
	}

}

  

package Create.d.Builder;

//业务实现类
public class GuangDongTax implements Tax {

	@Override
	public void calcTax() {
		// TODO Auto-generated method stub
		System.out.println("开始计算广东子公司的税收!");
	}

}

  

package Create.d.Builder;

//业务实现类
public class GuangDongSalary implements Salary {

	@Override
	public void calcSalary() {
		// TODO Auto-generated method stub
		System.out.println("开始计算广东子公司的薪资!");
	}

}

  

package Create.d.Builder;

//工厂接口
public interface Factory {
  Salary createSalary();
  Tax createTax();
}

  

package Create.d.Builder;

//业务实现类工厂类
public class HeBeiFactory implements Factory {

	@Override
	public Salary createSalary() {
		// TODO Auto-generated method stub
		return new HeBeiSalary();
	}

	@Override
	public Tax createTax() {
		// TODO Auto-generated method stub
		return new HeBeiTax();
	}

}

  

package Create.d.Builder;

//业务实现类工厂类
public class GuangDongFactory implements Factory {

	@Override
	public Salary createSalary() {
		// TODO Auto-generated method stub
		return new GuangDongSalary();
	}

	@Override
	public Tax createTax() {
		// TODO Auto-generated method stub
		return new GuangDongTax();
	}

}

  

package Create.d.Builder;


//创建者类
public class Builder {
		Factory fa;
		public Builder() {
			super();
		}
		
		public Builder(Factory fa) {
			super();
			this.fa = fa;
		}
		
		//封装业务实现类调用
		public void build(){
			Salary sa = fa.createSalary();
			Tax tax = fa.createTax();
			sa.calcSalary();
			tax.calcTax();
		}
		
		
}

  

package Create.d.Builder;

//业务调用类
public class Client {
public static void main(String[] args) {
	Builder builder = new Builder(new HeBeiFactory());
	builder.build();
	
	builder =  new Builder(new GuangDongFactory());
	builder.build();
	
	
}
}

  

04.创建型————创建者模式

标签:

原文地址:http://www.cnblogs.com/ciade/p/5095304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!