一日一技:策略模式(附demo)

概念

在策略sql教程模式(Strategy Pattern)中,一个类的行java基础教程为或其算法python基础教程可以在运c#教程行时更改。这种类型的设vb.net教程计模式属于行为型模式。

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

策略其实就是做一件事情有很多很多的方法,比如说一个商场要搞促销,促销的方式有可能有很多:打折啊,满100返50啊、积分等等之类的。这种不同的促销方式在我们系统中表示就是一个一个的策略。

实现方式

1、定义策略接口

复制代码

   public interface IPromotion
    {
        /// <summary>
        /// 根据原价和策略计算新价格
        /// </summary>
        /// <param name="originPrice">原价</param>
        /// <returns></returns>
        double GetPrice(double originPrice);
    }

复制代码

2、实现不同的接口1、2

复制代码

 public class Discount : IPromotion
    {
        public double GetPrice(double originPrice)
        {
            Console.WriteLine("打八折:");
            return originPrice * 0.8;
        }
    }

    public class MoneyBack : IPromotion
    {
        public double GetPrice(double originPrice)
        {
            Console.WriteLine("满100返50");
            return originPrice - (int)originPrice / 100 * 50;
        }
    }

复制代码

3、定义策略工厂

复制代码

public class PromotionContext
    {
        private IPromotion p = null;

        public PromotionContext(IPromotion p)
        {
            this.p = p;
        }

        public double GetPrice(double originPrice)
        {
            // 默认策略
            if (this.p == null)
            {
                this.p = new Discount();
            }
            return this.p.GetPrice(originPrice);
        }

        /// <summary>
        /// 更改策略的方法
        /// </summary>
        /// <param name="p"></param>
        public void ChangePromotion(IPromotion p)
        {
            this.p = p;
        }
    }

复制代码

4、测试策略,切换不同的实现方式

复制代码

 public IActionResult Index()
        {
            // 默认策略:打八折的策略
            PromotionContext pc = new PromotionContext(null);
            Console.WriteLine(pc.GetPrice(200));

            // 更改策略:满100返50的策略
            pc.ChangePromotion(new MoneyBack());
            Console.WriteLine(pc.GetPrice(155.9));

            return View();
        }

复制代码

结果如下

开源地址

https://gitee.com/conanOpenSource_admin/Example/tree/master/%E7%AD%96%E7%95%A5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Java 实现策略模式的示例代码: 首先,我们需要定义一个策略接口,例如: ```java interface Strategy { int execute(int a, int b); } ``` 然后,我们可以定义一些具体的策略类,例如: ```java class AddStrategy implements Strategy { @Override public int execute(int a, int b) { return a + b; } } class SubStrategy implements Strategy { @Override public int execute(int a, int b) { return a - b; } } class MultiplyStrategy implements Strategy { @Override public int execute(int a, int b) { return a * b; } } ``` 接下来,我们可以定义一个上下文类,用于执行策略。例如: ```java class Context { private final Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return strategy.execute(a, b); } } ``` 最后,我们可以在客户端代码中使用上下文类来执行具体的策略。例如: ```java public class Client { public static void main(String[] args) { Context context = new Context(new AddStrategy()); int result = context.executeStrategy(1, 2); System.out.println(result); // 输出 3 context = new Context(new SubStrategy()); result = context.executeStrategy(3, 2); System.out.println(result); // 输出 1 context = new Context(new MultiplyStrategy()); result = context.executeStrategy(2, 3); System.out.println(result); // 输出 6 } } ``` 这个示例演示了如何使用策略模式来实现根据不同的策略执行不同的行为。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值