Click here to Skip to main content
15,867,299 members
Articles / All Topics

Most Commonly Used dEsign pAttern

Rate me:
Please Sign up or sign in to vote.
3.89/5 (5 votes)
27 Sep 2008CPOL6 min read 73.2K   19   1
Pattern solves common problem in the software development & steamlines the code


Façade Pattern

Definition

According to the Gang of Four:

Façade Pattern provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Problem: You need to use only a subset of a complex system or you need to interact with the system in a particular way.

Solution: The facade presents a new interface for the client of the existing system to use.

Facade_Pattern_-_Generic_Structure.gif

Practical Examples

Example 1

We can implement this pattern for ”Data Access Application block” provided by Microsoft ® under Enterprise Library.

Why would you like to implement Façade pattern for predefined and already tested classes?

The problem that we faced with Data Access Application block that it is complex and a person who likes to use this block efficiently needs to understand structure.

<shape id="_x0000_i1026" style="WIDTH: 469.5pt; HEIGHT: 187.5pt" type="#_x0000_t75" o:allowoverlap="f"><imagedata src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image003.png">

Data_Access_Application_Block.GIF

[Data Access Application Block class diagram]

To reduce this complexity and time required to understand, we opt to define Wrapper of the component. This wrapper is merely a class, offering all operations through “Data Access Application Block”.

Facade_Implementation_for_Data_Access.jpg

<shape id="_x0000_i1027" style="WIDTH: 6in; HEIGHT: 444pt" type="#_x0000_t75"><imagedata o:title="Facade Implementation for Data Access" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image005.jpg">

Having this approach, we will get the following benefits:

  • Understand “DataAccessWrapper” class would suffix our requirement.
  • DataAccessWrapper is the central place to communicate with Data Access component hence we can amend feature easily, such as logging, exception management.
    Apart from that, we also implemented the feature that when an exception will occur, the wrapper will check if it can be recovered by running the query again.
  • Block can be changed easily -> If there is any vast change in “Data Access Application Block”, it would be handy to change only the wrapper class and we're done.

Example 2

Another practical example I came across is: “Managing ASP.NET session variable”, written by David Hay.

I am trying to show through the below diagram what it is all about. However, to read the original text, please visit this article.

Problem:

Facade_Pattern_-_Direct_Access_to_HttpSessionState.gif

<shape id="_x0000_i1028" style="WIDTH: 6in; HEIGHT: 352.5pt" type="#_x0000_t75"><imagedata o:title="Facade Pattern - Direct Access to HttpSessionState" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image007.png">

Solution (with the help of Façade Pattern):

Facade_Pattern_-_Implementation_in_HttpSessionState.gif

<shape id="_x0000_i1029" style="WIDTH: 6in; HEIGHT: 438.75pt" type="#_x0000_t75"><imagedata o:title="Facade Pattern - Implementation in HttpSessionState" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image009.png">

When to Use Façade?

  • You don’t need to use all the functionality of a complex system and can create a new class that contains all the rules for accessing that system.
  • You want to encapsulate or hide the original system.
  • You want to use the functionality of the original system and want to add some new functionality as well.
  • The cost of writing a new class is less expensive than the cost of everybody learning how to use the original system.

Adapter Pattern

Definition

According to the Gang of Four:

Adapter Pattern: Convert the interface of a class into another interface that the clients expect. Adapter lets classes work together that could not otherwise because of incompatible interface.

Problem: A system has the right data and behavior but the wrong interface. Typically used when you have to make something a derivative of an abstract class.

Solution: The Adapter provides a wrapper with the desired interface.

Adapter_Pattern_-_Generic_Structure.gif

<shape id="_x0000_i1030" style="WIDTH: 6in; HEIGHT: 435.75pt" type="#_x0000_t75"><imagedata o:title="Adapter Pattern - Generic Structure" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image011.png">

Practical Examples

The easiest way to understand the intent of the Adapter pattern is to look at an example of where it is useful.

Example 1

Let's say you want to use Infragistics control: UltraWebGrid, for the presentation layer of the project but this control has not been released. However, Infragistics has provided you Interface and description of each function that would be available in their control.

By considering interface, you have spent a number of hours for the presentation layer development.

When Infragistics released controls, you identified that the function name given by them is different than what you have considered throughout the development.

Adapter_Pattern_-_Infragistics_Problem.gif

<shape id="_x0000_i1031" style="WIDTH: 431.25pt; HEIGHT: 249pt" type="#_x0000_t75"><imagedata o:title="Adapter Pattern - Infragistics Problem" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image013.png">

One solution would be to change your entire presentation layer, and correct function calls. But it would increase the cost of the product!

You talked with Infragistics and they apologize for not updating you. The System Architect at Infragistics suggests that they can provide UltraWebGridAdapter so that there would be minimal changes in your project.

Adapter_Pattern_-_Infragistics_Solution.gif

<shape id="_x0000_i1032" style="WIDTH: 431.25pt; HEIGHT: 249pt" type="#_x0000_t75"><imagedata o:title="Adapter Pattern - Infragistics Solution" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image015.png">

What all you did is to create object of UltraWebGridAdapter class, instead of UltraWebGrid class and the rest is the same.

Adapter Patterns Types

There are two types of Adapter patterns:

  1. Object Adapter pattern – The adapter pattern I have been using is called a object adapter because it relies on one object (the adapting object) containing another (the adapted object).
  2. Class Adapter pattern – Another way to implement the adapter pattern is with multiple inheritance, in this case it is called a class adapter pattern.

Façade Pattern Vs Adapter Pattern

Comparing the façade pattern with the Adapter pattern:

Façade Adapter
Are there existing classes? Yes Yes
Is there an interface we must design to? No Yes
Does an object need to behave polymorphically? No Probably
Is a simpler interface needed? Yes No

Bottom Line: A façade simplifies an interface while an Adapter converts a pre-existing interface into another interface.

Strategy Pattern

Definition

According to the Gang of Four:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Problem: The selection of an algorithm that needs to be applied depends on the client making the request or the data being acted on. If you just have a rule in place that does not change, you don’t need a strategy pattern.

Solution: Separate the selection of an algorithm from the implementation of the algorithm. Allows for the selection to be made based upon context.

Strategy_Pattern_-_Generic_Structure.gif

<shape id="_x0000_i1033" style="WIDTH: 431.25pt; HEIGHT: 254.25pt" type="#_x0000_t75"><imagedata o:title="Strategy Pattern - Generic Structure" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image017.png">

Practical Examples

Example 1

Recently, I am working on a project for the international market. It is in .NET and we do know that .NET is rich to provide classes to handle this. However, we don’t want to use those classes directly. Because there may be the case that at given point in time, those classes are not sufficient to fulfill our requirement.

Say CompareInfo cannot be set in existing culture. However, if we create custom culture through “CultureAndRegionInfoBuilder”, it would allow to specify CompareInfo that was being used during comparison.

Here is what strategy pattern helped me to handle this requirement without affecting client side.

What we did is create an interface IcultureFactory and each.

Bridge Pattern

According to the Gang of Four, the intent of the bridge pattern is to “ decouple an abstraction from its implementation so that the two can vary independently.”

Problem: The derivations of an abstract class must use multiple implementations without causing an explosion in the number of classes.

Solution: Define an interface for all implantations to use and have the derivations of the abstract class use that.

Bridge_Pattern_-_Generic_Structure.gif

<shape id="_x0000_i1034" style="WIDTH: 6in; HEIGHT: 288.75pt" type="#_x0000_t75"><imagedata o:title="Bridge Pattern - Generic Structure" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image019.png">

Abstract Factory Pattern

Problem: Families of related objects need to be instantiated.

Solution: Coordinates the creation of families of objects. Gives a way to take the rules of how to perform the instantiation out of the client object that is using these created objects.

Abstract_Factory_Pattern_-_Generic_Structure.gif<shape id="_x0000_i1035" style="WIDTH: 431.25pt; HEIGHT: 326.25pt" type="#_x0000_t75"><imagedata o:title="Factory Pattern - Generic Structure" src="file:///C:/DOCUME~1/amit/LOCALS~1/Temp/msoclip1/01/clip_image021.png">

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Australia Australia
10+ years of Software Development experience in innovation, project strategy, implementation programs on wide ranging enterprise platforms through excellent technical skills and software processes (waterfall, SDLC & Agile (Scrum, TDD, Kanban).

Domain specialisation across: Payroll/ Accounting, Asset Management, CRM, Recruitment process, Oil and Gas Industries, Procurement, E-Commerce

Hands on experience in Visual Studio .Net 2012/2010, .Net 4.5, Inversion of Control (IoC) through nInject and Unity, ASP.NET MVC 4, JQUERY, JQUERY UI, WCF-RIA, Entity Framework 4.0, Microsoft SQL 2012 R2, TFS 2012,2008, Silverlight 4.0/ WPF, LINQ (DLINQ), C# 4.0, and UML 2.0.

Hands on experience in Android Mobile application developed through Eclipse-SVN IDE.

Supporting Microsoft Dynamics Nav 4.0 since 2006.

Experience in the development of real-time, client- server and highly available web application systems.

Strong understanding of Computer Science fundamentals including OO, data structures, UI patterns and algorithms Solid exposure to unit testing and continuous integration

Potential to work in Microsoft Dynamics CRM and iOS/ iPhone development.

Interpersonal skills, onsite experience, providing problem resolution and insight from a technical perspective.

Expertise with aligning solutions to business strategies and software architecture
Needs. Definition of Critical Success Factors and Measurement Metrics to ensure organisations
can continue to measure effectiveness of strategic initiatives.

Working in Microsoft technologies along with Business Owners has hardened my work ability to deliver software within specified Budgets. I have sharpened my skills in Software technologies along with IT services, and produce the best possible Software Architecture.

Comments and Discussions

 
GeneralInfragistics???? - more gray hair..... Pin
Alex Cherkasov5-Apr-10 18:09
Alex Cherkasov5-Apr-10 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.