测试:mock对象
using System;
using System.Text;
using System.Threading;
using System.Messaging;namespace QueueSpace
{//外部依賴的接口,它屏蔽了mock對象和真對象class Queue{public virtual object readMessage(){return null;}}//mock對象,模擬真對象的行為class TestQueue : Queue{private int index = 0;private object[] Data = { "hello","world"};public override object readMessage(){while (index < Data.Length){object data = Data[index];index++;return data;}Thread.Sleep(60*60*60);return null;}}//真正的業務對象class WorkQueue : Queue{MessageQueue queue = null;public WorkQueue(){string queueName = ".\\Private$\\MSMQDemo";if (MessageQueue.Exists(queueName)){queue = new MessageQueue(queueName);}else{queue = MessageQueue.Create(queueName, false);queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);}}public override object readMessage(){System.Messaging.Message message = queue.Receive();string s =(string)message.Body;return (object)s;}}//對象工廠class QueueFactory{public static Queue makeQueue(){return new TestQueue();}}
}
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using QueueSpace;namespace ConsoleApplication1 {class Program{static void Main(string[] args){Queue queue = QueueFactory.makeQueue();while (true){object msg = queue.readMessage();Console.WriteLine((string)msg);}}} }?
轉載于:https://www.cnblogs.com/code-style/archive/2012/09/22/2698210.html
總結
- 上一篇: iPhone OS框架 介绍
- 下一篇: thinkphp3.1的新功能