Follow by Email

facade pattern

Facade pattern is one of the Structural Pattern.
Main purpose of the facade pattern is to encapsulate whole subsystem under one class or in simple words one class represents the whole subsystem.
Client only talks to the facade class and not to other classes which are inside the system.
This pattern make complex code very easy.



Following diagram shows how the facade pattern works.
Main class only talks to the facade class which in turns talks to other class in the subsystem.
Example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
facade fd = new facade();
fd.methodfacade();
Console.ReadLine();
}
}
class subclass1
{
public void method1()
{
Console.WriteLine("Subclass1");
}
}

class subclass2
{
public void method2()
{
Console.WriteLine("Subclass2");
}
}

class subclass3
{
public void method3()
{
Console.WriteLine("Subclass3");
}
}

class facade
{
private subclass1 one = new subclass1();
private subclass2 two = new subclass2();
private subclass3 three = new subclass3();
public void methodfacade()
{
one.method1();
two.method2();
three.method3();
}
}
}

0 comments:

Post a Comment