creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - Strategy Behavioral Design Pattern


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.


The classes and/or objects participating in this pattern are:


  • Strategy
    • declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
  • ConcreteStrategy
    • implements the algorithm using the Strategy interface
  • Context
    • is configured with a ConcreteStrategy object
    • maintains a reference to a Strategy object
    • may define an interface that lets Strategy access its data.
using
System;

namespace DoFactory.GangOfFour.Strategy.Structural
{

// MainApp test application

class MainApp
{
static void Main()
{
Context context;

// Three contexts following different strategies
context = new Context(new ConcreteStrategyA());
context.ContextInterface();

context = new Context(new ConcreteStrategyB());
context.ContextInterface();

context = new Context(new ConcreteStrategyC());
context.ContextInterface();

// Wait for user
Console.Read();
}
}

// "Strategy"

abstract class Strategy
{
public abstract void AlgorithmInterface();
}

// "ConcreteStrategyA"

class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine(
"Called ConcreteStrategyA.AlgorithmInterface()");
}
}

// "ConcreteStrategyB"

class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine(
"Called ConcreteStrategyB.AlgorithmInterface()");
}
}

// "ConcreteStrategyC"

class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine(
"Called ConcreteStrategyC.AlgorithmInterface()");
}
}

// "Context"

class Context
{
Strategy strategy;

// Constructor
public Context(Strategy strategy)
{
this.strategy = strategy;
}

public void ContextInterface()
{
strategy.AlgorithmInterface();
}
}
}

Output expected:


Called ConcreteStrategyA.AlgorithmInterface()
Called ConcreteStrategyB.AlgorithmInterface()
Called ConcreteStrategyC.AlgorithmInterface()

Other pages of interest:
[+] Programming - WCF
[+] Programming - Design patterns in Software
[+] Programming - Factory Method Design Pattern
[+] Forward thinking eCommerce is the next-generation dot com Forward thinking eCommerce is about pr..
[+] Embedded Systems Engineering Embedded software and embedded systems, are those that require 100% o..
[+] IBM has moved further into the compliance market with the acquisition of Texas based BuildForge, I..
[+] C++ creator, Bjarne Stroustrup bares all in Computerworld interview
[+] .NET Framework
[+] Software Testing
[+] Artificial Neural Networks
[+] LINQ in .NET 3.0
[+] Java EE and Java SE
[+] Silverlight 2 on Mobile from Nokia
[+] The Apache Jakarta Project has released JMeter 2.3
[+] Case studies - Software in business


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: