Strategy model learning and case realization of design patterns

Strategy mode

When a problem has many solutions and we want to change the solution used at runtime, we can use the strategy mode to solve it. For example, calculating the shopping cart price (original price, discount, full return), travel mode (metro, bus, bicycle)

Features

  1. Encapsulate a series of algorithms and can replace each other
  2. Solve the problem of using multiple condition judgments
  3. There is an interface to standardize these algorithms

achieve

In a game, a character has three skills: A, B, and C,

  • Skill A: Inflict damage of attack power*150%
  • Skill B: While causing basic damage, an additional 100 damage is added
  • Skill C: When causing damage, change the basic attack power according to your own state—攻击力 *(1 加或减 (-1,3])*150%

Attack.cs

interface Attack
{
	double hurtVal(double baseAttack);
}

BaseHurt.cs

class BaseHurt : Attack
{
	public double hurtVal(double baseAttack)
	{
		return baseAttack * 1.5;
	}
}

ExtraHurt.cs

class ExtraHurt : Attack
{
	private double extraVal;
	public ExtraHurt(double extraVal)
	{
		this.extraVal = extraVal;
	}

	public double hurtVal(double baseAttack)
	{
		return baseAttack * 1.5 + extraVal;
	}
}

StateHurt.cs

class StateHurt : Attack
{
	//增益或减益大于-1
	double stateVal;
	public StateHurt(double stateVal)
	{
		this.stateVal = stateVal;
	}

	public double hurtVal(double baseAttack)
	{
		return baseAttack * (1 + stateVal) * 1.5;
	}
}

AttackContext.cs

class AttackContext
{
	private Attack attack;
	
	public AttackContext(Attack attack)
	{
		this.attack = attack;
	}

	public double getHurtVal(double BaseVal)
	{
		return attack.hurtVal(BaseVal);
	}
}

Program.cs

class Program
{
	static void Main(string[] args)
	{
		//技能A  基础伤害
		AttackContext AC = new AttackContext(new BaseHurt());
		double AHurtVal = AC.getHurtVal(100);
		ole.WriteLine("A技能造成伤害{0}",AHurtVal);

		//B技能  额外伤害
		AttackContext BC = new AttackContext(new ExtraHurt(150));
		Console.WriteLine("B技能伤害{0}",BC.getHurtVal(100));

		//C技能  增益减益伤害
		AttackContext CC = new AttackContext(new StateHurt(1.5));
		Console.WriteLine("C技能伤害{0}",CC.getHurtVal(100));
		Console.ReadKey();
	}
}

to sum up

  • Advantages: 1. The algorithm can be switched freely. 2. Avoid using multiple condition judgments. 3. Good scalability.
  • Disadvantages: 1. Strategy categories will increase. 2. All strategy categories need to be exposed.
  1. Intent : Define a series of algorithms, encapsulate them one by one, and make them interchangeable.
  2. The main solution : in the case of multiple similar algorithms, the use of if...else is complicated and difficult to maintain.
  3. When to use : A system has many, many classes, and what distinguishes them is their direct behavior.
  4. How to solve : Encapsulate these algorithms into classes one by one and replace them arbitrarily.
  5. Key code : implement the same interface.
  6. Improvement : Can cooperate with factory mode to create Context instance

Guess you like

Origin blog.csdn.net/weixin_36382492/article/details/84839712
Recommended