C# 设计模式 策略模式

请添加图片描述

1.抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    abstract class Strategy
    {
        /// <summary>
        /// 根据简单订单 计算折扣后的价格
        /// </summary>
        /// <param name="productOrder"></param>
        /// <returns></returns>
        public abstract double computePrice(ProductOrder productOrder);

    }
}

2.订单类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    class ProductOrder
    {
        private double oldPrice;
        private int userId;
        private int productId;

        public double OldPrice { get => oldPrice; set => oldPrice = value; }
        public int UserId { get => userId; set => userId = value; }
        public int ProductId { get => productId; set => productId = value; }

        public ProductOrder(double oldPrice, int userId, int productId)
        {
            this.oldPrice = oldPrice;
            this.userId = userId;
            this.productId = productId;
        }
    }
}

3.无折扣策略

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    /// <summary>
    /// 正常 折扣策略
    /// </summary>
    class NormalActivity : Strategy
    {
        public override double computePrice(ProductOrder productOrder)
        {
            return productOrder.OldPrice;
        }
    }
}

4.折扣策略

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    class DiscountActivity : Strategy
    {
        private double rate;

        public DiscountActivity(double rate)
        {
            this.rate = rate;
        }

        public override double computePrice(ProductOrder productOrder)
        {
            return productOrder.OldPrice * rate;
        }
    }
}

5.优惠券策略

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    class VoucherActivity : Strategy
    {
        private double voucher;

        public VoucherActivity(double voucher)
        {
            this.voucher = voucher;
        }

        public override double computePrice(ProductOrder productOrder)
        {
            if(productOrder.OldPrice > voucher)
            {
                return productOrder.OldPrice - voucher;
            }
            else
            {
                return 0;
            }
        }
    }
}

6.促销上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    class VoucherActivity : Strategy
    {
        private double voucher;

        public VoucherActivity(double voucher)
        {
            this.voucher = voucher;
        }

        public override double computePrice(ProductOrder productOrder)
        {
            if(productOrder.OldPrice > voucher)
            {
                return productOrder.OldPrice - voucher;
            }
            else
            {
                return 0;
            }
        }
    }
}

7.具体测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strategy
{
    class Program
    {
        static void Main(string[] args)
        {
            // 订单对象
            ProductOrder productOrder = new ProductOrder(800, 001, 202);
            Console.WriteLine("标准价格:800");

            PromotionContext context;

            // 不同的策略 计算出不同的价格
            context = new PromotionContext(new NormalActivity());
            double finalPrice = context.ExecuteStrategy(productOrder);
            Console.WriteLine("普通策略 价格:" + finalPrice);

            // 8折策略
            context = new PromotionContext(new DiscountActivity(0.8));
            finalPrice = context.ExecuteStrategy(productOrder);
            Console.WriteLine("8折扣策略 价格:" + finalPrice);

            // 优惠券策略
            context = new PromotionContext(new VoucherActivity(100));
            finalPrice = context.ExecuteStrategy(productOrder);
            Console.WriteLine("优惠券100策略 价格:" + finalPrice);


            Console.ReadLine();

        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值