C # Design Patterns - Strategy Pattern

Strategy Mode Introduction

Defines a set of algorithms, each algorithm are packaged together, and the interchangeable therebetween. In a variety of algorithms similar case, if ... else brought complex and difficult to maintain. These algorithms will be encapsulated into a class one, arbitrarily replaced.
Here Insert Picture Description

1, if there is a system in which many classes, the difference between them only in their behavior, then use the strategy pattern allows an object can be dynamically select an action in a number of behavior.
2, a system need to dynamically choose one of several algorithms.
3. If an object has a lot of behavior, if that is not appropriate mode, these acts had to use multiple conditional selection statement to achieve

Demo Mode C # Strategy

Here Insert Picture Description
AbstractLog.cs

namespace StrategyPattern
{
    public abstract class AbstractLog
    {
        public abstract void Write(string msg);
    }
}

FileLog.cs

using System;
namespace StrategyPattern
{
    public class FileLog : AbstractLog
    {
        public override void Write(string msg)
        {
            if( msg.Length > 10)
            {
                throw new Exception("");
            }
            Console.WriteLine("记录日志到文本:{0}", msg);
        }
    }
}

DbLog.cs

using System;
namespace StrategyPattern
{
    public class DbLog : AbstractLog
    {
        public override void Write(string msg)
        {
            Console.WriteLine("记录日志到数据库:{0}", msg);
        }
    }
}

StrategyContext.cs

using System;
namespace StrategyPattern
{
    public class StrategyContext
    {
        AbstractLog log = null;

        public StrategyContext()
        {
            log = new FileLog();
        }

        public StrategyContext(AbstractLog log)
        {
            this.log = log;
        }

        public void Write(string msg)
        {
            try
            {
                log.Write(msg);
            }
            catch (Exception)
            {

                log = new DbLog();
                log.Write(msg);
            }
        }
    }
}

Program.cs

namespace StrategyPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            StrategyContext strategyContext = new StrategyContext();
            strategyContext.Write("aaa");
            strategyContext.Write("bbbbbbbbbbbbbbbb");

            strategyContext = new StrategyContext(new DbLog());
            strategyContext.Write("aaa");
            strategyContext.Write("bbbbbbbbbbbbbbbb");
        }
    }
}

Test Run:
Here Insert Picture Description
it can be seen from this example: The main strategy used polymorphism pattern object-oriented thinking.

Note: If the policy is a system of more than four, we need to consider the use of mixed-mode, problem-solving strategies class expansion.

Resources

https://www.bilibili.com/video/av78515440?t=876&p=9
rookie Tutorial | Design Patterns

Published 18 original articles · won praise 17 · views 2696

Guess you like

Origin blog.csdn.net/chasinghope/article/details/104206924