using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Command { //信件接收者接口 public interface IReceiver { //收件人收到信件读取 void readMail(String message); } //信件接收者 public class Receiver : IReceiver { public void readMail(String message) { Console.WriteLine("收件人读取信件:" + message); } } //邮局接口 public interface IPost { //邮局发送信件 void sendMail(String message); } public class Post : IPost { private IReceiver receiver; public Post(IReceiver receiver) { this.receiver = receiver; } public void sendMail(string message) { Console.WriteLine("邮局将信件发给收信人..."); this.receiver.readMail(message); } } //发信人 public class Invoker { private IPost post; public void setPost(IPost post) { this.post = post; } //发信人发送信件 public void postMail(string message) { Console.WriteLine("发信人投递信件给邮局..."); this.post.sendMail(message); } } class Program { static void Main(string[] args) { Receiver receiver = new Receiver(); IPost post = new Post(receiver); Invoker invoker = new Invoker(); invoker.setPost(post); invoker.postMail("您好,好久不见,最近工作忙吗?"); } } }
原文地址:http://blog.csdn.net/ry513705618/article/details/39808459