C # observer

Source: Internet
Author: User

Iron observer

Introduction

The responsibility chain model mentioned in the previous article is a very interesting model. Today, this model is also a very interesting model. Let's just go straight to the topic.

Scenario Introduction: Speaking of using part detection in the previous article, it cleverly decouples callers and processors (it doesn't matter if you haven't read the previous article, but the story will continue to develop ), to check the parts, let the individual stare at them? Or are parts automatically detected after production? There is no doubt that it must be automated.

Let's take a look at the component structure.

1 /// <summary> 2 // Part 3 /// </summary> 4 public class ComponentModel 5 {6 public string Name {get; set ;} 7 public int Value 8 {9 get10 {11 return 5; 12} 13} 14 15}

This part is still the one in the previous article and has not been changed. This is just an example, in order to let friends who have not read the previous article know.

According to the requirements in the scenario, the following two types are defined: ComponentModelFactory type. The ComponentModel type factory is responsible for producing the ComponentModel component and notifying the component objects to be processed separately when the component is completed.
The ExecutionCheck type is the object to be notified. It waits for others to tell it that "a part has been produced and needs to be checked by you". It will not be boring.

1 /// <summary> 2 /// sample ComponentModel type factory has produced the ComponentModel notification ExecutionCheck object to detect 3 /// </summary> 4 public class ComponentModelFactory 5 {6 public static componentModel ComModelFactory () 7 {8 return new ComponentModel (); 9} 10} 11 // <summary> 12 // send the ComponentModel object to check 13 /// </summary> 14 public class ExecutionCheck15 {16 public void ComponentModelCheck (ComponentModel comModel) 17 {18 // send to perform detection 19 // or some other operations 20} 21}

Let's take a look at the call code:

1 ExecutionCheck executionCheck = new ExecutionCheck();2 executionCheck.ComponentModelCheck(ComponentModelFactory.ComModelFactory());3 executionCheck.ComponentModelCheck(ComponentModelFactory.ComModelFactory());4 executionCheck.ComponentModelCheck(ComponentModelFactory.ComModelFactory());

It seems that the problem is solved, but the result is terrible. Although executionCheck is very happy, because it has been active, I can only notify executionCheck in this way, some people may say it would be better to modify the ComponentModelFactory type. I know it is like this:

 1     public class ComponentModelFactory 2     { 3         public static ComponentModel ComModelFactory() 4         { 5             return new ComponentModel(); 6         } 7         public void PastMessage(ComponentModel componentModel,ExecutionCheck executionCheck) 8         { 9             executionCheck.ComponentModelCheck(componentModel);10         }11     }

The call code is adjusted:

1 ComponentModelFactory componentModelFactory = new ComponentModelFactory();2 ExecutionCheck executionCheck=new ExecutionCheck();3 componentModelFactory.PastMessage(ComponentModelFactory.ComModelFactory(), executionCheck);

As a result, there is no problem with the call method, but the ComponentModelFactory type has such a high coupling internally, which cannot be tolerated. You must find a way or mode to solve this problem, otherwise, it will be difficult to renew your services !!!!

 

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

-- Gof

According to the definition of the pattern, the ExecutionCheck type is abstracted first.

1 /// <summary> 2 // abstract ExecutionCheck (Abstract observer) 3 /// </summary> 4 public abstract class ABSExecutionCheck 5 {6 public abstract void ComponentModelCheck (ComponentModel comModel ); 7} 8 9 // <summary> 10 // send the ComponentModel object to detect (observer) 11 // </summary> 12 public class ExecutionCheck: ABSExecutionCheck13 {14 public override void ComponentModelCheck (ComponentModel comModel) 15 {16 // sent to perform detection 17 // or some other operations 18} 19}

Then, the ComponentModelFactory is transformed and abstracted.

1 /// <summary> 2 /// abstract target 3 /// </summary> 4 public abstract class ABSComponentModelFactory 5 {6 protected ABSExecutionCheck absExecutionCheck; 7 public abstract void RegisterABSExecutionCheck (ABSExecutionCheck executionCheck); 8 public abstract void ClearABSExecutionCheck (); 9 public abstract void PastMessage (ComponentModel componentModel); 10}

The ABSComponentModelFactory type must be implemented by the ComponentModelFactory type. From here, we can see that abstract programming is followed by the design principles. Of course, the last PastMessage function is ignored, now let's take a look at the implementation of the ComponentModelFactory (specific goal) of the abstract target ABSComponentModelFactory.

1 /// <summary> 2 /// sample ComponentModel type factory has produced the ComponentModel notification ExecutionCheck object for detection (specific target) 3 /// </summary> 4 public class ComponentModelFactory: ABSComponentModelFactory 5 {6 public static ComponentModel ComModelFactory () 7 {8 return new ComponentModel (); 9} 10 11 public override void partition (ABSExecutionCheck executionCheck) 12 {13 absExecutionCheck = executionCheck; 14} 15 16 public override void ClearABSExecutionCheck () 17 {18 absExecutionCheck = null; 19} 20 public override void PastMessage (ComponentModel componentModel) 21 {22 absExecutionCheck. componentModelCheck (componentModel); 23} 24}

Now let's take a look at the called code:

1 ABSExecutionCheck executionCheck = new ExecutionCheck();2 ABSComponentModelFactory componentModelFactory = new ComponentModelFactory();3 componentModelFactory.RegisterABSExecutionCheck(executionCheck);4 componentModelFactory.PastMessage(ComponentModelFactory.ComModelFactory());

In this way, it is relatively stable. Dynamic injection can be made in the place of the first line of new ExecutionCheck (), which is the same as the factory production object in the last line. This is because the examples are not very rigorous. The definition is one-to-multiple. You can modify the fields in the ABSComponentModelFactory type to the set type. Modify the corresponding implementation class.

 

The observer will talk about this. The next article is the appearance, so stay tuned.

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.