如果一个处理不了,就传给链上的小一个来处理。
subject notification subject1
subject notification subject1
subject-->object1-->object2-->object3
interface HelpInterface
{
public void getHelp(int helpConstant);
}
public class FrontEnd implements HelpInterface
{
final int FRONT_END_HELP = 1;
HelpInterface successor;
public FrontEnd(HelpInterface s)
{
successor = s;
}
public void getHelp(int helpConstant)
{
if(helpConstant != FRONT_END_HELP) {
successor.getHelp(helpConstant);
} else {
System.out.println("This is the front end.");
}
}
}
public class IntermediateLayer implements HelpInterface
{
final int INTERMEDIATE_LAYER_HELP = 2;
HelpInterface successor;
public FrontEnd(HelpInterface s)
{
successor = s;
}
public void getHelp(int helpConstant)
{
if(helpConstant != INTERMEDIATE_LAYER_HELP) {
successor.getHelp(helpConstant);
} else {
System.out.println("This is the IntermediateLayer.");
}
}
}
public class Application implements HelpInterface
{
public Application()
{
}
public void getHelp(int helpConstant)
{
System.out.println("This is the MegaGigaCo application.");
}
}
public class TestHelp
{
public static void main(String args[])
{
final int FRONT_END_HELP = 1;
final int INTERMEDIATE_LAYER_HELP = 2;
final int GENERAL_HELP = 3;
Application app = new Application();
IntermediateLayer intermediateLayer = new IntermediateLayer(app);
FrontEnd frontEnd = new FrontEnd(intermediateLayer);
frontEnd.getHelp(GENERAL_HELP);
}
}