Bridge Pattern
Summary: A mechanism that decouples an interface (hierarchy) from an implementation (hierarchy).
Sample Code
Code
public interface IStorage
{
void SaveFile(byte[] bytes);
}
public class InternalStorage : IStorage
{
public void SaveFile(byte[] bytes)
{
System.Console.WriteLine("File sent to disk.");
}
}
public class CloudStorage : IStorage
{
public void SaveFile(byte[] bytes)
{
System.Console.WriteLine("File sent to server.");
}
}
public class DigitalCamera
{
private IStorage storage;
public DigitalCamera(IStorage storage)
{
this.storage = storage;
}
public void TakePhoto()
{
// Simulation of taking a photo from the digital image sensor
byte[] photo = new byte[1024];
System.Console.WriteLine("Photo retrieved.");
// Saving photo
storage.SaveFile(photo);
}
}
Usage
var camera = new DigitalCamera(new CloudStorage());
camera.TakePhoto();
Output
Photo retrieved.File sent to server.
Comments
Post a Comment