`
chenying998179
  • 浏览: 25237 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

The Strategy Pattern Example in C#

阅读更多

The Strategy Pattern is a proven design construct to vary operations or algorithms independently from the clients that use it. The pattern underwrites the Open / Closed Principle of S.O.L.I.D., stating that a class should be open to extension, but closed to modification. It also keeps a clean separation of concerns.

Lets say we go to our local car wash. We buy a car washing program at the store. We then drive our car inside the washing machine where it starts to execute the washing program. When we apply the Strategy Pattern to this model, we come up with something like shown below.

First we create a washing program strategy interface. We implement the interface to express our different washing programs. In this case we have to flavors; Basic and Deluxe. For simplicity the strategy ‘algorithm’ Wash() only writes a program process description to the console window. Normally we would find fancy calculations of some sort here.

using System;

namespace StrategyPattern
{
    public interface ICarWashStrategy
    {
        void Wash(Car car);
    }

    public class BasicProgram : ICarWashStrategy
    {
        #region ICarWashStrategy Members

        public void Wash(Car car)
        {
            Console.WriteLine(
                "** Starting Basic Program on {0} **",
                car.Name);
            Console.WriteLine("Water");
            Console.WriteLine("Soap");
            Console.WriteLine("Clean");
            Console.WriteLine("Dry");
            Console.WriteLine("** DONE **");
        }

        #endregion
    }

    public class DeluxeProgram : ICarWashStrategy
    {
        #region ICarWashStrategy Members

        public void Wash(Car car)
        {
            Console.WriteLine(
                "** Starting Deluxe Program on {0} **",
                car.Name);
            Console.WriteLine("Pre Wash");
            Console.WriteLine("Adding a lot of Water");
            Console.WriteLine("Spraying Hot Soap");
            Console.WriteLine("Tire Brush");
            Console.WriteLine("Wrap-Around Washers");
            Console.WriteLine("Rinse and Wax");
            Console.WriteLine("Soft Dry");
            Console.WriteLine("** DONE **");
        }

        #endregion
    }
}

The CarWashService takes the washing program (the strategy) we have bought at the shop and initializes the washing machine with it.

namespace StrategyPattern
{
    internal class CarWashService
    {
        private readonly ICarWashStrategy _carWashStrategy;

        public CarWashService(ICarWashStrategy carWashStrategy)
        {
            _carWashStrategy = carWashStrategy;
        }

        public void Wash(Car car)
        {
            _carWashStrategy.Wash(car);
        }
    }
}

Using the CarWasService in an application should be pretty straight forward. We drive in our Opel Manta and start the washing operation by calling the Wash() method.

using System;

namespace StrategyPattern
{
    internal class Program
    {
        private static void Main()
        {
            var car = new Car { Name = "Opel Manta" };
            var washProgramStrategy = new DeluxeProgram();
            var carWash = new CarWashService(washProgramStrategy);

            carWash.Wash(car);

            Console.ReadLine();
        }
    }
}

Strategy Pattern Console

We can apply any washing program to the CarWashService we want. If new Washing Programs are added later, we don’t have to modify the CarWashService.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics