Responsible chain mode

xiaoxiao2021-03-06  108

Many objects are connected by each object.

Form a

chain

.

request

Pass on this chain until an object on the chain decision

Raise this request.

Send this requesting client does not know which object on the chain is most

End handle this request.

The responsible chain may be a straight line, a ring chain or even a tree structure

Part.

You can use the example of "drumming flowers".

Drum people will pass the flowers to Jiamu and start the flower game. Florists were passed by Jiamu, giving Jia Zheng,

Passing Jiagong to Jia Baoyu, also passed Jia Bao Yu to Jia Ring, transferred back to Jiamu, so reciprocated

(See below). When the drum is stopped, there is a flowers in your hand to perform alcohol.

.

Participant - abstract

Abstract class player {

Abstract public void

Handle

(INT I); //

Approach

Private Player Successor, PRIVATE PLAYER SUCCESSOR

;

Public Player () {successor = null;}

Protected Void Setsuccessor (Player Asuccessor) {

Success = asuccessor;

} Public void next (int index) {// transmission method

IF (Success! = NULL) {

Successor.handle (Index);

}

Else {

System.out.println ("Program Terminated.");

}

}

Participants - Specific (Jiajia Participants)

Class Jiamu Extends Player {

Public Jiamu (Player Asuccessor) {

THIS.

Setsuccessor

(Asuccessor);

}

Public void

Handle

(int i) {

IF (i == 1) {

System.out.Println ("Jia Mu Gotta Drink!");

}

Else {

System.out.println ("Jia Mu Passed!");

NEXT

(i);

}

}

}

Drum boots (client)

Public class drumbeater {

Private static Player Player;

Static Public Void Main (String [] ARGS)

{

Player = new

Jiamu

(New

Jiashe

(New

Jiazheng (New Jiabao "

Jiahuan

(NULL))))))))))))))))))))

Player.Handle (4);

}

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

New Post(0)