Last active
December 19, 2015 18:19
-
-
Save yemrekeskin/5998285 to your computer and use it in GitHub Desktop.
this code snippet is implementation of adapter design pattern with C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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