Are there any design patterns used in the .NET Framework?

前端 未结 7 876
Happy的楠姐
Happy的楠姐 2020-12-22 18:31

I would like to know: are any GoF design patterns are used in the .NET Framework?

BOUNTY:

I have seen the MSDN link below in an answer. Are

7条回答
  •  旧巷少年郎
    2020-12-22 18:50

    The .NET framework uses many of the Gang of Four patterns. Here are just a few examples:

    Creational patterns

    • Abstract Factory: System.Data.Common.DbProviderFactory. Every member function of this class is a factory method.
    • Builder: The WCF channel construction infrastructure.
    • Factory Method:
      • System.Data.IDbConnection.BeginTransaction(). The type of transaction created depends on the underlying IDbConnection implementation.
      • WebRequest.Create() returns a concrete type that depends on the URL scheme.
    • Prototype - used in framework for cloning and serialization
    • Singleton - used as an activation method in WCF, i.e. a web service may be treated as a singleton by the WCF infrastructure. Ditto for .NET Remoting.

    Structural patterns

    • Adapter: The ADO.NET providers, eg System.Data.SqlClient.SqlConnection, System.Data.OleDb.OleDbConnection etc. Each provider is an adapter for its specific database.
    • Composite: many examples
      • System.Windows.Forms.Control and its derived classes.
      • System.Web.UI.Control and its derived classes.
      • System.Xml.XmlNode and its derived classes.
    • Decorator:
      • System.Windows.Controls.Decorator (in WPF).
      • Some implementations of Stream are decorators around an inner stream (e.g. GZipStream, CryptoStream).
    • Facade: System.Xml.Serialization.XmlSerializer. XmlSerializer hides a complex task (that includes generating assemblies on the fly!) behind a very easy-to-use class.
    • Proxy: The web service proxies generated by svcutil.exe and deriving from System.ServiceModel.ClientBase

    Behavioral Patterns

    • Chain of responsibility: System.Web.UI.Control.OnBubbleEvent() and System.Web.UI.Control.RaiseBubbleEvent().
    • Command:System.Windows.Input.ICommand (in WPF).
    • Interpreter: System.Linq.Expressions.Expression and related classes.
    • Iterator: many examples
      • System.Collections.IEnumerable.
      • System.Collections.Generic.IEnumerable.
      • System.Data.IDataReader.
    • Memento: The .NET Serializable pattern is a variation on the Memento pattern.
    • Observer - The .NET event mechanism.
    • Strategy - Sort method in ArrayList
    • Template Method - Render method for custom controls
    • Visitor : System.Linq.Expressions.ExpressionVisitor (used internally by [LINQ])

提交回复
热议问题