Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Last active December 19, 2015 18:19
Show Gist options
  • Save yemrekeskin/5998285 to your computer and use it in GitHub Desktop.
Save yemrekeskin/5998285 to your computer and use it in GitHub Desktop.
this code snippet is implementation of adapter design pattern with C#
// Target or Wrapper
public interface IPostOperation
{
void AddPost(string head);
}
// util operation class
public class PostOperation
:IPostOperation
{
public void AddPost(string head)
{
Console.WriteLine(head);
}
}
// client
public class PostRepository
{
public PostRepository(IPostOperation postOperation)
{
this._postOperation = postOperation;
}
public PostRepository()
{
this._postOperation = new PostOperation();
}
private readonly IPostOperation _postOperation;
public void AddPost(string head)
{
this._postOperation.AddPost(head);
}
}
//adaptee
public class ImageService
{
public void AddImage(string head)
{
Console.WriteLine("Video : {0}", head);
}
}
// adapter
public class ImageAdapter
:IPostOperation
{
public ImageAdapter()
{
this._imageService = new ImageService();
}
public ImageService _imageService;
public void AddPost(string head)
{
_imageService.AddImage(head);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment