本文介绍了什么样的设计模式,在.NET框架中实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么类在.NET框架实现各种设计模式,如装饰,厂房等?

What classes in the .net framework implement the various design patterns like decorator,factory etc?

推荐答案

那么,你的要求可能是一个非常广泛的列表,如设计模式是各地使用的.NET平台。下面是一些例子我能想到把我的头顶部:

Well, what your asking for is probably a VERY extensive list, as design patterns are use all over the .NET platform. Here are some examples I can think of off the top of my head:

适配器

适配器的图案,桥接系统和平台的一个共同的机制,是在以各种方式在.NET框架实现。一个本在.NET中最prevalent例子是运行时可调用包装或RCW的。 RCW的,与 tlbimp.exe是程序生成,​​提供让.NET托管code很容易通过.NET API调用到传统的COM code适配器。

The adapter pattern, a common mechanism of bridging systems and platforms, is implemented in a variety of ways in the .NET framework. One of the most prevalent examples of this in .NET are Runtime Callable Wrappers, or RCW's. RCW's, generated with the tlbimp.exe program, provide adapters that let .NET managed code easily call into legacy COM code via a .NET API.

工厂方法

工厂方法的模式是可能是最知名的模式之一。它是实现相当普遍在整个.NET框架中,特别是在原始时代,同时也对许多其他人。这种模式在框架中的一个很好的例子是转换类,它提供了许多方法来创建与其他常见的原语常用语。

The factory method pattern is probably one of the most well-known patterns. It is implemented quite commonly throughout the .NET framework, particularly on primitive times, but also on many others. An excellent example of this pattern in the framework is the Convert class, which provides a host of methods to create common primitives from other common primitives.

此外,这种模式的另一种普遍形式是.Parse()和.TryParse()上许多原始和基本类型中的方法。

Additionally, another pervasive form of this pattern are the .Parse() and .TryParse() methods found on many primitive and basic types.

迭代

迭代的图案经由一对接口和一些语言构造,像的foreach和在C#所述屈服关键字实现。该了IEnumerable 接口和通用的对应是由几十个集合在.NET框架的实施,使非常各种各样​​的数据很容易,动态循环设置:

The Iterator pattern is implemented via a couple interfaces and some language constructs, like foreach and the yeild keyword in C#. The IEnumerable interface and its generic counterpart are implemented by dozens of collections in the .NET framework, allowing easy, dynamic iteration of a very wide variety of data sets:

IEnumerable<T>
IEnumerator<T>

foreach(var thing in someEnumerable)
{
   //
}

在C#中的一代产量关键字允许的真正形式的的的迭代器的实现,只有通过循环招致处理一个迭代的成本当迭代要求:

The yeild keyword in C# allows the true form of an iterator to be realized, only incurring the cost of processing an iteration through a loop when that iteration is demanded:

IEnumerable<string> TokenizeMe(string complexString)
{
    string[] tokens = complexString.Split(' ');
    foreach (string token in toekens)
    {
        yield return token;
    }
}

生成器

生成器的模式在.NET框架实现了几声。一对夫妇值得注意的是连接字符串的建设者。连接字符串可以是一个挑剔的事情,并动态地构造它们在运行时,有时是一种痛苦。连接字符串生成器类展示生成器模式理想的:

The Builder pattern is implemented a few times in the .NET framework. A couple of note are the connection string builders. Connection strings can be a picky thing, and constructing them dynamically at runtime can sometimes be a pain. Connection String Builder classes demonstrate the builder pattern ideally:

string connectionString = new SqlConnectionStringBuilder
{
    DataSource = "localhost",
    InitialCatalog = "MyDatabase",
    IntegratedSecurity = true,
    Pooling = false
}.ConnectionString;

其他类在整个.NET框架,如UriBuilder,也实现建造者模式。

Other classes throughout the .NET framework, such as UriBuilder, also implement the builder pattern.

观察

观察的模式是一种常见的模式,允许一个类来观看其它事件。作为.NET 4的,这种模式被支撑在两个方面:经由语言集成事件(紧密耦合观察者),并经由的IObservable / IObserver接口(松散耦合事件)

The observer pattern is a common pattern that allows one class to watch events of another. As of .NET 4, this pattern is supported in two ways: via language-integrated events (tightly coupled observers), and via the IObservable/IObserver interfaces (loosely coupled events).

经典语言事件使用的代表的,或者强类型的函数指针,以跟踪事件回调中的事件的属性。事件,触发时,将按顺序执行每个被跟踪的回调。像这样的事件被普遍地用于整个.NET框架。

Classic language events make use of delegates, or strongly-typed function pointers, to track event callbacks in event properties. An event, when triggered, will execute each of the tracked callbacks in sequence. Events like this are used pervasively throughout the .NET framework.

public class EventProvider
{
    public event EventHandler SomeEvent;

    protected virtual void OnSomeEvent(EventArgs args)
    {
        if (SomeEvent != null)
        {
            SomeEvent(this, args); // Trigger event
        }
    }
}

public class EventConsumer
{
    public EventConsumer(EventProvider provider)
    {
        provider.SomeEvent += someEventHandler; // Register as observer of event
    }

    private void someEventHandler(EventArgs args)
    {
        // handle event
    }
}

在.NET框架4新的松散耦合事件。这些是通过实施完成的的IObservable&LT;出T&GT; IObserver&LT;在T&GT; 接口,可以更直接地支持原来观察的设计模式。尽管不受任何我所知道的.NET框架类型直接执行,用于图案的核心基础设施是.NET 4的一个组成部分

New with the .NET 4 framework are loosely coupled events. These are accomplished by implementing the IObservable<out T> and IObserver<in T> interfaces, which more directly support the original Observer design pattern. While not directly implemented by any .NET framework types that I am aware of, the core infrastructure for the pattern is an integral part of .NET 4.

public class SomethingObservable: IObservable<SomethingObservable>
{
    private readonly List<IObserver<SomethingObservable>> m_observers;

    public IDisposable Subscribe(IObserver<SomethingObservable> observer)
    {
        if (!m_observers.Contains(observer))
        {
            m_observers.Add(observer);
        }
        var unsubscriber = new Unsubscriber(m_observers, observer)
        return unsubscriber;
    }

    private class Unsubscriber: IDisposable
    {
        public Unsubscriber(IList<IObserver<SomethingObservable>> observers, IObserver<SomethingObservable> observer)
        {
            m_observers = observers;
            m_observer = observer;
        }

        private readonly IList<IObserver<SomethingObservable>>  m_observers;
        private readonly IObserver<SomethingObservable> m_observer;

        public void Dispose()
        {
            if (m_observer == null) return;
            if (m_observers.Contains(m_observer))
            {
                m_observers.Remove(m_observer);
            }
        }
    }
}

装饰

修饰的图案是通过一个单一的基本类型提供行为替代重新presentations,或形式,的方法。很多时候,一组共同的功能是必需的,但这些功能的实际实施需要改变。这在.NET框架的一个很好的例子就是Stream类及其衍生物。在.NET中的所有数据流提供相同的基本功能,但每个数据流的功能是不同的。

The decorator pattern is a way of providing alternative representations, or forms, of behavior through a single base type. Quite often, a common set of functionality is required, but the actual implementation of that functionality needs to change. An excellent example of this in the .NET framework is the Stream class and its derivatives. All streams in .NET provide the same basic functionality, however each stream functions differently.

    • 的MemoryStream
    • BufferedStream
    • 的FileStream
      • IsolatedStorageFileStream
      • Stream
        • MemoryStream
        • BufferedStream
        • FileStream
          • IsolatedStorageFileStream
          • AnonymousPipeClientStream
          • AnonymousPipeServerStream
          • NamedPipeClientStream
          • NamedPipeServerStream

          很多很多其他的设计模式的.NET框架中使用。 .NET的几乎每一个方面,从语言到框架的基本运行时的概念,是基于常用的设计模式。的.NET Framework显著部分,诸如ASP.NET中,在本身的图案和。举个例子来说,ASP.NET MVC框架,这是MVC的Web变的实现,或的模型 - 视图 - 控制器的。在WPF和Silverlight UI框架直接支持一个叫做MVVM模式,或的模型 - 视图 - 视图模型的。 ASP.NET管道本身就是模式的集合,其中的拦截过滤器页控制器路由器的,等等。最后,一个最常用的模式,组成的,用如此广泛的.NET框架,这可能是整个框架的最根本的模式之一。

          Many, many other design patterns are used within the .NET framework. Almost every aspect of .NET, from language to framework to fundamental runtime concepts, are based on common design patterns. Significant portions of the .NET framework, such as ASP.NET, are in and of themselves patterns. Take, for example, the ASP.NET MVC framework, which is an implementation of the web variant of MVC, or Model-View-Controller. The WPF and Silverlight UI frameworks directly support a pattern called MVVM, or Model-View-ViewModel. The ASP.NET pipeline itself is a collection of patterns, including intercepting filter, page controller, router, etc. Finally, one of the most commonly used patterns, composition, is used so extensively in the .NET framework that it is probably one of the most fundamental patterns of the entire framework.

          这篇关于什么样的设计模式,在.NET框架中实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:28