Facade Pattern
Summary: Provide 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.
Sample Code
public class DigitalCameraFacade
{
ICamera camera;
IStorageManager storageManager;
INetworkManager networkManager;
public DigitalCameraFacade(ICamera camera, IStorageManager storageManager, INetworkManager networkManager)
{
this.camera = camera;
this.storageManager = storageManager;
this.networkManager = networkManager;
}
public void TakePhoto()
{
var photo = camera.TakePhoto();
storageManager.WriteFile(photo);
networkManager.Send(photo);
}
}
public interface ICamera
{
byte[] TakePhoto();
}
public class DSLR : ICamera
{
public byte[] TakePhoto()
{
System.Console.WriteLine("Taking photo.");
return new byte[1024];
}
}
public interface IStorageManager
{
byte[] ReadFile();
void WriteFile(byte[] file);
}
public class SDCardManager : IStorageManager
{
public byte[] ReadFile()
{
System.Console.WriteLine("Reading file to drive.");
return new byte[1024];
}
public void WriteFile(byte[] file) => System.Console.WriteLine("Writing file from drive.");
}
public interface INetworkManager
{
void Connect();
void Send(byte[] file);
}
public class WifiManager : INetworkManager
{
public void Connect() => System.Console.WriteLine("Connecting.");
public void Send(byte[] file) => System.Console.WriteLine("Sending file.");
}
Usage
var digitalCamera = new DigitalCameraFacade(new DSLR(), new SDCardManager(), new WifiManager());
digitalCamera.TakePhoto();
Output
Taking photo.Writing file from drive.
Sending file.
Comments
Post a Comment