Let's look at the delegation and events in C,

Source: Internet
Author: User

Let's look at the delegation and events in C,

In a design pattern-Observer pattern, we already know how to apply the observer pattern, but through recent deep learning, we find that the Observer pattern has the following defects:

1. the abstract notification depends on the abstract observer;

2. Each specific observer must call the same method.

For example, the application of the observer mode in the data center charging system is shown in:


This design has the following problems:

1. Abstract notification users need to add all the specific objects in the abstract observer to the collection for sending notification messages to each specific observer;

2. When sending a notification message, the methods used to be notified in the specific observer must be the same; otherwise, the notification owner cannot traverse the notification;

3. There is nothing like updating the balance in the card, updating the student's computer status, and saving the student's computer records. The abstract observer is somewhat far-fetched.

After hard thinking, I found that using the delegate and event mechanism in C # To solve the above problem, the code structure is as follows:

Abstract notifiers:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public interface Informer {void y (); string Action {get; set ;}}}
Specific notifiers-normal dismounting:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public class Down: Informer {// declare a delegate public delegate void EventHandler (); // Add event Update public event EventHandler Update to delegate EventHandler; public void Policy () {this. update () ;}private string action; public string Action {get {return action ;}set {action = value ;}}}}
Specific notifiers -- force drop:
Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public class ForceDown: Informer {// declare a delegate public delegate void EventHandler (); // Add an event Update public event EventHandler Update to the delegate EventHandler; public void Policy () {this. update () ;}private string action; public string Action {get {return action ;}set {action = value ;}}}}
We do not need to abstract the observer. Instead, we need to update the internal balance of the card as follows:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public class UpdateBalance {protected Informer dn; public UpdateBalance (Informer dn) {this. dn = dn;} public void ModifyBalance () {Console. writeLine ("{0 }! The card will be down and the balance will be updated! ", Dn. Action );}}}
Specific observer -- Update Students' Computer status:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public class OffLine {protected Informer dn; public OffLine (Informer dn) {this. dn = dn;} public void DelOnlineInfo () {Console. writeLine ("{0 }! The card is going to go offline. Delete the card information in the machine table! ", Dn. Action );}}}
Specific observer -- save the Students' Computer records:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {public class SaveRec {protected Informer dn; public SaveRec (Informer dn) {this. dn = dn;} public void SaveLineRec () {Console. writeLine ("{0 }! The card needs to be down and the computer record information will be saved! ", Dn. Action );}}}
Main method:

Using System; using System. collections. generic; using System. linq; using System. text; namespace observer mode _ delegate {class Program {static void Main (string [] args) {Down dn = new Down (); OffLine ol = new OffLine (dn ); updateBalance up = new UpdateBalance (dn); SaveRec sr = new SaveRec (dn); dn. update + = new Down. eventHandler (ol. delOnlineInfo); dn. update + = new Down. eventHandler (up. modifyBalance); dn. update + = new Down. eventHandler (sr. saveLineRec); dn. action = "swipe your card to get down"; dn. notify ();}}}

The program execution result is as follows:


Through the execution results, we can find that the normal down event dn in the main method. after an Update occurs, it is delegated to go Down. eventHandler is bound to dn. the three methods on the Update event are ol. delOnlineInfo, up. modifyBalance, sr. savelineRec is executed to achieve the effect of the observer mode.

Now we can find that abstract notifiers no longer rely on abstract observers, because abstract notifiers notify specific observers by entrusting them. The methods called in specific observers do not need to be consistent, because you only need to bind the method you want to execute to the event. The specific notification does not have to have commonalities, so you do not have to abstract these three classes. The delegated event perfectly solves the above problems.


I am a beginner in C language. Now let's look at the C programming language and see the following code.

In the C language, or more accurately, it is said that the end of file is in the C standard library ). In the while LOOP, EOF is used as the end mark of the file, which must be a text file. In text files, data is stored in the form of ASCII code values of characters. We know that the ASCII code value ranges from 0 ~ 255.-1 is not possible, so EOF can be used as the end mark of the file.

Want to learn arm; now let's look at the C language. Is linked list important in C? Some people say that it involves very little, and some people say that they want to learn the C Data Structure of ARM,

Hello, there are many options for learning embedded, and the focus of knowledge in each direction is different. Let's take a look at the following:
If you are working on the software layer, you will be divided into application software development, operating system development, device-driven development, and so on.
Because there are few embedded system resources and the corresponding application software is also small, there may be many open-source modules prepared by others, generally less linked lists, and more algorithms are used, if you need to read the open source code, you need to understand the data structure. Pointers are also commonly used.
To develop a complete system that involves a large amount of data structure knowledge, you must master the data structure and as many algorithms as possible.
Device-driven development is a bit difficult to deal with the physical layer. You need to understand the compiling of some protocols and machine programming, and also use data structures (such as LCD font and graphic library ), or if you are writing programs to run on bare metal systems, you will try to use algorithms to reduce the amount of code to speed up.
Therefore, if you choose the software direction, you can't do without the data structure and algorithms. If you want to learn them, you should try to learn them. The hardware design is not much to mention.
We recommend a book on data structures and algorithms to help you understand the role of this book. (Chinese Version)

To learn about ARM, you must first understand the composition of an embedded system and then learn it in a certain direction. First, you can use the processor to control peripheral devices on the Development Board to familiarize yourself with the use of ARM and other chips. This is related to the C language and internal structure of arm (learning the C language development of arm, if you have the ability, you can also understand the compilation and use it if necessary.) then, you can see how to run the system. Here, you can read a set of books from the power Publishing House to help you understand it.
The time is not long. Let's talk about it here.

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.