Issuevision Study Notes (III)-Observer mode in Design Mode

Source: Internet
Author: User
Let's take a look at this time. Issuevision The application of the Design Pattern in, Issuevision Mainly used Observer (Observer) mode and Command (Command) mode. Let's take a look at it today. Observer (Observer) mode in Issuevision In Issuevision Is playing an important role.

This is the definition of "Gang of Four" gof.Observer(Observer) mode ------ defines a one-to-many relationship between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated.
From the definition, we can see that,Observer(Observer) mode logically requires two groups of objects. First, it must havePublisher (publish), Also known as the observedTarget (subject)(In habits, it is calledTarget subjectWhich will be called laterTarget subject).Subscribe)In terms of habitsObserver (observer). A target object corresponds to multiple observer objects. When the target object changes, all observer objects registered in the target object will be notified and automatically updated.

In the ApplicationProgramIn the development process, user interfaces and business logic are often required to be separated to define clear boundaries. because the application must be able to quickly change the user interface and cannot have a joint impact on other parts of the application, and the business logic also changes and requires that all these changes have nothing to do with the user interface.Observer (observer)It is the most commonly used design pattern to solve this problem. It helps to clearly define the boundaries between objects in the system.

It is best to present this form. Viewers (table objects, bar chart objects, and pie chart objects) depend on data objects.SubjectSo all changes to the data object subject will notify them. But they do not know each other's existence, and the table object does not know the existence of other table objects or other bar chart objects. For data objectsSubjectThere can be any number of observers who can be notified when the subject object changes, so that their status is synchronized with the subject:

Let's take a look at the concept.Observer(Observer) mode implementation. GeneralObserver(Observer) I will not talk about the implementation of the pattern here. You can refer to the gof design pattern and Java and pattern books. We will focus on it.Net FrameworkMediumObserver(Observer) mode implementation.

.Net FrameworkIntroducedDelegateAndEventThey provide updates and more powerful methods for implementation.Observer(Observer) mode. (aboutDelegateAndEventFor more information, see the relevant documentation). If you are not familiarDelegateAndEvent, ImplementationObserver(Observer) mode requires a lot of work (as implemented in Java ).IssuevisionTo briefly describe.

InIssuevisionMediumPatternsThe folder contains two files.Isubject. CSAndIobserver. CSThey define the interfaces of the target (subject) object and the viewer (observer) object respectively,CodeAs follows:

Isubject. CS

Namespace issuevision
{
// Isubject is a simple marker interface that supports the implementation
// Of The Observer Pattern in issuevision.
Public interfaceIsubject
{
}
}

Iobserver. CS

Namespace issuevision
{
// Iobserver is a simple interface that supports the implementation of
// Observer Pattern in issuevision.
Public interfaceIobserver
{
Isubject subject
{
Set;
}
}
}

You may find that these two interfaces are almost empty and have no definition. What are the functions of these two interfaces? In fact, the role of defining these two interfaces is mainly the "Standardization" of encoding. As long as the class implements either of these two interfaces, this class is implemented.Observer(Observer) mode, and clearly knows who isSubjectWho isObserver.

IssuevisionInIssuesubjectThe component implements the isubject interface, which isIssuevisionImplementationObserverThe most important part of the (observer) model isIssuevisionThe most complex class. We will analyze it at 1.1 points (of course, the analysisObserver(Observer) mode ):

Issuesubject. CS

Public classIssuesubject: Component, isubject

First define the classIssuesubjectIt is inherited from component and isubject. From this and the class name, we can see that this class is usedObserverIn (observer) ModeTarget (subject)Implemented.Target (subject)Object requirementsObserver (observer)OfRegister (Register)AndNotificationThe component class is used to implementRegister (Register). MostTarget (subject)Object implementation does not meanObserver (observer)References are directly stored in your instance variables. Instead, you can delegate this character to a separate object (usually a container). The component class is such a container, the following code shows how to use a component class container to storeObserver (observer)Object ReferenceRegister (Register)

PublicIssuesubject(Icontainer container): this ()
{
Container. Add (this );
}

Let's take a look at how it works.Register (Register)First, we findObserver (observer)The controls/issuetreeview user control isObserver (observer)Object. It implements the iobserver interface.

Controls/issuetreeview. CS

Namespace issuevision
{
// The issuetreeview user control implements the View Selection UI for issuevision
Public classIssuetreeview: Usercontrol, iobserver
{
 
.....
Private Treeview trvviews;
Private imagelist images;

Private issuesubject m_subject = NULL;
Private const string m_fontname = "tahoma ";

Private icontainer components;

Public Virtual isubject subject // method of the isubject Interface
{
Set
{
M_subject = (issuesubject) value;
.....
}
}

The pane/staffpane user control references thisIssuetreeview, It is alsoObserver (observer)Object.

Pane/staffpane. CS

Namespace issuevision
{
Public classStaffpane: Usercontrol, iobserver
{

....
Private issuetreeview itvviews;

Private icontainer components = NULL;

// Iobserver. Subject
Public isubject subject
{
Set
{
Itvviews. Subject = (issuesubject) value;
}
}

The code for calling this user control in mainform. CS is as follows:

Private issuesubject m_issuesubject = NULL;

.....
M_issuesubject = newIssuesubject(This. components); // call the issuesubject constructor.

Panestaff. Subject = m_issuesubject;

By calling the issuesubject constructorObserver (observer)Object registeredTarget (subject)Object. (via Container. Add ())

This completesObserver(Observer) the first step of the pattern,Observer (observer)Object Registration. Let's take a look at Step 2,Target (subject)How to notify objectsObserver (observer)Object

This requires the useDelegateAndEventNow. Check it out.Issuesubject

Issuesubject. CS

Public classIssuesubject: Component, isubject
{

# Region delagate and event declarations
.......

Public DeleGate void encode (Object sender, eventargs E);
Public DeleGate void lookupdatachangedeventhandler (Object sender, eventargs E);
// conflictdatachanged changes when a conflict is resolved, or new conflicts are
// detected.
Public Virtual event conflictdatachangedeventhandler conflictdatachanged;
// lookupdatachanged is raised when lookup data is downloaded from the server
Public Virtual event lookupdatachangedeventhandler lookupdatachanged ;
......

InIssuesubjectStatement inDelegateAndEvent,Observer (observer)Object RegistrationEvent, WhenIssuesubjectAfter the change, activateEvent, Then allObserver (observer)The object can be notified of this change to activate the corresponding processing.

Check controls/issuetreeview. CS again.

Namespace issuevision
{
// The issuetreeview user control implements the View Selection UI for issuevision
Public classIssuetreeview: Usercontrol, iobserver
{
 
.....
Private Treeview trvviews;

Private issuesubject m_subject = NULL;

Private icontainer components;

Public Virtual isubject subject // method of the isubject Interface
{
Set
{
M_subject = (issuesubject) value;

// RegisterIssuesubjectEvent, and hand it to the relevant method to handle the event
M_subject.lookupdatachanged + = new issuesubject. lookupdatachangedeventhandler (this. subject_lookupdatachanged );
M_subject.conflictdatachanged + = new issuesubject. conflictdatachangedeventhandler (this. subject_conflictdatachanged );
}
}

FinallyIssuesubjectTo activate these events.

Issuesubject. CS

Private void loadissuedata ()
{
.......
M_dataset.datasetname = "issuesubject ";

If (lookupdatachanged! = NULL)
{
Lookupdatachanged(This, eventargs. Empty );
}
}

After a few simple stepsObserver(Observer) mode,. NET FrameworkProvidedDelegateAndEventThe Mechanism greatly simplifies the implementation of the mode.

Copyright yellowwee 2004. All right reserved.

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.