Knowledge about design patterns in avcap code

Source: Internet
Author: User

I do not have a deep understanding of the design model. Maybe I am using it, but it cannot be raised to the theoretical level. So far, I may be most familiar with the MFC. Next time, I will make a summary of the Windows message mechanism. Today, I want to talk about a design model. I think it's great. At least in my opinion, it's very practical and easy to understand, I call this observer mode "instructor-student teaching mode" by myself ".
People who have compiled the window program must be familiar with the message. This mechanism has indeed brought great convenience to programming. After the idea of object-oriented, this mechanism has evolved into a model that is easy to understand and closer to reality. Two objects can communicate with each other by sending messages. (I remember there was also such a method in multithreading, which is more extensive in the sense). I think of both teachers and students as objects, which is obvious in real life, when the school bell rings, a relationship is established between the two classes. The abstract point is one-to-many, one teacher, multiple students, and students observe the teacher's behavior, the teacher "Broadcasts" knowledge to the students, and the students also respond to a message accordingly. This is a basic Observer Model. One target (teacher), multiple observers (students), and they interact through message (broadcast. You can think about it. Such a model may often appear in our program design. If we can use inheritance methods to make such a model as the base class, then the derived new classes naturally have a message transmission mechanism. How convenient is this? Think of the complex technologies that MFC uses to implement a message mechanism, such as a large number of macros, now we can use this simple method to make a simple code organization style. Its biggest advantage is that each module can use this mechanism to achieve a loose coupling and code can be reused to the maximum extent. Well, this is the theory. (I hate theory very much) How can I use code to implement such a model? Three steps:
1. design an Object List class for "Instructors" to store the "Students" list. You can also store the instructor list for students.
Ii. Design Category.
3. Design the student category.

# Ifndef _ h_cobjectlist __
# DEFINE _ h_cobjectlist __

# Include <afxcoll. h>

Class cobjectlist: Public cptrarray
Cobjectlist (void );
Virtual ~ Cobjectlist (void );

Int getindex (void * inpointer );
Int add (void * inpointer );
Int add (void * inpointer, int inindex );
Void remove (void * inpointer );

Void * getprevious (void * inpointer );
Void * getnext (void * inpointer );
# Endif

The above is a general Object List class.

# Ifndef _ h_cmsgstation __
# DEFINE _ h_cmsgstation __

# Include "cobjectlist. H"

Typedef long messaget;
Typedef long policyt;

Class cmsgreceiver;
Class cmsgstation
Cobjectlist mreceivers;

Cmsgstation (void );
Virtual ~ Cmsgstation (void );

Void addmsgreceiver (cmsgreceiver * inreceiver );
Void removemsgreceiver (cmsgreceiver * inreceiver );

Bool broadcast (messaget inmessage, void * ioparam = 0, void * ioparam2 = 0 );
Bool notify (notifyt innotification, long inparam1 = 0, long inparam2 = 0 );

Const messaget msg_stationdestroyed = 0;
Const messaget msg_policy = 1;

Struct snotifstrustruct
Yyt mnotification;
Cmsgstation * mstation;
Long mparam1;
Long mparam2;

# Endif/_ h_cmsgstation __

The above is the target class (which can be understood as a message Station)

# Ifndef _ h_cmsgreceiver __
# DEFINE _ h_cmsgreceiver __

# Include "cmsgstation. H"

Class cmsgreceiver
Cobjectlist mstations;

Cmsgreceiver (void );
Virtual ~ Cmsgreceiver (void );

Void addmsgstation (cmsgstation * instation );
Void removemsgstation (cmsgstation * instation );

Virtual bool receivemessage (messaget inmessage, void * ioparam,
Void * ioparam2 );
Virtual bool respond (notifyt innotification, cmsgstation * instation,
Long inparam1, long inparam2 );

# Endif/_ h_cmsgreceiver __

This is the observer class. (It can be understood as the receiver of the message)
The code above shows that the instructor adds a new message receiver through the addmessagereceiver () method. In this method, the addmsgstation () method of the receiver is called, to put this target class in your own list to complete the process of mutual notification. Then, the instructor sends messages to the observer he holds using the broadcast () method. In this function, the observer's receivemessage () method is called to send messages, this mode is a bit like the "push" Mode in COM. What does respond and policy mean? haha, it's very simple. Sometimes there are several teachers who assign assignments to students at different times, as a student, you always have to know which teacher assigned it. If you don't know it, do it. Be careful when you make up the exam!
The above is my understanding of this model. In the future, I will also think about some application of this model. I will post it after I have a mature idea. Let's talk about it together, the above remarks must be inaccurate.
I mainly refer to the following two books
1: DirectShow practices Selection
2: design patterns, reusable object-oriented Basics



Added in the source code. When you delete the plug-and-play device, it will be executed to cmsgstation cmsgrecieve.

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: 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.