KnowDotNet Visual Organizer

Design Patterns - Facade Pattern

Practical Design Patterns

by Les Smith
Print this Article Discuss in Forums

Have you learned Design Patterns?  You probably need it on your resume and they will improve your coding.  This article will demonstrate another simple Design Pattern called the Facade Pattern.

This is the second in a series of articles on Design Patterns.  The first article covered the
Singleton Pattern, which is a Creational Pattern.  This article will cover the simplest Structural pattern, called the Facade pattern.  The purpose of this pattern is to hide the complexity of dealing with the methods, etc., of several classes, and present a simple interface for the calling application.  You can view the next article on Design Patterns made simple by clicking Strategy Pattern,

In this article, I will show three of the numerous service classes I have in an application that I am currently working on.  Each of the three classes have multiple methods.  Each of the classes could be called from the main class or module in VB.NET (console application).  However, it is confusing to start off in a main driving method instantiating objects and calling the myriad methods that are involved in the processing of this application.  So to keep the "top down" look of the application simple, I have created a Process Class, using the Facade Pattern, to hide the complexity of the application from the main driving level.  

Shown below, is the UML diagram of the three classes and the relationship to the Facade (Process) Class.  You can see from this partial diagram that there will be multiple classes and multiple methods within the classes.



UML Diagram


Since I wrote the first article using VB.NET, I thought I would shift to C# for this one.  I don't want to show partiality.  First, I will show the code for the three service classes.  The first service class is for downloading and uploading files from/to FTP sites.  The shell code for this class is shown below, and a simple call to the constructor will invoke the dowlnloading of files.  Obviously the processing classes are oversimplified, but they are not the object of this article.

   public class FTPHandler
   {
      FTPLib ftp;
      
private string ftpURL;
      
private string ftpPW;
      
private string ftpUID;
      
private string dataDir;

      
public FTPHandler()
      {
         ftpUID = GetSettings(
"FTPURL");
         ftpURL = GetSettings(
"FTPUID");
         ftpPW = GetSettings(
"FTPPassword");
         dataDir = GetSettings(
"FTPDIR");

         ftp =
new FTPLib(ftpURL, ftpUID, ftpPW);
         ftp.DownLoadFiles(
"*.PGP", dataDir);
      }
// constructor
   }

Next I will a Zip file handling class.  

   public class ZipProcessing
   {
      
private string zipDirectory;
      
private string zipFileName;
      
private ZipComponent zip;

      
private ZipProcessing()
      {
         zipDirectory = GetSettings(
"ZipDir");
         zipFileName = GetSettings(
"ZipFile");
         zip =
new ZipComponent(zipDirectory, zipFileName);
         zip.ZipFiles();
      }
// constructor: ZipProcessing
   } // class

I will not flesh out a copy of the ProcessFiles class, as it will add nothing to the content of the article.  Now, I wll show a copy of the Process Class, which is the Facade.

   public class Process
   {
      
public CallProcessingMethods(string procType)
      {
        
switch (procType)
         {
            
case "FTP":
              
FTPHandler ftp = new FTPHandler();
              
break;
            
case "ZIP":
              
ZipProcessing zip = new ZipProcessing();
              
break;
            
case "PF":
               FileProcessor fp =
new FileProcessor();
              
break;
         }
// switch
      } // Constructor
   }

The main program would call the Process class, passing a string to designate the type of processing to perform.  The whole purpose  of the Facade (Process Class) is to hide any of the details of the processing classes from the main program.  Although the processing classes shown above are very simple, they could be quite complex, but the Facade pattern hides that complexity from the main program.

Have you tried our newest product, Visual Class Organizer?  You'll be amazed how easy it  is to keep the code in your code windows organized.  TRY IT FREE FOR 30  DAYS BY CLICKING HERE.



If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable  time typing hundreds of braces {} daily needlessly.  Try CSharpCompleter for 30  DAYS FREE.



Ask a Question, or  give your feedback on my articles or products by going to the KnowDotNet Forum or by  clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com.


Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing