惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Y
Y Combinator Blog
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
博客园 - 司徒正美
I
InfoQ
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
U
Unit 42

博客园 - love .net FrameWork

SQL Server 2005 中的分区表和索引应用 Windows Forms 分页控件 WPF 插件开发(.NET Framework 3.5 System.Addin) 一生回味的100句凡人语录 经典短句 一生中必看的30个故事 别人看不懂的悲伤 Most of the 10 classic love sentence 5月28日 《魔兽争霸3》微操作基础教程 数据访问抽象基础类 无法将类型“ASP.login_aspx”转换为“System.Web.UI.WebControls.Login” - love .net FrameWork 看到一很好的SQL代码 拼接sql语句 关于ibatis.net框架(NPetshop学习) Factory Method 工厂方法模式(创建型模式) SQL各种日期转换代码大全 在ASP.NET中使用Session常见问题集锦(转) Sys.ArgumentOutOfRangeException: Value must be an integer - love .net FrameWork
Stratrgy 策略模式
love .net FrameWork · 2008-03-03 · via 博客园 - love .net FrameWork

      定义一系列的算法,把他们一个一个的封装起来,并使它们可以相互替换。该模式使得算法可独立使用它的客户而变换。
                                                                                                                  ——《设计模式》GOF

     Stratrgy应用比较广泛,比如,有一个文件,要实现生成两种不同字体的文件,这取决于用户的要求,所以我们要准备几套变量字符替代方案.

先在IDAL层定义一个接口来表示生成文件的方法:

public interface IStrategy
    {
        
void FileType(string Types);
    }

实现接口:

 class StrategyFanti : IStrategy
    {
        
public void FileType(string Types)
        {
            Console.WriteLine(
"繁体文件:{0}", Types);
        }
    }
class StrategyZhongwen : IStrategy
    {
        
public void FileType(string Types)
        {
            Console.WriteLine(
"简体文件{0}", Types);
        }
    }

定义实现方法:

public class Solve
    {
        IStrategy _strategy 
= null;
        
public Solve(IStrategy strategy)
        {
            
this._strategy = strategy;
        }
public void Type(string Types)
        {
            _strategy.FileType(Types);
        }
    }

调用:

 class Program
    {
        
static void Main(string[] args)
        {
            Solve so 
= new Solve(new StrategyFanti());
            so.Type(
"策略模式");
        }
    }