Henry Instruction - Use the Template Method Design Mode .NET Event Processing Mechanism (2)

zhaozj2021-02-16  48

Henry Interse - Using Template Method Design Mode

.NET event processing mechanism (2)

By Kevin McFarlane

Han Rui translation (2002.10.14)

4. Example - Template Method Design Mode Event Processing

The following is an example of a complete use of C # and Visual Basic.net. It consists of three classes: Supplier, ExternalClient, and InternalClient. Supplier triggers event. The other two classes perform this event. INTERNALCLIENT is derived from Supplier.

ExternalClient includes an embedded SuppliR reference. However, it is initialized by an InternalClient reference. Therefore, when the ExternalClient is registered for the Supplier event, it calls an onNameChangeD () of INTERNALCLIENT. The event is then processed by the INTERNALCLIENT's NameChanged (), and finally by the nameclient's NameChanged ().

The output of the code is: InternalClient.onnameChanged

INTERNALCLIENT.NAMECHANGED

ExternalClient.nameChanged 1) C # example:

Using system;

Class test

{

Static void main (string [] args)

{

EXTERNALCLIENT Client = New ExternalClient ();

Client.testsupplier ();

}

}

// When its attribute is set, an event is generated

Public Class Supplier

{

Public Supplier () {}

Public Event EventHandler NameChanged;

Public String Name

{

Get {return name;}

Set {name = value; internaMechanged ();

}

// Internal user, that is, derived class, can rewrite the default behavior

protected virtual void onnamechanged ()

{

/ / Execute the default behavior here

Console.writeline ("Supplier.onNameChanged");

}

// If the internal user (derived class) renovates the default behavior in the onNameChanged, the external user will still accept this event.

Private voidinnamechanged ()

{

// Derived class rewritten default behavior

ONNAMECHANGED ();

// After registration, the user can trigger this event.

IF (NameChanged! = null)

NameChanged (this, new evenetargs ());

}

PRIVATE STRING NAME;

}

// "Internal" user handling the support of the Supplier.nameChanged event, but first to rewrite its default behavior

Public Class InternalClient: Supplier

{

PublicinalClient ()

{

NameChanged = New EventHandler (this.supplier_namechanged);

}

protected override void onnamechanged ()

{

// Rewote the default behavior of Supplier.NameChanged

Console.writeline ("InternalClient.onNameChanged");

Private Void Supplier_NameChanged (Object Sender, Eventargs E)

{

// Handle Supplier.nameChanged

Console.writeline ("INTERNALCLIENT.NAMECHANGED");

}

}

// A "external" user that handles the Supplier.nameChanged event.

Public Class ExternalClient

{

Public externalclient ()

{

// Instantate the Supplier as a reference to an InternalClient instance. When an event is evoked, it triggers the overwritten INTERNALCLIENT.ONNAMECHANGED

Supplier = new internelclient ();

Supplier.namechanged = new eventhandler (this.supplier_namechange);

}

Public void testsupplier ()

{

// The following code triggers an event and processes the Handlers of INTERNALCLIENT and EXTERNALCLIENT.

Supplier.name = "kevin mcfarlane";

}

Private Void Supplier_NameChanged (Object Sender, Eventargs E)

{

// Handle Supplier.nameChanged

Console.writeline ("externalClient.namechange);

}

Private Supplier Supplier;

}

2) VB.NET example:

Module Test

Sub main () 'program entry

DIM Client As ExternalClient = New ExternalClient ()

Client.testsupplier ()

End Sub

End module

'When it is set, an event is generated

Public Class Supplier

Sub new ()

End Sub

Public Event NameChanged As EventHandler

Public property name () AS STRING

Get

Return Mname

END GET

Set (byval value as string)

mname = value

INTERNALONNAMECHANGED ()

End set

End Property

'The internal user, that is, derived class, can rewrite the default behavior

Protected overridable sub ?onnamechanged ()

'Execute the default behavior here

Console.writeline ("Supplier.onNameChanged")

End Sub

Private subinloadNameChanged ()

'Derived class rewritten default behavior

ONNAMECHANGED ()

'Evil events for users

RaiseEvent NameChanged (ME, New EventArgs ())

End Sub

Private mname as string

END CLASS

'"Internal" user handling the support of the support of the support, but first to rewrite its default behavior

Public Class InternalClient

Inherits Supplier

Sub new ()

End Sub

Protected overrides sub onnamechanged ()

'Rewrive the default behavior of support support for Supplier.NameChange

Console.writeline ("InternalClient.onNameChanged")

End Sub

Private subsupplier_namechanged (byval e AS Object, ByVal e as Eventargs) _ handles mybase.namechanged

'Hands Supplier.NameChanged

Console.writeline ("InternalClient.nameChanged")

End Sub

END CLASS

'A "external" user that handles the Supplier.NameChanged event.

Public Class ExternalClient

Sub new ()

'Instantilize Supplier as a reference to an INTERNALCLIENT instance. When an event is evoked, it triggers the overwritten INTERNALCLIENT.ONNAMECHANGED

Msupplier = new internelclient ()

'Register for Supplier.nameChanged events

AddHandler Msupplier.nameChanged, Addressof Msupplier_NameChanged

End Sub

Public Sub TestSupplier ()

'The following code triggers an event and processes the Handlers of INTERNALCLIENT and ExternalClient.

Msupplier.name = "kevin mcfarlane"

End Sub

Private sub msupplier_namechanged (Byval e AS Object, ByVal E as Eventargs)

'Hands Supplier.NameChanged

Console.writeline ("externalClient.namechange)

End Sub

Private Msupplier as Supplier

END CLASS

E-mail: ruigeren@sina.com QQ: 18349592 ---- Declaration: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

转载请注明原文地址:https://www.9cbs.com/read-25557.html

New Post(0)