哪些是C#本机内置设计模式?

Ami*_*aei 22 c# design-patterns

无论框架版本如何,C#都支持哪些内置设计模式?我正在考虑可以在接口IObservable中找到的Observer模式等模式.ObservableCollection,INotifyPropertyChanged等.

请在答案中提供模式的命名空间!

jga*_*fin 26

Action<T> (通常用作访客模式)

发现您已经在.NET Framework中使用的设计模式(MSDN Magazine)

public class Root
{
    //Private and not exposed in a IList property = Encapsulation
    private List<Node> _nodes = new List<Node>(); 

    public void Visit(Action<Node> visitor)
    {
        // Controlled enumeration, can for instance handle exceptions in here.
        foreach (var item in _nodes)
        {
            visitor(node);
        }
    }
}

// usage
root.Visit(node => Console.WriteLine(node));
Run Code Online (Sandbox Code Playgroud)


小智 8

创作模式

抽象工厂

  • System.Data.Common.DbProviderFactory

生成器

  • System.Text.StringBuilder
  • System.Data.Common.DbConnectionStringBuilder

工厂方法

  • System.Activator
  • System.Net.WebRequest

原型

  • System.ICloneable

独生子

  • System.StringComparer.InvariantCulture
  • System.StringComparer.InvariantCultureIgnoreCase

结构模式

适配器

  • 就是System.IO.StreamReader

  • System.Globalization.CultureInfo

综合

  • System.ComponentModel.IComponent

装饰

  • System.IO.Stream

正面

  • 统环境
  • System.String

飞锤

  • System.StringComparer

代理

  • System.Net.WebClient
  • System.Runtime.Remoting.Proxies.RealProxy
  • System.ServiceModel.ICommunicationObject

行为模式

责任链

  • Microsoft.Practices.EnterpriseLibrary.Logging.Logger

命令

  • System.Windows.RoutedEventArgs

翻译员

  • System.IFormatProvider
  • System.Text.RegularExpressions.Regex

迭代器

  • System.Collections.IEnumerable
  • System.Data.IDataReader

中间人

  • System.Threading.Timer

纪念品

  • System.Runtime.Serialization.ISerializable

观察

  • System.EventHandler
  • System.IObservable

  • ??

战略

  • System.Collections.Generic.IComparer

模板方法

  • System.Web.UI.Page

游客

  • System.Linq.Expressions.ExpressionVisitor


Ode*_*ded 6

Iterator是一个(所有集合类和数组都可以使用该foreach语句进行迭代).

另一个是观察者模式 - 这就是事件的真实情况.在4.0中添加了IObservable和ObservableCollection.