This is probably the most user-friendly strategy pattern was talking about the article

Weekend without incident, nest inside the home to see "Game of Thrones eighth season," look very relish, although feeling a little to be unfinished, but I could not help going to see in the end who can sit on the Iron Throne.

Girlfriend takeaway point in the side, as if to use the discount when it came to a little small problem.

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

Tactics

Strategy, the program refers to a set of goals can be achieved, in certain circumstances, is between the policy can be interchangeable.

Such as we see on these offers take-away platform. Full cut, and red envelopes and other members, each large item contains a number of concessions have specific discount program. The full reduction activities, while there may be at least 20 minus 15, minus 30, etc. over 50. Members include ordinary members, super affiliate and so on.

Each of the following more favorable concessions program, in fact, is a strategy. Among these strategies are mutually exclusive, it can be replaced. And have a certain priority order.

This is probably the most user-friendly strategy pattern was talking about the article

 

As shown above, an order to use the CCP four concessions, we can say that a combination of four preferential policies.

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

How to calculate the amount of

We acquire a take-away point in the sub-Member discounts example to illustrate it. A take-away shop on the platform for promotional purposes, set a variety of member benefits, which includes 20% discount super affiliates, general membership discount 10% discount and ordinary users do not have three.

We want the user at the time of payment, according to the user's membership level, you can know what kind of discount strategy in line with the user, and then discount, calculate the amount due.

The code can be written:

This is probably the most user-friendly strategy pattern was talking about the article

 

The above code is relatively simple, logical judgment is performed by the code in the if-else, different types of members enjoy different discounts.

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

Then add a membership type

This time, the platform adds a shop exclusive membership, which members can enjoy a 30% discount for a shop dishes, then have to change the code so the following:

This is probably the most user-friendly strategy pattern was talking about the article

 

Member Discount change

Behind, along with business development, new demands require exclusive membership to shop at a single amount is greater than $ 30 when it can enjoy the benefits. Code needs to be modified again:

This is probably the most user-friendly strategy pattern was talking about the article

 

Then, there are a metamorphosis of demand, if the super-user membership has expired, and the expiration time in a week, then for single orders in accordance with the user's discount super affiliate, and strong reminders at the cash register, guide the user again open membership, discount and once only. Code needs to be modified as follows:

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

Why use the strategy pattern

The above code, all code of member discounts on all written in a calPrice approach, increase or reduce one type of membership, we need to change the whole approach. Also consider priority issues this membership discounts.

除了增加会员类型外,其中任何一种会员类型的折扣策略发生变化,也需要改动到整个算法。

这就会导致这个算法越来越臃肿,进而得到的后果就是代码行数越来越多,开发人员改动一点点代码都需要把所有功能全部都回归一遍。

就比如说我只是把超级会员的折扣从8折改为8.5折,这时候因为代码都在一起,那就需要在上线的时候对于会员折扣的所有功能进行回归。

This is probably the most user-friendly strategy pattern was talking about the article

 

久而久之,这段代码就变成了一段谁都不愿改,谁都不敢改的代码。俗称"屎山"。这种代码会使代码有极低的可读性、可维护性、可扩展性,并且回归成本较高。

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

策略模式

我们说日常生活中,我们要实现目标,有很多方案,每一个方案都被称之为一个策略。在软件开发中也常常遇到类似的情况,实现某一个功能有多个途径,此时可以使用一种设计模式来使得系统可以灵活地选择解决途径,也能够方便地增加新的解决途径。这就是策略模式。

策略模式(Strategy Pattern),指的是定义一系列算法,将每一个算法封装起来,并让它们可以相互替换。策略模式让算法独立于使用它的客户而变化。

特别说明一下,策略模式只适用管理一组同类型的算法,并且这些算法是完全互斥的情况。也就是说任何时候,多个策略中只有一个可以生效的那一种。如满减中的满20减10与满30减20之间;普通会员折扣与超级会员折扣之间等。

在策略模式中,定义一些独立的类来封装不同的算法,每一个类封装一个具体的算法,在这里,每一个封装算法的类我们都可以称之为策略(Strategy),为了保证这些策略的一致性,一般会用一个抽象的策略类来做算法的定义,而具体每种算法则对应于一个具体策略类。

要实现策略模式,肯定离不开策略。如前面提到的超级会员、普通会员、专属会员等的折扣其实都是策略。完全可以通过策略模式来实现。

实现策略模式主要包含的角色如下:

This is probably the most user-friendly strategy pattern was talking about the article

 

抽象策略类

先定义一个接口,这个接口就是抽象策略类,该接口定义了计算价格方法,具体实现方式由具体的策略类来定义。

This is probably the most user-friendly strategy pattern was talking about the article

 

具体策略类

针对不同的会员,定义三种具体的策略类,每个类中都分别实现计算价格方法。

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

上面几个类的定义体现了封装变化的设计原则,不同会员的具体折扣方式改变不会影响到其他的会员。

定义好了抽象策略类和具体策略类之后,我们再来定义上下文类,所谓上下文类,就是集成算法的类。这个例子中就是收银台系统。采用组合的方式把会员集成进来。

This is probably the most user-friendly strategy pattern was talking about the article

 

这个Cashier类就是一个上下文类,该类的定义体现了多用组合,少用继承、针对接口编程,不针对实现编程两个设计原则。

由于这里采用了组合+接口的方式,后面我们在推出其他类型会员的时候无须修改Cashier类。只要再定义一个类实现Buyer接口 就可以了。

除了增加会员类型以外,我们想要修改某个会员的折扣情况的时候,只需要修改该会员对应的策略类就可以了,不需要修改到其他的策略。也就控制了变更的范围。大大降低了成本。

下面定义一个客户端来测试一下:

This is probably the most user-friendly strategy pattern was talking about the article

 

输出结果:

//普通会员商品的最终价格为:270.0
//超级会员商品的最终价格为:240.0

从上面的示例可以看出,策略模式仅仅封装算法,提供新的算法插入到已有系统中,策略模式并不决定在何时使用何种算法。在什么情况下使用什么算法是由客户端决定的。

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

策略模式的优缺点

策略模式可以充分的体现面向对象设计原则中的封装变化、多用组合,少用继承、针对接口编程,不针对实现编程等原则。

策略模式具有以下特点:

  • 策略模式的关注点不是如何实现算法,而是如何组织、调用这些算法,从而让程序结构更灵活,具有更好的维护性和扩展性。
  • 策略模式中各个策略算法是平等的。对于一系列具体的策略算法,大家的地位是完全一样的,正因为这个平等性,才能实现算法之间可以相互替换。所有的策略算法在实现上也是相互独立的,相互之间是没有依赖的。所以可以这样描述这一系列策略算法:策略算法是相同行为的不同实现。
  • 运行期间,策略模式在每一个时刻只能使用一个具体的策略实现对象,虽然可以动态地在不同的策略实现中切换,但是同时只能使用一个。

If all of the specific policy class has some public behavior. At this time, we should put these public behavior into a common strategy role-Strategy abstract class inside. Of course, this time abstract strategy role must use Java abstract class that implements the interface can not be used.

This is probably the most user-friendly strategy pattern was talking about the article

 

However, there is no silver bullet programming, strategy pattern is no exception, he also has some disadvantages, let's review and sum up his advantage:

  • Strategy Mode provides the perfect support for the "opening and closing principle", the user can select on the basis of the algorithm or behavior does not modify the original system, but also the flexibility to add new algorithms or behavior.
  • Strategy pattern provides a family of algorithms related to the management approach. Class hierarchy strategy defines a family of algorithms or behavior. Proper use inheritance can be moved to a common parent class code, the code to avoid repetition.
  • Use the Strategy pattern to avoid using multiple conditions (if-else) statements. Multiple conditional statement is not easy to maintain, take it to an algorithm which logic and arithmetic or logic or behavior which take an act of mixing together, all listed in a multiple conditional statement inside, also raw and backward than the way to use inheritance .

But at the same time, he also has the following disadvantages:

  • The client must know all the policy class, and the class to decide which strategy to use. This means that the client must understand the difference between these algorithms, in order to timely select the appropriate algorithm class. Create and choose this kind of strategy can actually be assisted by the factory model.
  • Since the policy of each particular mode implement strategies are packaged into a separate class, if many alternative strategies, then the number of objects will be very considerable. It can reduce the number of objects to a certain extent by using Flyweight.
This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

This is probably the most user-friendly strategy pattern was talking about the article

 

Finally, attach a mind map of this article:

This is probably the most user-friendly strategy pattern was talking about the article

Guess you like

Origin www.cnblogs.com/CQqf2019/p/10935401.html