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