• Provider Pattern提供者模式和策略模式


    http://www.codeproject.com/Articles/18222/Provider-Pattern

    Introduction 

    Provider pattern is one of the most interesting features that Microsoft introduced in .NET 2. 

    提供者模式,是微软在.net2.0中介绍的最有趣的功能。
    A lot of features including membership providers, roles providers, profile providers, health monitor event providers, site map providers, and more had the same design concept. These features are not changeable but extendable, and that is the beauty of the provider pattern.

    许多功能有相同的设计概念,比如membership providers, roles providers, profile providers, health monitor event providers, site map providers等等。这些功能虽然是不变的,但是是可以扩展的,这就是提供者模式的优雅之处。

    In this article, I will demonstrate the concept of provider pattern itself, and how easy it is to create your own services designed by provider pattern, and make your services extendable and reusable.

    在这篇文章中,我将展示提供者模式的概念,并展示如何通过提供者模式实现你自己的服务,让你的服务变得可扩展和重用

    Provider Pattern in More Detail

    It is not a brand new idea; it came basically from the popular strategy pattern. It is a good idea to have a quick look at the strategy pattern in here.

    提供者模式并非一个全新的主意,它主要从流行的策略模式发展而来。快速浏览下策略模式是个不错的想法。

    I see the strategy pattern as a changeable algorithm depending on different situations or different context. This algorithm is encapsulated in object, this object implements an interface or inherits from an abstracted class.

    我认为的策略模式是一种根据不同的情况或环境,可以改变算法。算法被封装在对象中,对象实现了一个接口或者从抽象类继承。

    For example, the Paintbrush saves the same picture in different formats, GIF, BMP,...

    例如,画刷可以将同一张图片保存为多个不同的形式,Gif,Bmp格式等。

    Or Microsoft Word saves the document in different formats, doc, rtf, txt,... In my work, we used strategy pattern very often.

    再如微软的word文档,可以保存为多种形式,doc,rtf,txt……在的工作中,我们经常使用策略模式。

    Maybe, we will come to the top of our design pattern list, one of the implementations that we used, we used in collection builder to build the same collection in different ways.

    也许,我们来到了设计模式列表的顶端,我们使用的一种实现,在collection builder中我们使用不同的方式来创建同一个collection。

    For example, Customers collection has the same methods and behavior in a certain context, once the context changes, all the behavior changes.

    例如,Customers集合在一个确定的上下文中,有相同的方法和行为,一旦上下文变化了,所有的行为就都改变了。

    We have AllCustomers context and InvoiceCustomer context,ProductCustomer context, and each of them has a different database, sometimes it is a simple table in database or 1-many or many-many relation tables in database.

    我们有AllCustomers上下文以及InvoiceCustomer,ProductCustomer上下文,每一个都有一个不同的数据库。有时它只是数据库中的一张简单表或者1对多或多对多的关联表。

    So we create one collection but with different behaviors. I developed an authentication module.

    所以我们创建了一个有不同行为的集合。我开发了授权模块

    This module is very complex, but after I applied a strategy pattern with composite pattern, it became very simple to maintain, or to extend.

    这个模块是很复杂的,但是在我应用了复合模式的策略模式后,它变得简单可维护并且可扩展

    The user object has a property called Role, and depending on the context the Role changes.

    用户对象有一个叫做Role的属性,根据不同的上下文,属性会变化。

    The strategy pattern is beyond the scope of this article, but it is worth studying and implementing this pattern.

    策略模式超出了本文的范围,但是它值得学习并实现这个模式。

    However, I still can see a few differences between strategy pattern and provider pattern.

    然而,我始终可以发现策略模式和提供者模式之间的区别。

     

    Strategy pattern is a Generic concept, not sticking to a specific technology or global scenarios, but provider pattern in most cases is a configurable service, provided by external source, that you can plug-in to your application, or you may extend this service to create your own custom service, then plug-in to your application.

    策略模式是一个泛型的概念。没有和特定的技术或全局的情境绑定。但是提供者模式在大多数情况下是一个可配置的服务,由外部资源提供。你可以将它插入你的应用。或者你可以扩展服务来创建你自定义的服务,然后在插入到你的应用中。

    The service provider should have the same features or contract, that is defined in the base class as contract, but with different behaviors, different formats or different connectivity like using web services, database(s) or XML files or streams.

    服务提供者应该有相同的功能或者契约,这需要在基类中作为契约来定义,但是有不同的行为,形式,或连通度像web服务、数据库、或者xml文件或流。

    Creating Your Own Provider

    There are 3 major steps to create your own provider.  创建你自己的提供者,有3个主要步骤:

    Step 1: Create Your ServiceBase

    To create your own provider pattern, you must inherit your service fromSystem.Configuration.Provider.ProviderBase class:

    为了创建你自己的提供者模式,你必须从System.Configuration.Provider.ProviderBase继承你自己的服务:

    Step2: Create Your ServiceCollection

    To define your own collection class, you should inherit fromSystem.Configuration.Provider.ProviderCollection.

    为了定义你自己的集合类,你应该从System.Configuration.Provider.ProviderCollection继承。

    Step 3: Implement First Concrete Provider

    You should to implement at least one of the providers and test it carefully before releasing it in your own company library.

    你应该至少实现一个提供者,并且在它发布到你公司的类库前,仔细的测试。

    In the previous code, one of the most important codes is Initialize(…) method. You should retrieve the minimum information that should be in the configuration file.

    在之前的代码中,最重要的代码是Initialize方法。你应该从配置文件中检索出所需的最小信息。

    Step 3: Creating ProviderManger

    Moreover, to simplify the pattern, create a static class ProviderManager that has two static properties;Provider (for default provider), and Providers (for other providers that are registered in application configuration), to simplify the access in runtime.

    此外,为了简化模式,创建一个名为ProviderManager 的静态类,有2个静态属性:Provider 和Providers,确保在运行时可以方便的访问。

    Provider 默认的provider

    Providers给其他在应用程序中注册的providers

  • 相关阅读:
    linux编程:删除文件夹下的空文件
    设置vim的显示颜色
    LVM的实现流程
    LVM(逻辑卷管理器)原理
    Ext2文件的访问与日志文件系统
    Linux系统的Ext2文件系统
    linux 磁盘分区的理解
    postgresql在Linux下的安装
    内存泄漏工具VLD
    关于 正交投影 与 透视投影
  • 原文地址:https://www.cnblogs.com/chucklu/p/4495527.html
Copyright © 2020-2023  润新知