IT Management

Strategy Pattern – Cybarlab


Consider an action video game where a warrior needs to combat a great deal of bad guys. At the start of the video game the fighter has 100% power and health condition. At the starting the fighter remains in an aggressive state of mind. Slowly the warrior’s health started to decrease and he enters into protective state of mind. Then he begins trying to find food, when he gets food his health ends up being 100% once again and he enters into aggressive state of mind once again.

For this think about the following Fighter Class.

 public class Fighter
{
public int Health {get; set;}
public space ChangeMood( String state of mind).
{
switch (state of mind).
{
case "Aggressive":.
SetAnggsiveBehavior();.
break;.
case "Defensive":.
SetDefensiveBehavior();.
break;.
}
}
personal space SetDefensiveBehavior().
{
Console.WriteLine(" Defensive State Of Mind");.
}
personal space SetAnggsiveBehavior().
{
Console.WriteLine(" Aggressive State Of Mind");.
}
} 
It’s matching customer code is:
 class Program.
{
fixed space Main( string[] args).
{
var fighter = brand-new Fighter();.
var random = brand-new Random();.

fighter.Health = random.Next( 1, 100);.

if (fighter.Health < 50).
{
fighter.ChangeMood(" Aggressive");.
}
Console.ReadKey();.
}
} 

Here, if the fighter's health is above 50, we take the fighter to aggressive mode and if it is listed below 50, we take him to protective mode.

However if we offer the class library of this code to another person and he wishes to produce a brand-new state of mind for the fighter, he can't. We need to offer him the complete source code of our Fighter class. Then our things oriented shows will be the infraction of OCP (Open Close Concept). OCP implies a class or entity will just be open for extension however closed for adjustment.

It is likewise an infraction of the Concept of Least Understanding guideline. Due to the fact that the Fighter class needs to understand all the state of minds all the time. Here Fighter class do not require this understanding perpetuity. We can inform him at runtime, you combat in this state of mind now.

Given that the typical function of all fighters is to combat, we can produce a typical user interface IFighter with an approach called Battle ().

 public user interface IFighter.
{
void Battle();.
} 

In reality out approaches can be huge. That's why it is much better to transform them into class

 public class Aggresive: IFighter.
{
public space Battle().
{
Console.WriteLine(" Fighter is now in aggresive state of mind");.
}
} 
 public class Defensive: IFighter.
{
public space Battle().
{
Console.WriteLine(" Fighter is now in defensie state of mind");.
}
} 
 public class Fighter.
{
public int Health {get; set;}
personal IFighter _ fighter;.
public space ChangeMood( IFighter fighter).
{
_ fighter = fighter;.
_ fighter.Fight();.
}
} 
 class Program.
{
fixed space Main( string[] args).
{
var fighter = brand-new Fighter();.
var random = brand-new Random();.

fighter.Health = random.Next( 1, 100);.

if (fighter.Health < 50).
{
fighter.ChangeMood( brand-new Aggresive());.
}
Console.ReadKey();.
}
} 

Now if we offer the class library of this style to another person and if he wishes to produce a brand-new state of mind for the fighter, he will never ever need to customize the Fighter class. He requires to produce a class for the brand-new mode that will execute IFighter.


Source link