Design patterns: Facade

A Façade class provides a higher level interface to a set of sub level interfaces. This will make the subsystem easier to use.

The high level interface will hide details of the low level interfaces. This may be complex workflows to use the low level interfaces, for example a mandatory initialization process. The Façade also hides the needed connections and interactions between the low level interfaces. Furthermore it will reduce complexity by removing parameters or result values which are not needed on the high level.

In other words the Façade is a class that provides a set of methods and properties that makes it easier for clients to use a complex subsystem of classes.

The following code shows a possible implementation of the Façade design pattern. In this example some configuration data shall be stored within a file. The data is stored in xml format and for security reasons the file will be encrypted. The low level system offers classes to convert the data, do the encryption and the file handling.

class ConfigurationData
{
    public string MyFirstValue { get; set; }
    public int MySecondValue { get; set; }
}

class XmlConverter
{
    public string ToXml(ConfigurationData data)
    {
        //...serialization
    }

    public ConfigurationData ToConfigurationData(string xml)
    {
        //...deserialization
    }
}

class EncryptionService
{
    public string Encrypt(string rawData)
    {
        //...encryption
    }

    public string Decrypt(string encryptedData)
    {
        //...decryption
    }
}

class FileService
{
    public void WriteFile(string fileName, string data)
    {
        //...write file
    }

    public string ReadFile(string fileName)
    {
        //...read file
    }
}

The client does not need to know these details and therefore we offer a simple to use façade class.

class ConfigurationManager
{
    public void SaveConfiguration(ConfigurationData data)
    {
        string fileName = "MyFile.cfg";

        string rawData;
        string encryptedData;

        XmlConverter converter = new XmlConverter();
        EncryptionService encryptionService = new EncryptionService();
        FileService fileService = new FileService();

        rawData = converter.ToXml(data);
        encryptedData = encryptionService.Encrypt(rawData);
        fileService.WriteFile(fileName, encryptedData);
    }
}

Due to the simple design and the huge effort of a Façade class it is a very common design pattern. It is one of the most commonly used design patterns in a 3-tier architectural model. In such a architecture the presentation layer is the client. The client calls to business layer take place via a Façade, often called the service layer. This service layer hides the complexity of the business layer objects and their interactions.

Furthermore a Façade can be used if you have to deal with confusing or messy legacy classes. You can hide such classes behind a Façade which exposes only what is necessary and presents this needs in an easy to use and well-organized interface.

Last but not least I want to mention that the Façade pattern is sometimes mixed up with the Wrapper pattern. But it is easy to distinguish between both if you have a look to the functionality they provide. A Wrapper offers the same functionality like the sub level interface it hides. It will only change the interface itself for example to solve compatibility issues. In contrast, a Facade offers an simplified and easy to use interface of a complex subsystem. A Facade therefore does not contain the full set of features and possibilities of the subsystem. It will offer the functionality needed by the client only. In summary, a Wrapper is used for compatibility and a Façade is used to simplify interfaces.

Advertisements
Dieser Beitrag wurde unter .NET, C#, Design Pattern veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s