Java 设计模式-合成模式(Composite)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java 设计模式-合成模式(Composite)

Java 设计模式-合成模式(Composite)

 2015/1/24 0:32:48  zhangwei_david  程序员俱乐部  我要评论(0)
  • 摘要:合成模式(Composite)是一组对象的组合,这些对象可以是容器对象,表现为组的概念;另外一些对象则代表了单个对象,称为叶子对象。组对象可以包含叶子对象也可以包含其他组对象。组对象和叶子对象定义相同的行为。合成模式(Composite)的意图就是为了保证客户端调用单对象和调用组合对象的一致性,也就需要组合对象和单个对象需要共享同一个接口。在对组合对象建模时,为了防止死循环,可以确保组合对象都是树形结构。有一个对象模型需要校验,是用合成模式的校验的结果是
  • 标签:Java 模式 设计 设计模式

??? 合成模式(Composite)是一组对象的组合,这些对象可以是容器对象,表现为组的概念;另外一些对象则代表了单个对象,称为叶子对象。
???? 组对象可以包含叶子对象也可以包含其他组对象。组对象和叶子对象定义相同的行为。
?
?? 合成模式(Composite)的意图就是为了保证客户端调用单对象和调用组合对象的一致性,也就需要组合对象和单个对象需要共享同一个接口
?? 在对组合对象建模时,为了防止死循环,可以确保组合对象都是树形结构。

?????有一个对象模型需要校验,是用合成模式的校验的结果是:

?

?

?

Java代码
    class="dp-j">
  1. /**?
  2. ?*?
  3. ?*?@author?Lenovo?
  4. ?*?@version?$Id:?RemitModel.java,?v?0.1?2014年9月24日?下午5:03:53?Lenovo?Exp?$?
  5. ?*/??
  6. public?class?RemitModel?{??
  7. ??
  8. ????private?PayeeModel?payeeModel;??
  9. ??
  10. ????private?String?????remark;??
  11. ??
  12. ????private?String?????bankAccount;??
  13. ??
  14. ????/**?
  15. ?????*?Getter?method?for?property?<tt>payeeModel</tt>.?
  16. ?????*?
  17. ?????*?@return?property?value?of?payeeModel?
  18. ?????*/??
  19. ????public?PayeeModel?getPayeeModel()?{??
  20. ????????return?payeeModel;??
  21. ????}??
  22. ??
  23. ????/**?
  24. ?????*?Setter?method?for?property?<tt>payeeModel</tt>.?
  25. ?????*?
  26. ?????*?@param?payeeModel?value?to?be?assigned?to?property?payeeModel?
  27. ?????*/??
  28. ????public?void?setPayeeModel(PayeeModel?payeeModel)?{??
  29. ????????this.payeeModel?=?payeeModel;??
  30. ????}??
  31. ??
  32. ????/**?
  33. ?????*?Getter?method?for?property?<tt>remark</tt>.?
  34. ?????*?
  35. ?????*?@return?property?value?of?remark?
  36. ?????*/??
  37. ????public?String?getRemark()?{??
  38. ????????return?remark;??
  39. ????}??
  40. ??
  41. ????/**?
  42. ?????*?Setter?method?for?property?<tt>remark</tt>.?
  43. ?????*?
  44. ?????*?@param?remark?value?to?be?assigned?to?property?remark?
  45. ?????*/??
  46. ????public?void?setRemark(String?remark)?{??
  47. ????????this.remark?=?remark;??
  48. ????}??
  49. ??
  50. ????/**?
  51. ?????*?Getter?method?for?property?<tt>bankAccount</tt>.?
  52. ?????*?
  53. ?????*?@return?property?value?of?bankAccount?
  54. ?????*/??
  55. ????public?String?getBankAccount()?{??
  56. ????????return?bankAccount;??
  57. ????}??
  58. ??
  59. ????/**?
  60. ?????*?Setter?method?for?property?<tt>bankAccount</tt>.?
  61. ?????*?
  62. ?????*?@param?bankAccount?value?to?be?assigned?to?property?bankAccount?
  63. ?????*/??
  64. ????public?void?setBankAccount(String?bankAccount)?{??
  65. ????????this.bankAccount?=?bankAccount;??
  66. ????}??
  67. ??
  68. }??
  69. /**?
  70. ?*?
  71. ?*?@author?Lenovo?
  72. ?*?@version?$Id:?PayeeModel.java,?v?0.1?2014年9月24日?下午5:32:38?Lenovo?Exp?$?
  73. ?*/??
  74. public?class?PayeeModel?{??
  75. ??
  76. ????private?String?payeeBankAccount;??
  77. ??
  78. ????/**?
  79. ?????*?Getter?method?for?property?<tt>payeeBankAccount</tt>.?
  80. ?????*?
  81. ?????*?@return?property?value?of?payeeBankAccount?
  82. ?????*/??
  83. ????public?String?getPayeeBankAccount()?{??
  84. ????????return?payeeBankAccount;??
  85. ????}??
  86. ??
  87. ????/**?
  88. ?????*?Setter?method?for?property?<tt>payeeBankAccount</tt>.?
  89. ?????*?
  90. ?????*?@param?payeeBankAccount?value?to?be?assigned?to?property?payeeBankAccount?
  91. ?????*/??
  92. ????public?void?setPayeeBankAccount(String?payeeBankAccount)?{??
  93. ????????this.payeeBankAccount?=?payeeBankAccount;??
  94. ????}??
  95. ??
  96. }??
? Java代码
  1. /**?
  2. ?*?校验器?
  3. ?*?@author?Lenovo?
  4. ?*?@version?$Id:?Validator.java,?v?0.1?2014年9月24日?下午5:01:40?Lenovo?Exp?$?
  5. ?*/??
  6. public?interface?Validator?{??
  7. ????/**?
  8. ?????*?是否含有子节点?
  9. ?????*?
  10. ?????*?@return?
  11. ?????*/??
  12. ????public?boolean?hasChild();??
  13. ??
  14. ????/**?
  15. ?????*?校验数据?
  16. ?????*?
  17. ?????*?@param?remitModel?
  18. ?????*?@return?
  19. ?????*/??
  20. ????public?boolean?validate(RemitModel?remitModel);??
  21. ??
  22. ????/**?
  23. ?????*?添加子校验器?
  24. ?????*?
  25. ?????*?@param?validator?
  26. ?????*/??
  27. ????public?void?addValidator(Validator?validator);??
  28. ??
  29. }??
  30. import?java.util.ArrayList;??
  31. import?java.util.List;??
  32. ??
  33. /**?
  34. ?*?
  35. ?*?@author?Lenovo?
  36. ?*?@version?$Id:?RemitModelValidator.java,?v?0.1?2014年9月24日?下午5:05:25?Lenovo?Exp?$?
  37. ?*/??
  38. public?class?RemitModelValidator?implements?Validator?{??
  39. ??
  40. ????private?List<Validator>?children?=?new?ArrayList<Validator>();??
  41. ??
  42. ????/**?
  43. ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
  44. ?????*/??
  45. ????public?boolean?validate(RemitModel?remitModel)?{??
  46. ????????if?(hasChild())?{??
  47. ????????????for?(Validator?validator?:?children)?{??
  48. ????????????????if?(!validator.validate(remitModel))?{??
  49. ????????????????????System.out.println("校验失败");??
  50. ????????????????????return?false;??
  51. ????????????????}??
  52. ????????????????System.out.println("--");??
  53. ????????????}??
  54. ????????}??
  55. ????????System.out.println("校验通过");??
  56. ????????return?true;??
  57. ????}??
  58. ??
  59. ????/**?
  60. ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
  61. ?????*/??
  62. ????public?boolean?hasChild()?{??
  63. ????????return?!children.isEmpty();??
  64. ????}??
  65. ??
  66. ????/**?
  67. ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
  68. ?????*/??
  69. ????public?void?addValidator(Validator?validator)?{??
  70. ????????children.add(validator);??
  71. ????}??
  72. ??
  73. }??
  74. ??
  75. import?java.util.ArrayList;??
  76. import?java.util.List;??
  77. ??
  78. /**?
  79. ?*?
  80. ?*?@author?Lenovo?
  81. ?*?@version?$Id:?PayeeValidator.java,?v?0.1?2014年9月24日?下午5:20:06?Lenovo?Exp?$?
  82. ?*/??
  83. public?class?PayeeValidator?implements?Validator?{??
  84. ??
  85. ????private?List<Validator>?children?=?new?ArrayList<Validator>();??
  86. ??
  87. ????/**?
  88. ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
  89. ?????*/??
  90. ????public?boolean?hasChild()?{??
  91. ????????return?!children.isEmpty();??
  92. ????}??
  93. ??
  94. ????/**?
  95. ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
  96. ?????*/??
  97. ????public?boolean?validate(RemitModel?remitModel)?{??
  98. ????????if?(hasChild())?{??
  99. ????????????for?(Validator?validator?:?children)?{??
  100. ????????????????if?(!validator.validate(remitModel))?{??
  101. ????????????????????System.out.println("校验失败");??
  102. ????????????????????return?false;??
  103. ????????????????}??
  104. ????????????????System.out.println("**");??
  105. ????????????}??
  106. ????????}??
  107. ????????System.out.println("收款人校验通过");??
  108. ????????return?true;??
  109. ????}??
  110. ??
  111. ????/**?
  112. ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
  113. ?????*/??
  114. ????public?void?addValidator(Validator?validator)?{??
  115. ????????children.add(validator);??
  116. ????}??
  117. ??
  118. }??
  119. /**?
  120. ?*?
  121. ?*?@author?Lenovo?
  122. ?*?@version?$Id:?RemarkValidator.java,?v?0.1?2014年9月24日?下午5:13:48?Lenovo?Exp?$?
  123. ?*/??
  124. public?class?RemarkValidator?implements?Validator?{??
  125. ??
  126. ????/**?
  127. ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
  128. ?????*/??
  129. ????public?boolean?hasChild()?{??
  130. ????????return?false;??
  131. ????}??
  132. ??
  133. ????/**?
  134. ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
  135. ?????*/??
  136. ????public?boolean?validate(RemitModel?remitModel)?{??
  137. ????????System.out.println("校验汇款附言通过");??
  138. ????????return?true;??
  139. ????}??
  140. ??
  141. ????/**?
  142. ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
  143. ?????*/??
  144. ????public?void?addValidator(Validator?validator)?{??
  145. ????????throw?new?UnsupportedOperationException("子节点,无法增加");??
  146. ????}??
  147. ??
  148. }??
  149. /**?
  150. ?*?
  151. ?*?@author?Lenovo?
  152. ?*?@version?$Id:?BankAccountValidator.java,?v?0.1?2014年9月24日?下午5:17:28?Lenovo?Exp?$?
  153. ?*/??
  154. public?class?BankAccountValidator?implements?Validator?{??
  155. ??
  156. ????/**?
  157. ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
  158. ?????*/??
  159. ????public?boolean?hasChild()?{??
  160. ????????return?false;??
  161. ????}??
  162. ??
  163. ????/**?
  164. ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
  165. ?????*/??
  166. ????public?boolean?validate(RemitModel?remitModel)?{??
  167. ????????System.out.println("通过银行账户的校验");??
  168. ????????return?true;??
  169. ????}??
  170. ??
  171. ????/**?
  172. ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
  173. ?????*/??
  174. ????public?void?addValidator(Validator?validator)?{??
  175. ????????throw?new?UnsupportedOperationException("当前为叶子节点");??
  176. ????}??
  177. ??
  178. }??
  179. /**?
  180. ?*?
  181. ?*?@author?Lenovo?
  182. ?*?@version?$Id:?PayeeAddressValidator.java,?v?0.1?2014年9月24日?下午5:22:38?Lenovo?Exp?$?
  183. ?*/??
  184. public?class?PayeeAddressValidator?implements?Validator?{??
  185. ??
  186. ????/**?
  187. ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
  188. ?????*/??
  189. ????public?boolean?hasChild()?{??
  190. ????????return?false;??
  191. ????}??
  192. ??
  193. ????/**?
  194. ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
  195. ?????*/??
  196. ????public?boolean?validate(RemitModel?remitModel)?{??
  197. ????????System.out.println("收款人地址校验通过");??
  198. ????????return?true;??
  199. ????}??
  200. ??
  201. ????/**?
  202. ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
  203. ?????*/??
  204. ????public?void?addValidator(Validator?validator)?{??
  205. ????????throw?new?UnsupportedOperationException("当前为叶子节点");??
  206. ????}??
  207. ??
  208. }??
Java代码
  1. /**?
  2. ?*?
  3. ?*?@author?Lenovo?
  4. ?*?@version?$Id:?Client.java,?v?0.1?2014年9月24日?下午5:15:23?Lenovo?Exp?$?
  5. ?*/??
  6. public?class?Client?{??
  7. ??
  8. ????/**?
  9. ?????*?
  10. ?????*?@param?args?
  11. ?????*/??
  12. ????public?static?void?main(String[]?args)?{??
  13. ????????RemitModelValidator?remitModelValidator?=?new?RemitModelValidator();??
  14. ????????remitModelValidator.addValidator(new?RemarkValidator());??
  15. ????????remitModelValidator.addValidator(new?BankAccountValidator());??
  16. ????????PayeeValidator?payeeValidator?=?new?PayeeValidator();??
  17. ????????payeeValidator.addValidator(new?PayeeAddressValidator());??
  18. ????????remitModelValidator.addValidator(payeeValidator);??
  19. ????????remitModelValidator.validate(new?RemitModel());??
  20. ????}??
  21. ??
  22. }??
??????? 运行结果:

?

校验汇款附言通过
--
通过银行账户的校验
--
收款人地址校验通过
**
收款人校验通过
--
校验通过
发表评论
用户名: 匿名