Write the mock object again.

Source: Internet
Author: User

TheProgramIn fact, it is not really successful because an exception occurs when the real business object is used to read data from the real message queue of the computer and the data is converted to the object type and passed to the application (The message. Body is converted to the object type, indicating that no matching format object exists.), I designed the Message Queue reading class to be unrelated to the read data type. It is a common processing program, so the interface is defined as an object, I don't want to violate my design philosophy. In addition, I rushed to write for my friends.CodeIf you do not have an environment, you will not be able to debug this program. In addition, the focus of the above section is on how to use mock to replace real objects. However, you still want to understand it recently. The latest code is as follows:

Code of the Core Module

 Using  System;  Using  System. text;  Using  System. Threading;  Using  System. messaging; Namespace  Queuespace {  //  External dependent interface, which shields mock objects and real objects      Class  Queue {  //  Taking into account the real business processing program, it requires elementtype, mock object does not need          Public   Virtual   Object  Readmessage (type elementtype ){  Return   Null  ;}} //  Mock object, simulating the behavior of a real object      Class  Testqueue: queue {  Private   Int Index = 0  ;  Private   Object [] DATA = { "  Hello  " , "  World  " };  Public   Override   Object  Readmessage (type elementtype ){  While (Index < Data. Length ){  Object Data = Data [Index]; index ++ ;  Return  Data;} thread. Sleep (  60 *60 * 60  );  Return   Null  ;}}  //  Real Business Objects      Class  Workqueue: queue {messagequeue queue = Null  ;  Public  Workqueue (){  String Queuename ="  . \ Private $ \ test  "  ;  If  (Messagequeue. exists (queuename) {queue = New  Messagequeue (queuename );}  Else  {Queue = Messagequeue. Create (queuename, False  ); Queue. setpermissions (  " Everyone  "  , Messagequeueaccessrights. fullcontrol );}}  Public   Override   Object  Readmessage (type elementtype ){  //  Because elementtype is needed to read the real processing code. System. messaging. Message message = Queue. Receive (); message. formatter = New Xmlmessageformatter ( New Type [] {elementtype });  Return ( Object  ) Message. Body ;}}  //  Object Factory      Class  Queuefactory {  Public   Static  Queue makequeue (){  //  Return new testqueue ();              Return   New Workqueue ();}}} 

 

Read code

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Using  Queuespace;  Namespace  Queuetest {  Class  Program { Static   Void Main ( String  [] ARGs) {queue = Queuefactory. makequeue ();  While ( True  ){  //  Read built-in messages, no problem                  Object MSG = queue. readmessage ( Typeof ( String ); Console. writeline ((  Int  ) MSG );}}}} 

 

Write Module

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Using  System. messaging;  Namespace Pushmessage {  Class  Program {  Static   Void Main ( String  [] ARGs) {messagequeue queue = Null  ;  String Queuename = "  . \ Private $ \ test  "  ;  If (Messagequeue. exists (queuename) {queue = New  Messagequeue (queuename );}  Else  {Queue = Messagequeue. Create (queuename, False  ); Queue. setpermissions (  "  Everyone  "  , Messagequeueaccessrights. fullcontrol );}  For (Int I = 0 ; I < 3 ; I ++ ) {Queue. Send (  "  Hello  "  ); Console. writeline (  "  Write it  "  );}}}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.