Chapter 2 Caché Design Pattern Strategic Pattern

Chapter 2 Caché Design Pattern Strategic Pattern

definition

  • The strategy pattern is a method to define a series of algorithms. From a conceptual point of view, all of these algorithms do the same work, but only implement differently. It can call all the algorithms in the same way, reducing various algorithm classes Coupling with classes using algorithms.

scenes to be used

  • Hide the implementation details of specific strategies from customers, completely independent of each other
  • Multiple ways to deal with the type of consent, only when the specific behavior is different
  • Many behaviors are defined in a class, and the operations of these behaviors in this class appear in the form of multiple conditional statements

advantage

  • Simplified unit testing, because each algorithm has its own class, which can be tested separately through its own interface
  • Use strategy pattern one to avoid using multiple conditional statements, which are not easy to maintain and error-prone. Eliminate excessive if else nesting
  • Easy to expand. When you need to add a strategy, you only need to implement the interface.

Disadvantages

  • Each strategy is a class, with low reusability. If there are too many strategies, the number of classes will increase
  • The upper-layer module must know what strategies are in order to be able to use these strategies, which is contrary to the Dimit principle

The difference between simple factory model and strategy model

  • The factory pattern is mainly the instantiated object of the returned interface implementation class, and the final result returned is the method in the interface implementation class.
  • The strategy pattern is already created when the strategy pattern is instantiated, and we can splice and rewrite methods in the strategy pattern at will.
  • The factory model is no matter how the method is spliced. He only pays attention to the final result, not the process, and the strategic model focuses on the process.
  • What the factory model can do, the strategy model can do it.
  • What the strategy model can do, the factory model can also do it, but it will become troublesome.
  • The factory pattern only deals with production instances. How to use the factory instance is determined by the caller. The strategy pattern is to put the usage strategy of the generated instance in the strategy class and then provide it to the caller.
  • The factory mode caller can directly call the method attribute of the factory instance, etc., the strategy mode cannot directly call the method attribute of the instance, and needs to be called after encapsulating the strategy in the strategy class.

Example:
You can combine tricks at will and add three fighting methods ThreeFighting (). The factory method needs to be called three times.

Class PHA.YX.Design.Strategy.Context Extends %RegisteredObject
{

Property mFightingStrategy As PHA.YX.Design.Strategy.FightingStrategy;

Method %OnNew(fightingStrategy As PHA.YX.Design.Strategy.FightingStrategy) As %Status [ Private, ServerOnly = 1 ]
{
	s ..mFightingStrategy=fightingStrategy
	Quit $$$OK
}

Method Fighting()
{
	d ..mFightingStrategy.Fighting()
}

Method ThreeFighting()
{
	d ..mFightingStrategy.Fighting()
	d ..mFightingStrategy.Fighting()
	d ..mFightingStrategy.Fighting()
}

}
/// d ##class(PHA.YX.Design.Program).Strategy() 
ClassMethod Strategy()
{
	#dim context as PHA.YX.Design.Strategy.Context
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.CommonRivalStrategy).%New())
	d context.Fighting()
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.StrongRivalStrategy).%New())
	d context.Fighting()
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.WeakRivalStrategy).%New())
	d context.Fighting()
	d context.ThreeFighting()
	q ""
}

Structure diagram

Complete example

description

  • According to different levels of opponents, specify different plans.

Define policy interface

Class PHA.YX.Design.Strategy.FightingStrategy [ Abstract ]
{

Method Fighting() [ Abstract ]
{
}

}

Specific strategy implementation

Class PHA.YX.Design.Strategy.StrongRivalStrategy Extends (%RegisteredObject, PHA.YX.Design.Strategy.FightingStrategy)
{

Method Fighting()
{
	w "遇到了强大的对象,张无忌使用乾坤大挪移",!
}

}
Class PHA.YX.Design.Strategy.CommonRivalStrategy Extends (%RegisteredObject, PHA.YX.Design.Strategy.FightingStrategy)
{

Method Fighting()
{
	w "遇到了普通的对象,张无忌使用圣火令",!
}

}
Class PHA.YX.Design.Strategy.WeakRivalStrategy Extends (%RegisteredObject, PHA.YX.Design.Strategy.FightingStrategy)
{

Method Fighting()
{
	w "遇到了较弱的对象,张无忌使用太极剑法",!
}

}

Context strategy

Class PHA.YX.Design.Strategy.Context Extends %RegisteredObject
{

Property mFightingStrategy As PHA.YX.Design.Strategy.FightingStrategy;

Method %OnNew(fightingStrategy As PHA.YX.Design.Strategy.FightingStrategy) As %Status [ Private, ServerOnly = 1 ]
{
	s ..mFightingStrategy=fightingStrategy
	Quit $$$OK
}

Method Fighting()
{
	d ..mFightingStrategy.Fighting()
}

}

transfer

Class PHA.YX.Design.Program Extends %RegisteredObject
{
/// d ##class(PHA.YX.Design.Program).Strategy() 
ClassMethod Strategy()
{
	#dim context as PHA.YX.Design.Strategy.Context
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.CommonRivalStrategy).%New())
	d context.Fighting()
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.StrongRivalStrategy).%New())
	d context.Fighting()
	s context = ##class(PHA.YX.Design.Strategy.Context).%New(##class(PHA.YX.Design.Strategy.WeakRivalStrategy).%New())
	d context.Fighting()
	q ""
}
}

Output

DHC-APP>w ##class(PHA.YX.Design.Program).Strategy()
遇到了普通的对象,张无忌使用圣火令
遇到了强大的对象,张无忌使用乾坤大挪移
遇到了较弱的对象,张无忌使用太极剑法
 

Thinking

Shopping malls are discounted, 100% off 300, 20% off, normal charges, 50% off 200, 50% off, 9% off and 9% off Interested students can send me to refer to it after realization.

Published 30 original articles · Like 31 · Visits 2327

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/105446580