<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>MsCorEE : design pattern</title><link>http://weblogs.asp.net/jgonzalez/archive/tags/design+pattern/default.aspx</link><description>Tags: design pattern</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Provider, Strategy, Adapter pattern discussion</title><link>http://weblogs.asp.net/jgonzalez/archive/2007/02/19/provider-strategy-adapter-pattern-discussion.aspx</link><pubDate>Tue, 20 Feb 2007 05:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1725990</guid><dc:creator>likwid</dc:creator><author>likwid</author><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgonzalez/rsscomments.aspx?PostID=1725990</wfw:commentRss><comments>http://weblogs.asp.net/jgonzalez/archive/2007/02/19/provider-strategy-adapter-pattern-discussion.aspx#comments</comments><description>Some of my coworkers and I were having a discussion today regarding the aforementioned patterns. &amp;nbsp;First we debated the validity of the Provider pattern actually being considered a pattern, or if it was really just a renamed Strategy or Adapter pattern.&lt;br /&gt;&lt;br /&gt;Here is some background on the patterns for reference. &amp;nbsp;I took my definitions from &lt;a href="http://www.dofactory.com/Patterns/Patterns.aspx" title="DOFactory"&gt;DOFactory&lt;/a&gt;. &amp;nbsp;I use their site quite a bit when referencing patterns or when I need a refresher.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Adapter&lt;/strong&gt;&lt;br /&gt;Converts the interface of a class into another interface clients expect. &amp;nbsp;Adapter lets classes work together that couldn&amp;#39;t otherwise because of incompatible interfaces. &amp;nbsp;Forgive the Java naming conventions. &amp;nbsp;=P&lt;br /&gt;&lt;br /&gt;Suppose you have two separate interfaces:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;interface Iterator&lt;br /&gt;{&lt;br /&gt; &amp;nbsp; &amp;nbsp;HasNext()&lt;br /&gt; &amp;nbsp; &amp;nbsp;Next()&lt;br /&gt; &amp;nbsp; &amp;nbsp;Remove()&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;blockquote&gt;interface Enumeration&lt;br /&gt;{&lt;br /&gt; &amp;nbsp; &amp;nbsp;HasMoreElements()&lt;br /&gt; &amp;nbsp; &amp;nbsp;NextElement()&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;These two interfaces are pretty similar, but they have different method names and Iterator is read-write while Enumeration is read-only. &amp;nbsp; For my example, I want to adapt Enumeration to the Iterator interface. &amp;nbsp;In Java most things use the Iterator interface now, but it is possible that some legacy code might still be using the Enumeration interface. &amp;nbsp;This pattern allows us to use the Iterator interface, but provides a method to interoperate with the Enumeration interface as well.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;public class EnumerationIterator : Iterator&lt;br /&gt;{&lt;br /&gt;&lt;blockquote&gt;private Enumeration _Enumeration;&lt;br /&gt;&lt;/blockquote&gt;&lt;blockquote&gt;public EnumerationIterator(Enumeration enum)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; _Enumeration = enum;&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;blockquote&gt;public Boolean HasNext() { return enum.HasMoreElements(); }&lt;br /&gt;public Object Next() { return enum.NextElement(); }&lt;br /&gt;public void Remove() { throw new NotImplementedException(); }&lt;br /&gt;&lt;/blockquote&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;div style="direction: ltr"&gt;&lt;br /&gt;&lt;strong&gt;Strategy&lt;/strong&gt;&lt;br /&gt;Define a family of algorithms, encapsulate each one, and make them interchangeable. &amp;nbsp;Strategy lets the algorithm vary independently from the clients that use it.&lt;br /&gt;&lt;/div&gt;&lt;div style="direction: ltr"&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="direction: ltr"&gt;IComparable is a good example of the Strategy pattern. &amp;nbsp;IComparable defines a single member called CompareTo (Object o). &amp;nbsp;This allows different classes to define different strategies for comparison. &amp;nbsp;As long as the class implements IComparable and the CompareTo member, the implementation is up to the class itself.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Provider&lt;/strong&gt;&lt;br /&gt;I couldn&amp;#39;t exactly find the definition of this pattern on DOFactory, so I looked at &lt;a href="http://weblogs.asp.net/rhoward" title="Rob Howard"&gt;Rob Howard&lt;/a&gt;&amp;#39;s &lt;a href="http://msdn2.microsoft.com/en-us/library/ms972319.aspx" title="article"&gt;article&lt;/a&gt; on MSDN. &amp;nbsp;He states, &amp;quot;Defined, a provider is simply a contract between an API and the Business Logic/Data Abstraction Layer. The provider is the implementation of the API separate from the API itself.&amp;quot;&lt;br /&gt;&lt;br /&gt;This pattern was advocated pretty heavily by the folks at Microsoft during the .NET 2.0 framework development cycle. &amp;nbsp;The Membership API is a good example of the concrete implementation. &amp;nbsp;The Membership API contains several methods and properties like CreateUser, GetAllUsers, and ValidateUser. &amp;nbsp; By default the Membership API uses the built in providers that Microsoft developed. &amp;nbsp;They realized that Joe Developer might want to store his users in SQL Server, and Josie Developer might want to talk to Active Directory for her users. &amp;nbsp;To that end, they created the provider pattern. &amp;nbsp;I won&amp;#39;t rehash the whole thing, but for clarification check out Rob&amp;#39;s article.&lt;br /&gt;&lt;br /&gt;I think the provider pattern also happens to have a bit of Dependency Injection/Inversion of Control flavor to it. &amp;nbsp;It doesn&amp;#39;t specifically use a container technology like Windsor or Spring.NET, but by preferring configuration over convention it allows the developer to configure their API to use a specific provider without a huge amount of coding.&lt;br /&gt;&lt;/div&gt;&lt;div style="direction: ltr"&gt;&lt;br /&gt;&lt;span style="font-weight: bold"&gt;Discussion&lt;/span&gt;&lt;br /&gt;Our conversation today centered around whether the Provider pattern is a unique pattern or just a representation of the Strategy/Adapter patterns. &amp;nbsp;Both Strategy and Adapter seem very similar on the surface, but if you look deeper, they seem to be diametrically opposed to each other. &amp;nbsp;Implementation of the Strategy pattern seems like part of the design process, prior to coding. &amp;nbsp;You establish a need for an interchangeable algorithm and create an interface that supports it. Then you code the different implementations for each strategy. &amp;nbsp;On the other hand, the Adapter pattern seems to be sort of an afterthought. You have an established interface that maybe is used in a lot of places, but you want to adapt it to another more current interface. This is a pretty common scenario when dealing with legacy code.&lt;br /&gt;&lt;br /&gt;I was advocating the idea of the Provider pattern really being a Strategy. &amp;nbsp;As I mentioned earlier, the Membership API might have a different strategy for storing user data between two developers. &amp;nbsp;One developer uses SQL Server and another uses Active Directory. &amp;nbsp;My boss Shannon (sorry, no blog&amp;hellip;yet) stated that you could argue that the Membership API was adapting each of those implementations to fit the needs of the developer.&lt;/div&gt;&lt;div style="direction: ltr"&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="direction: ltr"&gt;We commented that depending on your point of view the Provider pattern could fit either of those patterns, it just depends on when and where you start.&lt;br /&gt;&lt;br /&gt;What do you think?&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1725990" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/adapter/default.aspx">adapter</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/design+pattern/default.aspx">design pattern</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/design+patterns/default.aspx">design patterns</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/designpattern/default.aspx">designpattern</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/designpatterns/default.aspx">designpatterns</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/pattern/default.aspx">pattern</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/patterns/default.aspx">patterns</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/provider/default.aspx">provider</category><category domain="http://weblogs.asp.net/jgonzalez/archive/tags/strategy/default.aspx">strategy</category></item></channel></rss>