设计模式第四讲:建造者模式

建造者模式


一.模式定义:

将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。


二.模式特点:

与工厂模式很类似,但是建造者模式最主要的功能是对对象的基本方法的调用顺序安排,和工厂模式最主要的功能:创建,并不一样。


三.使用场景:

1.产品类很复杂,内部方法逻辑复杂;

2.产品类中,基本方法的调用顺序不同产生不同效果,比如:超市计算折扣,各自折扣的计算顺序针对不同的用户群体,是不一样的。


四. 具体实现:


这里以商场对于不同消费者群体,采取不同的折扣方式为例子。

1.定义消费者基本抽象类:


import java.util.ArrayList;

//消费者基本类
public abstract class Customer {
	
	//付账时的折扣计算顺序
	private ArrayList sequence = new ArrayList<>();
	//消费金额
	protected float spendMoney = 1000;
	//七折
	protected abstract void seventyPercentDiscount();
	//八折
	protected abstract void eightytyPercentDiscount();
	//九折
	protected abstract void ninetyPercentDiscount();
	//结账计算
	final public String calculate()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("总花费:");
		sb.append(spendMoney);
		for(int i = 0;i < this.sequence.size();i++)
		{
			String account = this.sequence.get(i);
			if(account.equalsIgnoreCase("seventy"))
			{
				this.seventyPercentDiscount();
				sb.append(" 打七折,");
			}
			else if(account.equalsIgnoreCase("eighty"))
			{
				this.eightytyPercentDiscount();
				sb.append(" 打八折,");
			}
			else
			{
				this.ninetyPercentDiscount();
				sb.append(" 打九折,");
			}
		}
		sb.append("结算金额:"+spendMoney);
		return sb.toString();
	}
	
	//设定结账计算折扣顺序
	final public void setSequence(ArrayList sequence)
	{
		this.sequence = sequence;
	}
}

2.定义不同的消费者具体类


//一般的消费者
public class NormalCustomer extends Customer{

	@Override
	protected void seventyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.7);
	}

	@Override
	protected void eightytyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.8);
	}

	@Override
	protected void ninetyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.9);
	}

}

//VIP消费者
public class VipCustomer extends Customer{

	@Override
	protected void seventyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.7);
	}

	@Override
	protected void eightytyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.8);
	}

	@Override
	protected void ninetyPercentDiscount() {
		// TODO Auto-generated method stub
		this.spendMoney = (float) (this.spendMoney * 0.9);
	}

}

3.定义计算消费的建造者基本抽象类:


import java.util.ArrayList;

//消费者消费金额计算建造者抽象类
public abstract class CustomerSpendBuilder {
	//设置计算折扣顺序
	public abstract void setSequence(ArrayList sequence);
	//获取消费者对象
	public abstract Customer getCustomer();
}

4.定义计算消费的建造者具体类:


import java.util.ArrayList;

//普通费者消费金额计算建造者
public  class NormalCustomerSpendBuilder extends CustomerSpendBuilder{

	private NormalCustomer customer = new NormalCustomer();

	@Override
	public void setSequence(ArrayList sequence) {
		// TODO Auto-generated method stub
		customer.setSequence(sequence);
	}

	@Override
	public Customer getCustomer() {
		// TODO Auto-generated method stub
		return customer;
	}
}

import java.util.ArrayList;

//Vip消费者消费金额计算建造者
public  class VipCustomerSpendBuilder extends CustomerSpendBuilder{

	private VipCustomer customer = new VipCustomer();
	
	@Override
	public void setSequence(ArrayList sequence) {
		// TODO Auto-generated method stub
		customer.setSequence(sequence);
	}

	@Override
	public Customer getCustomer() {
		// TODO Auto-generated method stub
		return customer;
	}
	
}

5.定义控制建造者的Direct类:


import java.util.ArrayList;


//控制建造者的类
public class Direct {
	private ArrayList sequence = new ArrayList();
	private VipCustomer vipCustomer = new VipCustomer();
	private NormalCustomer normalCustomer = new NormalCustomer();
	
	//Vip用户的消费计算
	public VipCustomer getVipCustomer()
	{
		this.sequence.clear();
		
		this.sequence.add("seventy");
		this.sequence.add("eighty");
		this.sequence.add("ninety");
		
		this.vipCustomer.setSequence(this.sequence);
		return vipCustomer;
	}
	//普通用户的消费计算
	public NormalCustomer getNormalCustomer()
	{
		this.sequence.clear();
		
		this.sequence.add("ninety");
		
		this.normalCustomer.setSequence(this.sequence);
		return normalCustomer;
	}
			
}

6.使用实例:


public class main {

	public static void main(String[] args)
	{
		Direct direct = new Direct();
		
		NormalCustomer normalCustomer = direct.getNormalCustomer();
		System.out.println("普通消费者    "+normalCustomer.calculate());
		
		VipCustomer vipCustomer = direct.getVipCustomer();
		System.out.println("Vip消费者    "+vipCustomer.calculate());
	}
}



你可能感兴趣的:(设计模式实例讲解,设计模式)