标签:
设计模式的意义在于:面向业务内容、业务数据结构和系统架构,高内聚低耦合、优雅的将平面逻辑立体化。
1 package designPattern; 2 /** 3 * 链接模式2,功能全面 4 * @author Administrator 5 */ 6 public class A9_ChainTest2 { 7 public static void main(String[] args) { 8 Manager aManager = new Manager(); 9 ProjectManager1 aPM = new ProjectManager1(); 10 Programmer1 aProgrammer1 = new Programmer1(); 11 QA aQA = new QA(); 12 Others others = new Others(); 13 14 aManager.addChain(aPM); 15 aPM.addChain(aProgrammer1); 16 aProgrammer1.addChain(aQA); 17 aQA.addChain(others); 18 19 aManager.sendToChain("Get"); 20 aManager.sendToChain("Design"); 21 aManager.sendToChain("Coding"); 22 aManager.sendToChain("Test"); 23 aManager.sendToChain("Kill La Deng !"); 24 } 25 } 26 interface Chain { 27 public abstract void addChain(Chain c); 28 public abstract void sendToChain(String mesg); 29 public abstract Chain getChain(); 30 } 31 class Manager implements Chain { 32 private Chain nextChain = null; 33 private String responsibility = "Get Project";; 34 35 public Manager() { } 36 37 public void addChain(Chain c) { 38 nextChain = c; 39 } 40 41 public Chain getChain() { 42 return nextChain; 43 } 44 45 public void sendToChain(String mesg) { 46 if(mesg.equals(responsibility)) { 47 System.out.println("A manager --> Get a Project"); 48 } else { 49 if(nextChain != null) { 50 nextChain.sendToChain(mesg); 51 } 52 } 53 } 54 } 55 class Others implements Chain { 56 private Chain nextChain = null; 57 58 public Others() { 59 } 60 public void addChain(Chain c) { 61 nextChain = c; 62 } 63 64 public Chain getChain() { 65 return nextChain; 66 } 67 68 public void sendToChain(String mesg) { 69 System.out.println("No one can handle --> " + mesg); 70 } 71 } 72 class Programmer1 implements Chain { 73 private Chain nextChain = null; 74 private String responsibility = "Coding"; 75 76 public Programmer1() { 77 } 78 public void addChain(Chain c) { 79 nextChain = c; 80 } 81 82 public Chain getChain() { 83 return nextChain; 84 } 85 86 public void sendToChain(String mesg) { 87 if(mesg.equals(responsibility)) { 88 System.out.println("A Programmer1 --> Coding"); 89 } else { 90 if(nextChain != null) { 91 nextChain.sendToChain(mesg); 92 } 93 } 94 } 95 } 96 class ProjectManager1 implements Chain { 97 private Chain nextChain = null; 98 private String responsibility = "Design"; 99 100 public ProjectManager1() { 101 } 102 public void addChain(Chain c) { 103 nextChain = c; 104 } 105 106 public Chain getChain() { 107 return nextChain; 108 } 109 110 public void sendToChain(String mesg) { 111 if(mesg.equals(responsibility)) { 112 System.out.println("A PM --> Design"); 113 } else { 114 if(nextChain != null) { 115 nextChain.sendToChain(mesg); 116 } 117 } 118 } 119 } 120 class QA implements Chain { 121 private Chain nextChain = null; 122 private String responsibility = "Test"; 123 124 public QA() { 125 } 126 public void addChain(Chain c) { 127 nextChain = c; 128 } 129 130 public Chain getChain() { 131 return nextChain; 132 } 133 134 public void sendToChain(String mesg) { 135 if(mesg.equals(responsibility)) { 136 System.out.println("A QA --> Test"); 137 } else { 138 if(nextChain != null) { 139 nextChain.sendToChain(mesg); 140 } 141 } 142 } 143 }
环境:JDK1.6,MAVEN,tomcat,eclipse
源码地址:http://files.cnblogs.com/files/xiluhua/designPattern.rar
欢迎亲们评论指教。
标签:
原文地址:http://www.cnblogs.com/xiluhua/p/4413739.html