C# 设计模式之 策略模式 示例

封装算法

  1. using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPytternDemo{    ///     /// 策略模式    ///     public abstract class BaseStategy    {        public virtual int GetTicketPrice(int price)        {            return price;        }    }    public class CommonPersonStrategy : BaseStategy    {        public override int GetTicketPrice(int price)        {            return base.GetTicketPrice(price);        }    }    public class StudentStrategy : BaseStategy    {        public override int GetTicketPrice(int price)        {            return price / 2;        }    }    public class Context    {        private BaseStategy _context;        public Context(string personType)        {            switch (personType)            {                case "c":                    _context = new CommonPersonStrategy();                    break;                case "s":                    _context = new StudentStrategy();                    break;                default:                    break;            }        }        public int GetTicketPrice(int price)        {            return this._context.GetTicketPrice(price);        }    }}

登录后复制

 以上就是C# 设计模式之 策略模式  示例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    C# 设计模式之 工厂模式

    2025-3-5 2:22:35

    编程技术

    C# 设计模式之桥接模式的代码详解

    2025-3-5 2:22:49

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索