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

前端 未结 7 872
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:43

    The BeginXXX and EndXXX methods in the BCL are based loosely on the Asynchronous Completion Token pattern.

    0 讨论(0)
  • 2020-12-22 18:44

    Here's an article that discusses this very topic:

    Discover the Design Patterns You're Already Using in the .NET Framework

    And now the MVC pattern can be added with ASP.NET MVC. :)

    EDIT: Since your edit / request for more info:

    Here's an article that lists several patterns and where they are used in the framework.
    Structural Design Patterns and .NET Framework 2.0

    The Providers in .NET are all the Provider model pattern:
    ASP.NET 2.0 Provider Model: Introduction to the Provider Model

    The provider patterns in .NET also use the Strategy Pattern.

    The factory pattern is used in several places and here's a sample where it's used in ASP.NET.
    Exploring the Factory Design Pattern

    Here's a webcast on DP's in .NET:
    MSDN Webcast: Design Patterns in .NET

    I haven't watched it so I am not sure how much it goes into how they are used in the Framework...

    As already mentioned in a comment, the GoF patterns are likely all in use in the .NET framework. Where is not exactly the easiest to answer as the framework is massive and unless MS publishes as such listed in some of the examples given it is not always obvious. The more familiar one is with a pattern the more likely you would notice a framework class that was employing it.

    Hopefully the extra links I have added help you.

    Additionally, dofactory has a for sale kit ($79-99) that is about teaching how to use/implement GoF patterns in .NET BUT they do list on the reading they will also explain where MS uses them in the Framework.

    0 讨论(0)
  • 2020-12-22 18:45

    Read the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries". This book will show you the real design patterns that .NET was based on.

    0 讨论(0)
  • 2020-12-22 18:45

    Definitely. For instance, Factory pattern is used in ADO.NET data provider classes. Singleton pattern is used in .NET remoting. Dispose pattern is used in resource management.

    0 讨论(0)
  • 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<TChannel>

    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<T>.
      • 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])
    0 讨论(0)
  • 2020-12-22 19:02

    Yes there are many design patterns used in .Net framework BCL. In fact this is the best Place to look at to learn the design pattern's usage in real term perspective.

    Pattern : Implementation

    Observer Pattern : Implemented as Events & Delegates

    Decorator Pattern: Implemented in System.IO Stream classes

    Iterator Pattern : IEnumerable & Iterator

    Strategy Pattern : Implemented in Linq thru Lambda experssion queries (list.Where(expression))

    Factory Pattern : In System.Net.WebRequest

    Adapter Pattern : TLB Import / RCW (Runtime Calleable wrappers)

    Please check out this video for detailed presentation on this topic.

    https://www.youtube.com/watch?v=riHGGYHVoaQ&list=PL2_Cl8E2bG36PGuBGesHFPVM1s6M02JmN

    0 讨论(0)
提交回复
热议问题