C# 设计模式 策略模式

C# 设计模式 策略模式_第1张图片

1.抽象类

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

namespace Strategy
{
    abstract class Strategy
    {
        /// 
        /// 根据简单订单 计算折扣后的价格
        /// 
        /// 
        /// 
        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
{
    /// 
    /// 正常 折扣策略
    /// 
    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();

        }
    }
}

在这里插入图片描述

你可能感兴趣的:(C#,策略模式,c#,设计模式)