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

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

 

Introduction 

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

提供者模式,是微軟在.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.設計模式

在這篇文章中,我將展現提供者模式的概念,並展現如何經過提供者模式實現你本身的服務,讓你的服務變得可擴展和重用app

 

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.ide

提供者模式並不是一個全新的主意,它主要從流行的策略模式發展而來。快速瀏覽下策略模式是個不錯的想法。學習

 

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.測試

我認爲的策略模式是一種根據不一樣的狀況或環境,能夠改變算法。算法被封裝在對象中,對象實現了一個接口或者從抽象類繼承。ui

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

相關文章
相關標籤/搜索