Java--策略模式和简单工厂模式

通过商场促销来说明策略模式和简单工厂模式的合用例子,例子如下:

1、基类收费

public interface CashSuper{
	public int acceptCash(int money);
}
2、实现基类--正常收费
public class CashNormal implements CashSuper{
	@Override
	public int acceptCash(int money) {
		return money;
	}
}

3、实现基类--打折收费

public class CashRebate implements CashSuper{
	private int moneyRebate;
	public CashRebate(int moneyRebate) {
		super();
		this.moneyRebate = moneyRebate;
	}
	@Override
	public int acceptCash(int money) {
		return money*moneyRebate;
	}
	public int getMoneyRebate() {
		return moneyRebate;
	}
	public void setMoneyRebate(int moneyRebate) {
		this.moneyRebate = moneyRebate;
	}
}

4、实现基类--返利收费

public class CashReturn implements CashSuper{
	private int moneyCondition;
	private int moneyReturn;
	public CashReturn(int moneyCondition, int moneyReturn) {
		super();
		this.moneyCondition = moneyCondition;
		this.moneyReturn = moneyReturn;
	}
	@Override
	public int acceptCash(int money) {
		int result = money;
		if(money>moneyCondition){
			result = money - money/moneyCondition *moneyReturn;
		}
		return result;
	}
	public int getMoneyCondition() {
		return moneyCondition;
	}
	public void setMoneyCondition(int moneyCondition) {
		this.moneyCondition = moneyCondition;
	}
	public int getMoneyReturn() {
		return moneyReturn;
	}
	public void setMoneyReturn(int moneyReturn) {
		this.moneyReturn = moneyReturn;
	}
}
5、策略实现类
public class CashContext {
	private CashSuper cashSuper;
	public CashContext(String type) {
		switch (type) {
		case "1":
			cashSuper = new CashNormal();
			break;
		case "2":
			cashSuper = new CashReturn(200,100);
			break;
		case "3":
			cashSuper = new CashRebate(8);
			break;
		default:
			break;
		}
	}

	public CashSuper getCashSuper() {
		return cashSuper;
	}

	public void setCashSuper(CashSuper cashSuper) {
		this.cashSuper = cashSuper;
	}
	public int getResult(int money){
		return cashSuper.acceptCash(money);
	}
}
6、测试
public class Test {
	public static void main(String[] args) {
		CashContext cashContext = new CashContext("正常收费");
		System.out.println(cashContext.getResult(200));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
策略模式和工厂模式可以搭配使用,以解决策略模式中的一些缺陷。在策略模式中,我们可以使用工厂方法模式来明确返回对象的信息。具体实现可以分为以下几个步骤: 1. 定义策略的接口,包括策略的抽象方法。 2. 创建各个实现策略接口的具体策略实现类。 3. 使用组合的方式,将抽象接口的实现类作为参数创建一个容器类。 4. 在策略实现类中加入信息标签,可以使用枚举类的type值作为标签的载体。 5. 使用一个map对象作为容器类的参数,将标签和对应的策略实现类进行一一对应关系。 6. 在具体的策略实现类中继承InitializingBean接口,并重写其中的afterPropertiesSet方法,在该方法中将含有信息的type和具体的策略实现类放入容器类的map中。 7. 最后,通过容器类的map和标签,可以根据需要获取对应的策略实现。 使用工厂模式结合策略模式,不仅避免了大量的if-else判断,还能明确策略的返回信息,解决了策略模式的一些缺陷。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java 策略模式+工厂方法模式搭配思想](https://blog.csdn.net/weixin_44284706/article/details/124967861)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值