using System;
// Target Interface
public interface INikonZMount
{
void AttachLens();
void TakePicture();
}
// Adaptee (Existing lens with F-mount)
public class NikonFMountLens
{
public void Connect()
{
Console.WriteLine("F-Mount lens is connected.");
}
public void Capture()
{
Console.WriteLine("Picture taken with F-Mount lens.");
}
}
// Adapter
public class FMountToZMountAdapter : INikonZMount
{
private readonly NikonFMountLens _fMountLens;
public FMountToZMountAdapter(NikonFMountLens fMountLens)
{
_fMountLens = fMountLens;
}
public void AttachLens()
{
Console.WriteLine("Adapter: Connecting F-Mount lens to Z-Mount camera.");
_fMountLens.Connect();
}
public void TakePicture()
{
Console.WriteLine("Adapter: Taking picture using F-Mount lens on Z-Mount camera.");
_fMountLens.Capture();
}
}
// Client (Nikon Z-Mount Camera)
public class NikonZMountCamera
{
private readonly INikonZMount _lens;
public NikonZMountCamera(INikonZMount lens)
{
_lens = lens;
}
public void Mount()
{
Console.WriteLine("Mounting lens to Nikon Z-Mount camera.");
_lens.AttachLens();
}
public void Shoot()
{
Console.WriteLine("Nikon Z-Mount camera is taking a picture.");
_lens.TakePicture();
}
}
// Native Z-Mount Lens
public class NikonZMountLens : INikonZMount
{
public void AttachLens()
{
Console.WriteLine("Z-Mount lens is attached directly.");
}
public void TakePicture()
{
Console.WriteLine("Picture taken with Z-Mount lens.");
}
}
public class AdapterPatternDemo
{
public static void Main(string[] args)
{
// Using a native Z-Mount lens
Console.WriteLine("Using native Z-Mount lens:");
NikonZMountCamera zMountCamera = new NikonZMountCamera(new NikonZMountLens());
zMountCamera.Mount();
zMountCamera.Shoot();
Console.WriteLine("\n--------------------\n");
// Using an F-Mount lens with adapter
Console.WriteLine("Using F-Mount lens with adapter:");
NikonFMountLens fMountLens = new NikonFMountLens();
INikonZMount adapter = new FMountToZMountAdapter(fMountLens);
NikonZMountCamera adaptedCamera = new NikonZMountCamera(adapter);
adaptedCamera.Mount();
adaptedCamera.Shoot();
}
}