02_StrategyPatternデパートの販促

7209 ワード

CashFactoryファクトリクラス

package cn;

import java.lang.reflect.Constructor;
import java.util.HashMap;

public class CashFactory {
	private static CashSuper cashSuper;
	private static HashMap m = new HashMap();
	private static void initMap(){
		m.put("CashNormal", "cn.beans.CashNormal");
		m.put("CashRebate", "cn.beans.CashRebate");
		m.put("CashReturn", "cn.beans.CashReturn");
	}
	public static CashSuper getInstance(String type){
		initMap();
		try {
			String clazz;
			// , setter
			clazz=m.get(type).toString();
			cashSuper=(CashSuper)Class.forName(clazz).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cashSuper;
	}
	

	public static CashSuper getInstance(String type, String[] parameters){
		if (parameters==null){
			return getInstance(type);
		}
		initMap();
		try {
			//  
			String clazz=m.get(type).toString();
			// Class 
			Class cs = Class.forName(clazz);
			// 
			Class[] parameterTypes=new Class[parameters.length];
			for (int i=0;i<parameters.length;i++){
				parameterTypes[i]=String.class;
			}
			// 
			System.err.println(parameterTypes==null);
			Constructor constructor=cs.getConstructor(parameterTypes);
			// , 
			Object o=constructor.newInstance(parameters);
			cashSuper = (CashSuper)o;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return cashSuper;
	}
}

CashSuperポリシーインタフェース

package cn;

public interface CashSuper {
	public String acceptCash(String costPrice);
}

CashContextポリシーコンテキスト、接続ポリシーインタフェース、呼び出しポリシーのクラス

package cn.beans;

import cn.CashSuper;

public class CashContext {
	
	private CashSuper cashSuper;
	
	public CashContext(CashSuper cashSuper){
		this.cashSuper=cashSuper;
	}
	
	// 
	public String acceptCash(String costPrice){
		return cashSuper.acceptCash(costPrice);
	}
	
}

CashNormal普通Cash類

package cn.beans;

import cn.CashSuper;

public class CashNormal implements CashSuper {

	@Override
	public String acceptCash(String costPrice) {
		System.err.println(" CashNormal acceptCash ");
		return costPrice;
	}

}

CashRebate割引Cash類

package cn.beans;

import java.math.BigDecimal;

import cn.CashSuper;

public class CashRebate implements CashSuper{

	private String rebate="1";
	
	@Override
	public String acceptCash(String costPrice) {
		System.err.println(" CashRebate acceptCash ");
		BigDecimal dcm = new BigDecimal(costPrice.toCharArray());
		BigDecimal dcmRebate = new BigDecimal(rebate.toCharArray());
		// 
		dcm = dcm.multiply(dcmRebate).setScale(2,BigDecimal.ROUND_HALF_UP);
		return dcm.toString();
	}
	
	public CashRebate() {
		super();
	}

	public CashRebate(String rebate) {
		super();
		this.rebate = rebate;
	}

	public void setRebate(String rebate) {
		this.rebate = rebate;
	}
	
}

CashReturn返利Cash類

package cn.beans;

import java.math.BigDecimal;

import cn.CashSuper;

public class CashReturn implements CashSuper {

	private String cashCondition;
	private String cashReturn;
	
	public CashReturn(){
		super();
	}
	
	public CashReturn(String cashCondition,String cashReturn){
		super();
		this.cashCondition=cashCondition;
		this.cashReturn=cashReturn;
	}
	
	public void setCashCondition(String cashCondition) {
		this.cashCondition = cashCondition;
	}

	public void setCashReturn(String cashReturn) {
		this.cashReturn = cashReturn;
	}

	@Override
	public String acceptCash(String costPrice) {
		System.err.println(" CashReturn acceptCash ");
		BigDecimal dcm = new BigDecimal(costPrice.toCharArray());
		BigDecimal dcmCondition = new BigDecimal(cashCondition.toCharArray());
		BigDecimal dcmReturn = new BigDecimal(cashReturn.toCharArray());
		BigDecimal dcmTemp = new BigDecimal(0);
		dcmTemp = dcm.divide(dcmCondition,0,BigDecimal.ROUND_DOWN);
		dcmTemp = dcmReturn.multiply(dcmTemp);
		dcm = dcm.subtract(dcmTemp);
		return dcm.toString();
	}

}

JUnitテストクラス

package junit.test;

import org.junit.Test;

import cn.CashFactory;
import cn.CashSuper;
import cn.beans.CashContext;
import cn.beans.CashNormal;
import cn.beans.CashRebate;
import cn.beans.CashReturn;


public class Test_Cash {
	@Test public void test(){
		// 
		CashSuper cashSuper=CashFactory.getInstance("CashNormal");
		CashContext cashContext = new CashContext(cashSuper);
		String price = cashContext.acceptCash("1000");
		System.err.println(price);
		//  setter
		CashRebate cashRebate=(CashRebate)CashFactory.getInstance("CashRebate");
		cashRebate.setRebate("0.8");
		cashContext = new CashContext(cashRebate);
		price = cashContext.acceptCash("1000");
		System.err.println(price);
		// 300 100 setter
		CashReturn cashReturn = (CashReturn)CashFactory.getInstance("CashReturn");
		cashReturn.setCashCondition("300");
		cashReturn.setCashReturn("100");
		cashContext = new CashContext(cashReturn);
		price = cashContext.acceptCash("1000");
		System.err.println(price);		
		System.err.println("................ .....................");
		// 200 100 constructor
		String [] paramReturn= {"200","100"};
		cashReturn = (CashReturn)CashFactory.getInstance("CashReturn",paramReturn);
		cashContext = new CashContext(cashReturn);
		price = cashContext.acceptCash("1000");
		System.err.println(price);	
		//  constructor
		String [] paramRebate = {"0.85"};
		cashRebate = (CashRebate)CashFactory.getInstance("CashRebate",paramRebate);
		cashContext = new CashContext(cashRebate);
		price = cashContext.acceptCash("1000");
		System.err.println(price);	
		// 
		CashNormal cashNormal = (CashNormal)CashFactory.getInstance("CashNormal",null);
		cashContext = new CashContext(cashNormal);
		price = cashContext.acceptCash("1000");
		System.err.println(price);	
		
	}
}