Design mode Chain of Responsibility - Project (original)

xiaoxiao2021-03-06  94

Design mode Chain of Responsibility - Project

Recent units received a software project, requiring completion within the specified time, so our project group members began to live, we all know that the machining is required, in fact, our software development is also a process (Procedure) The requirements, that is, first analyze the system by analyzing the system, then the programmer is encoded, and finally the entire system is tested by the tester. Some people may say, I will bias, I have to code first, then analyze the design, of course, you have to do this, but you have to do this, hey, I think you can finally eat. If you don't believe you, try it.

Ok, gossip less, we start:

1. Let's first define a process such as Procedure:

Public interface procedure {

Public Abstract void nextprocedure (Procedure Procedure); // Next process

Public Abstract void ExecuteProcedure (String AIM); // Exercise

}

2. Define the implementation of the procedure interface:

A: Analysis Design Process (DesignProcedure)

Public class designprocedure imports procedure {

Private procedure nextprocedure = null;

Private string procedurename = "design"; // This work name

Public void nextprocedure (Procedure Procedure) {// Next process

Nextprocedure = procedure;

}

Public void executeprocedure (String currentprocedure) {// execution process

if (CurrentProcedure.equals (ProcedureName)) {// If the current process is consistent with the process

System.out.println ("System Analysis Design");

} else {

if (NextProcedure! = NULL) {// If the current process is not complussed, it is transferred to the next step.

NextProcedure.executeProcedure (CurrentProcedure);

}

}

}

}

B: CodeProcedure

Public class codeprocedure imports procedure {

Private procedure nextprocedure = null;

Private string urgedurename = "code"; // This work name

Public void nextprocedure (Procedure Procedure) {// Next process

Nextprocedure = procedure;

}

Public void executeprocedure (String currentprocedure) {// execution process

if (CurrentProcedure.equals (ProcedureName)) {// If the current process is consistent with the process

System.out.println ("Code Work");

} else {if (nextprocedure! = null) {// If the current process and the process do not match the next step

NextProcedure.executeProcedure (CurrentProcedure);

}

}

}

}

C: Test Procedure

Public class testprocedure imports procedure {

Private procedure nextprocedure = null;

Private string procedurename = "test"; // This work name

Public void nextprocedure (Procedure Procedure) {// Next process

Nextprocedure = procedure;

}

Public void executeprocedure (String currentprocedure) {// execution process

if (CurrentProcedure.equals (ProcedureName)) {// If the current process is consistent with the process

System.out.Println ("System Test");

} else {

if (NextProcedure! = NULL) {// If the current process is not complussed, it is transferred to the next step.

NextProcedure.executeProcedure (CurrentProcedure);

}

}

}

}

3, write test classes:

Public class test {

Public static void main (String [] args) {

DesignProcedure Design = New DesignProcedure (); // Analysis Design Process

CodeProcedure code = new codeProcedure (); // encoding process

TestProcedure test = new testprocedure (); // test process

Design.nextProcedure (Code); // Defines the next step of the analysis design process

Code.nextProcedure (TEST); / / Define the next step of the coding process

Design.executeProcedure ("Design"); // Start execution process

Design.executeProcedure ("CODE");

Design.executeProcedure ("Test");

}

4, explanation:

A: CHAIN ​​OF RESPONSIBILITY Definition: CHAIN ​​OF RESPONSIBILITY mode is a series of classes to try to process a request request, which is a loose coupling, and the only common point is to pass the request between them, that is, It comes to a request, A-class processed, if not processed, pass to class B processing, if not processed, pass to C-class processing, which is transmitted like a chain (chain).

B: In this example, the current process is judged in the DESIGNPROCEDURE, the DESIGNPROCEDURE, and the test process, and the current process is judged. If the current process is complussed with the process This process is performed, and if the current process and the process do not match the next step execution.

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

New Post(0)