开发者

Which are C# native built-in design patterns?

开发者 https://www.devze.com 2023-01-22 13:39 出处:网络
Which design patterns are build-in supported by C# regardless framework version? I\'m thinking of patterns such as Observer pattern that can be found in interface IObservable. ObservableCollection, IN

Which design patterns are build-in supported by C# regardless framework version? I'm thinking of patterns such as Observer pattern that can be found in interface IObservable. ObservableCollection, INotifyPropertyChanged etc.

Please pro开发者_JAVA技巧vide with the namespace of the pattern in your answers!


Action<T> (normally used as visitor pattern)

Discover the Design Patterns You're Already Using in the .NET Framework (MSDN Magazine)

Example

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

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

// usage
root.Accept(node => Console.WriteLine(node));


Creational Patterns

Abstract Factory

  • System.Data.Common.DbProviderFactory

Builder

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

Factory Method

  • System.Activator
  • System.Net.WebRequest

Prototype

  • System.ICloneable

Singleton

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

Structural Patterns

Adapter

  • System.IO.StreamReader

Bridge

  • System.Globalization.CultureInfo

Composite

  • System.ComponentModel.IComponent

Decorator

  • System.IO.Stream

Facade

  • System.Environment
  • System.String

Flyweight

  • System.StringComparer

Proxy

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

Behavioral Patterns

Chain Of Responsibility

  • Microsoft.Practices.EnterpriseLibrary.Logging.Logger

Command

  • System.Windows.RoutedEventArgs

Interpreter

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

Iterator

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

Mediator

  • System.Threading.Timer

Memento

  • System.Runtime.Serialization.ISerializable

Observer

  • System.EventHandler
  • System.IObservable

State

  • ??

Strategy

  • System.Collections.Generic.IComparer

Template Method

  • System.Web.UI.Page

Visitor

  • System.Linq.Expressions.ExpressionVisitor


Iterator is one (all collection classes and arrays can use the foreach statement for iteration).

Another is the observer pattern - pretty much this is what events are. In 4.0 the IObservable and ObservableCollection were added.


StringBuilder uses the builder design pattern..
And there is the DbDataAdapter class (adapter pattern).
The Null Object pattern is broadly used as well.


Observer pattern. All our events and delegates are raised through the observer pattern.

Factory pattern. Connection strings and db providers from factory.

Iterator Pattern: Ienumerable, Ienumerators in our foreach statements

Adapter: COM communication. Runtime Callable Wrappers(RCW)

Template: Used in several places esp in ASP.NET classes where you can override to provide new implementation

Proxy: For all our webservice calls. in c# 3.0 we got proxy collections as well.

There could be many more. But these are the ones that came to my mind


Abstract Factory: System.Data.Common.DbProviderFactory


The delegation pattern (that's what the delegates and expresssions are for)


The proxy pattern is used often.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号