In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of C# strategy model, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Strategy mode
In fact, the so-called strategy is that there are many ways to do one thing, such as a shopping mall to carry out sales promotion, there may be many ways to promote sales: discount, 100 to 50, points, and so on. This different way of promotion is expressed as a strategy in our system, and the strategy can be changed at any time, so the policy mode can be used when designing the system.
Shopping malls may change or add new promotion modes, that is, the strategy will be adjusted, that is, the previous code will be changed. In order to meet the opening and closing principle, abstract classes and interfaces will be used. Here we prefer to use interfaces. Define the method of policy in the interface, write different implementation classes according to different situations, and implement different strategies. Policy patterns are more suitable for frequent changes in algorithms, such as the way to calculate wages, the choice of travel mode, and so on.
As shown in the figure, we first define the interface of the policy (Promotion), then define the method of the policy in this policy interface (GetPrice ()), and then we define two specific policies (Discount discount) and (MoneyBack cash back).
The policy pattern will have a special context object (PromotionContext) to manage the policy class, and the relationship between the context object and the policy interface is aggregated, that is, the relationship between the whole and the part, so a reference of the promotion type should be saved in the context object. In addition, there are generally some methods in the context object that are convenient for the client to call, such as GetPrice (). The client program can get the price through the context object, and this GetPrice () will execute different policy methods according to different policies.
If the client does not want to use the default policy defined in the context, he can also modify the policy class, because there is a ChangePromotion () method in the context, and the client mainly uses the context object, and if it needs to modify the policy, it also depends on the specific policy object.
Example:
1. Policy API:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace policy mode {/ * policy interface * / public interface IPromotion {/ calculate the new price based on the original price and policy / original price / double GetPrice (double originPrice);}}
2. Discount discount policy class
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace policy mode {/ discount policy class / public class Discount: IPromotion {public double GetPrice (double originPrice) {Console.WriteLine ("20% discount:"); return originPrice * 0.8;}
3. MoneyBack cash-back class
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace policy mode {/ * Cash back policy class: policies returned to 50 after 100% * / class MoneyBack: IPromotion {public double GetPrice (double originPrice) {Console.WriteLine ("100 to 50"); return originPrice-(int) originPrice / 100 * 50;}
4. Policy context category
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace policy mode {/ * policy context, select the appropriate policy for the customer * / public class PromotionContext {private IPromotion p = null; public PromotionContext (IPromotion p) {this.p = p } public double GetPrice (double originPrice) {/ / default policy if (this.p = = null) {this.p = new Discount ();} return this.p.GetPrice (originPrice) } / change policy method / public void ChangePromotion (IPromotion p) {this.p = p;}
5. The main program calls
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace policy mode {class Program {static void Main (string [] args) {/ / default policy: 20% discount policy PromotionContext pc = new PromotionContext (null); Console.WriteLine (pc.GetPrice (200)) / / change policy: policy pc.ChangePromotion (new MoneyBack ()); Console.WriteLine (pc.GetPrice (155.9)); Console.ReadKey ();} above are all the contents of the article "sample Analysis of C# Policy pattern". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.