Real world example of adapter design pattern

Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Nintendo Switch with its default interface works on a smaller portable screen but with an HDMI adapter it also connects to a TV.

C# representation of the same might look like…

Portable interface and its implementation

public interface IPortableDisplay
{
   Stream GetStream();
}
	
public class PortableGameConsole : IPortableDisplay
{
  public Stream GetStream()
  {
   return gameStream;
  }
}

Adapter code…

public interface IHDMI 
{
  Stream GetHDMIStream();
}

public class PortableConsoleHDMIAdapter : IHDMI
{
  private PortableGameConsole _portableGameConsole;
		
  public PortableConsoleHDMIAdapter(PortableGameConsole portableGameConsole)
  {
     _portableGameConsole = portableGameConsole;
  }
		
  public Stream GetHDMIStream()
  {
    return _portableGameConsole.GetStream();
  }
}

Leave a comment