Strategy Design Pattern
This structural code demonstrates the Strategy pattern which encapsulates functionality in the form of an object. This allows clients to dynamically change algorithmic strategies.This pattern is come under Behavioral .
Real world Example:
Every day we are traveling to office using any of the transpotation .Assume that home is 20 Km from from your office. You have own a two wheeler also.
For going to office you can select different options
1)Using two wheelar
2)Using Bus
3)Using Train.
The above three are the different strategy to select by you,
Code
/// <summary>
/// MainApp startup class for Real-World/// Strategy Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
//
</summary>
static void Main()
{
// Two contexts following different strategies
Travelstr trlstr = new Travelstr();
trlstr.SetTravelStrlstr.SetTravelStrategy(new BusTravel());
trlstr.SetTravelStrategy(new Twowheelar());
trlstr.SetTravelStrategy(new TrainTravel());// Wait for user
Console.ReadKey();
}
}/// <summary>
/// The 'Strategy' abstract class
/// </summary>
abstract class TravelStrategy
{
public abstract void Travel();}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class Twowheelar : TravelStrategy
{
public override void Travel()
{
// Default is Twowheelar
Console.WriteLine("Two wheelar list ");
}
}
/// <summary>
/// ConcreteStrategy Class
/// </summary>
class BusTravel : TravelStrategy
{
public override void Travel()
{
Console.WriteLine("Travel by Bus ");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class TrainTravel : TravelStrategy
{
public override void Travel()
{
Console.WriteLine("Travel by Train ");}
}/// <summary>
/// The 'Context' class
/// </summary>
class Travelstr
{
private TravelStrategy _TravelStragy;
public void SetTravelStrategy(TravelStrategy traveltrategy)
{
this._TravelStragy = traveltrategy;
}
}
This structural code demonstrates the Strategy pattern which encapsulates functionality in the form of an object. This allows clients to dynamically change algorithmic strategies.This pattern is come under Behavioral .
Real world Example:
Every day we are traveling to office using any of the transpotation .Assume that home is 20 Km from from your office. You have own a two wheeler also.
For going to office you can select different options
1)Using two wheelar
2)Using Bus
3)Using Train.
The above three are the different strategy to select by you,
Code
/// <summary>
/// MainApp startup class for Real-World/// Strategy Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
//
</summary>
static void Main()
{
// Two contexts following different strategies
Travelstr trlstr = new Travelstr();
trlstr.SetTravelStrlstr.SetTravelStrategy(new BusTravel());
trlstr.SetTravelStrategy(new Twowheelar());
trlstr.SetTravelStrategy(new TrainTravel());// Wait for user
Console.ReadKey();
}
}/// <summary>
/// The 'Strategy' abstract class
/// </summary>
abstract class TravelStrategy
{
public abstract void Travel();}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class Twowheelar : TravelStrategy
{
public override void Travel()
{
// Default is Twowheelar
Console.WriteLine("Two wheelar list ");
}
}
/// <summary>
/// ConcreteStrategy Class
/// </summary>
class BusTravel : TravelStrategy
{
public override void Travel()
{
Console.WriteLine("Travel by Bus ");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class TrainTravel : TravelStrategy
{
public override void Travel()
{
Console.WriteLine("Travel by Train ");}
}/// <summary>
/// The 'Context' class
/// </summary>
class Travelstr
{
private TravelStrategy _TravelStragy;
public void SetTravelStrategy(TravelStrategy traveltrategy)
{
this._TravelStragy = traveltrategy;
}
}
No comments:
Post a Comment