您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java策略模式和簡單工廠方法怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java策略模式和簡單工廠方法怎么實(shí)現(xiàn)”吧!
策略模式由前端觸發(fā)決定使用某一組算法或邏輯,每一組算法或邏輯不相同,互相不干擾。最后交由一個(gè)上下文具體去執(zhí)行,并可以增加一定公有邏輯。
比如某電商平臺(tái)搞活動(dòng):
普通會(huì)員:滿100減10塊
白銀會(huì)員:滿100減15塊 送積分
黃金會(huì)員:滿100減18塊 送積分 參與抽獎(jiǎng)
鉆石會(huì)員:滿100減20塊 送積分 參與抽獎(jiǎng) 免費(fèi)送貨
由會(huì)員級(jí)別決定執(zhí)行哪一個(gè)活動(dòng),建一個(gè)枚舉:
public enum MemberLevel { /// <summary> /// 普通會(huì)員 /// </summary> Normal, /// <summary> /// 白銀會(huì)員 /// </summary> Silver, /// <summary> /// 黃金會(huì)員 /// </summary> Gold, /// <summary> /// 鉆石會(huì)員 /// </summary> Diamond }
一個(gè)用戶類:
public class User { public int Id { get; set; } public string Name { get; set; } public MemberLevel Level { get; set; } }
我們有1個(gè)會(huì)員基類,定義一個(gè)優(yōu)惠的方法:
public abstract class BaseMember { public abstract void Preferential(decimal customMoney); }
4種會(huì)員分別實(shí)現(xiàn)BaseMember:
/// <summary> /// 普通會(huì)員 /// </summary> public class NormalUser : BaseMember { public NormalUser() { Console.WriteLine("普通會(huì)員"); } public override void Preferential(decimal customMoney) { Console.WriteLine("滿100減10塊"); } } /// <summary> /// 白銀會(huì)員 /// </summary> public class SilverUser: BaseMember { public SilverUser() { Console.WriteLine("白銀會(huì)員"); } public override void Preferential(decimal customMoney) { Console.WriteLine("滿100減15塊"); Console.WriteLine("送積分100"); } } /// <summary> /// 黃金會(huì)員 /// </summary> public class GoldUser: BaseMember { public GoldUser() { Console.WriteLine("黃金會(huì)員"); } public override void Preferential(decimal customMoney) { //if (customMoney > 100) //{ Console.WriteLine("滿100減18塊"); Console.WriteLine("送積分100"); Console.WriteLine("參與抽獎(jiǎng)"); //} } } /// <summary> /// 鉆石會(huì)員 /// </summary> public class DiamondUser : BaseMember { public DiamondUser() { Console.WriteLine("鉆石會(huì)員"); } public override void Preferential(decimal customMoney) { //if (customMoney > 100) //{ Console.WriteLine("滿100減20塊"); Console.WriteLine("送積分100"); Console.WriteLine("參與抽獎(jiǎng)"); Console.WriteLine("免費(fèi)送貨"); //} } }
我們還需要一個(gè)簡單工廠:(這里可以從配置文件去獲取,通過反射來實(shí)例化)
public class SimpleFactory { public static BaseMember CreateMember(MemberLevel memberLevel) { return MemberLevelConfig.MemberDictionary[memberLevel]; } }
public static class MemberLevelConfig { public static Dictionary<MemberLevel, BaseMember> MemberDictionary = new Dictionary<MemberLevel, BaseMember>() { {MemberLevel.Normal,new NormalUser() }, {MemberLevel.Silver,new SilverUser() }, {MemberLevel.Gold,new GoldUser() }, {MemberLevel.Diamond,new DiamondUser() }, }; }
最后我們還需要一個(gè)上下文:增加一個(gè)公有的邏輯如果金額不夠100則無優(yōu)惠
public class Context { private BaseMember _baseMember; private decimal _customMoney; public Context(BaseMember baseMember,decimal customMoney) { _baseMember = baseMember; _customMoney = customMoney; } public void Preferential() { if (_customMoney < 100) { Console.WriteLine("金額未滿100,不能參加活動(dòng)"); } else { _baseMember.Preferential(_customMoney); } } }
現(xiàn)在我們的Main方法:
static void Main(string[] args) { User user = new User(); user.Level = MemberLevel.Silver; var member = SimpleFactory.SimpleFactory.CreateMember(user.Level); Console.WriteLine("********************"); Context.Context context = new Context.Context(member, 180); context.Preferential(); Console.ReadKey(); }
到此,相信大家對(duì)“java策略模式和簡單工廠方法怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。