Java 设计模式-合成模式(Composite)
??? 合成模式(Composite)是一组对象的组合,这些对象可以是容器对象,表现为组的概念;另外一些对象则代表了单个对象,称为叶子对象。
???? 组对象可以包含叶子对象也可以包含其他组对象。组对象和叶子对象定义相同的行为。
?
?? 合成模式(Composite)的意图就是为了保证客户端调用单对象和调用组合对象的一致性,也就需要组合对象和单个对象需要共享同一个接口。
?? 在对组合对象建模时,为了防止死循环,可以确保组合对象都是树形结构。
?
?
?
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?RemitModel.java,?v?0.1?2014年9月24日?下午5:03:53?Lenovo?Exp?$?
- ?*/??
- public?class?RemitModel?{??
- ??
- ????private?PayeeModel?payeeModel;??
- ??
- ????private?String?????remark;??
- ??
- ????private?String?????bankAccount;??
- ??
- ????/**?
- ?????*?Getter?method?for?property?<tt>payeeModel</tt>.?
- ?????*?
- ?????*?@return?property?value?of?payeeModel?
- ?????*/??
- ????public?PayeeModel?getPayeeModel()?{??
- ????????return?payeeModel;??
- ????}??
- ??
- ????/**?
- ?????*?Setter?method?for?property?<tt>payeeModel</tt>.?
- ?????*?
- ?????*?@param?payeeModel?value?to?be?assigned?to?property?payeeModel?
- ?????*/??
- ????public?void?setPayeeModel(PayeeModel?payeeModel)?{??
- ????????this.payeeModel?=?payeeModel;??
- ????}??
- ??
- ????/**?
- ?????*?Getter?method?for?property?<tt>remark</tt>.?
- ?????*?
- ?????*?@return?property?value?of?remark?
- ?????*/??
- ????public?String?getRemark()?{??
- ????????return?remark;??
- ????}??
- ??
- ????/**?
- ?????*?Setter?method?for?property?<tt>remark</tt>.?
- ?????*?
- ?????*?@param?remark?value?to?be?assigned?to?property?remark?
- ?????*/??
- ????public?void?setRemark(String?remark)?{??
- ????????this.remark?=?remark;??
- ????}??
- ??
- ????/**?
- ?????*?Getter?method?for?property?<tt>bankAccount</tt>.?
- ?????*?
- ?????*?@return?property?value?of?bankAccount?
- ?????*/??
- ????public?String?getBankAccount()?{??
- ????????return?bankAccount;??
- ????}??
- ??
- ????/**?
- ?????*?Setter?method?for?property?<tt>bankAccount</tt>.?
- ?????*?
- ?????*?@param?bankAccount?value?to?be?assigned?to?property?bankAccount?
- ?????*/??
- ????public?void?setBankAccount(String?bankAccount)?{??
- ????????this.bankAccount?=?bankAccount;??
- ????}??
- ??
- }??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?PayeeModel.java,?v?0.1?2014年9月24日?下午5:32:38?Lenovo?Exp?$?
- ?*/??
- public?class?PayeeModel?{??
- ??
- ????private?String?payeeBankAccount;??
- ??
- ????/**?
- ?????*?Getter?method?for?property?<tt>payeeBankAccount</tt>.?
- ?????*?
- ?????*?@return?property?value?of?payeeBankAccount?
- ?????*/??
- ????public?String?getPayeeBankAccount()?{??
- ????????return?payeeBankAccount;??
- ????}??
- ??
- ????/**?
- ?????*?Setter?method?for?property?<tt>payeeBankAccount</tt>.?
- ?????*?
- ?????*?@param?payeeBankAccount?value?to?be?assigned?to?property?payeeBankAccount?
- ?????*/??
- ????public?void?setPayeeBankAccount(String?payeeBankAccount)?{??
- ????????this.payeeBankAccount?=?payeeBankAccount;??
- ????}??
- ??
- }??
- /**?
- ?*?校验器?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?Validator.java,?v?0.1?2014年9月24日?下午5:01:40?Lenovo?Exp?$?
- ?*/??
- public?interface?Validator?{??
- ????/**?
- ?????*?是否含有子节点?
- ?????*?
- ?????*?@return?
- ?????*/??
- ????public?boolean?hasChild();??
- ??
- ????/**?
- ?????*?校验数据?
- ?????*?
- ?????*?@param?remitModel?
- ?????*?@return?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel);??
- ??
- ????/**?
- ?????*?添加子校验器?
- ?????*?
- ?????*?@param?validator?
- ?????*/??
- ????public?void?addValidator(Validator?validator);??
- ??
- }??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?RemitModelValidator.java,?v?0.1?2014年9月24日?下午5:05:25?Lenovo?Exp?$?
- ?*/??
- public?class?RemitModelValidator?implements?Validator?{??
- ??
- ????private?List<Validator>?children?=?new?ArrayList<Validator>();??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel)?{??
- ????????if?(hasChild())?{??
- ????????????for?(Validator?validator?:?children)?{??
- ????????????????if?(!validator.validate(remitModel))?{??
- ????????????????????System.out.println("校验失败");??
- ????????????????????return?false;??
- ????????????????}??
- ????????????????System.out.println("--");??
- ????????????}??
- ????????}??
- ????????System.out.println("校验通过");??
- ????????return?true;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
- ?????*/??
- ????public?boolean?hasChild()?{??
- ????????return?!children.isEmpty();??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
- ?????*/??
- ????public?void?addValidator(Validator?validator)?{??
- ????????children.add(validator);??
- ????}??
- ??
- }??
- ??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?PayeeValidator.java,?v?0.1?2014年9月24日?下午5:20:06?Lenovo?Exp?$?
- ?*/??
- public?class?PayeeValidator?implements?Validator?{??
- ??
- ????private?List<Validator>?children?=?new?ArrayList<Validator>();??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
- ?????*/??
- ????public?boolean?hasChild()?{??
- ????????return?!children.isEmpty();??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel)?{??
- ????????if?(hasChild())?{??
- ????????????for?(Validator?validator?:?children)?{??
- ????????????????if?(!validator.validate(remitModel))?{??
- ????????????????????System.out.println("校验失败");??
- ????????????????????return?false;??
- ????????????????}??
- ????????????????System.out.println("**");??
- ????????????}??
- ????????}??
- ????????System.out.println("收款人校验通过");??
- ????????return?true;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
- ?????*/??
- ????public?void?addValidator(Validator?validator)?{??
- ????????children.add(validator);??
- ????}??
- ??
- }??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?RemarkValidator.java,?v?0.1?2014年9月24日?下午5:13:48?Lenovo?Exp?$?
- ?*/??
- public?class?RemarkValidator?implements?Validator?{??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
- ?????*/??
- ????public?boolean?hasChild()?{??
- ????????return?false;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel)?{??
- ????????System.out.println("校验汇款附言通过");??
- ????????return?true;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
- ?????*/??
- ????public?void?addValidator(Validator?validator)?{??
- ????????throw?new?UnsupportedOperationException("子节点,无法增加");??
- ????}??
- ??
- }??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?BankAccountValidator.java,?v?0.1?2014年9月24日?下午5:17:28?Lenovo?Exp?$?
- ?*/??
- public?class?BankAccountValidator?implements?Validator?{??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
- ?????*/??
- ????public?boolean?hasChild()?{??
- ????????return?false;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel)?{??
- ????????System.out.println("通过银行账户的校验");??
- ????????return?true;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
- ?????*/??
- ????public?void?addValidator(Validator?validator)?{??
- ????????throw?new?UnsupportedOperationException("当前为叶子节点");??
- ????}??
- ??
- }??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?PayeeAddressValidator.java,?v?0.1?2014年9月24日?下午5:22:38?Lenovo?Exp?$?
- ?*/??
- public?class?PayeeAddressValidator?implements?Validator?{??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#hasChild()?
- ?????*/??
- ????public?boolean?hasChild()?{??
- ????????return?false;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#validate(com.cathy.demo.pattern.composite.RemitModel)?
- ?????*/??
- ????public?boolean?validate(RemitModel?remitModel)?{??
- ????????System.out.println("收款人地址校验通过");??
- ????????return?true;??
- ????}??
- ??
- ????/**?
- ?????*?@see?com.cathy.demo.pattern.composite.Validator#addValidator(com.cathy.demo.pattern.composite.Validator)?
- ?????*/??
- ????public?void?addValidator(Validator?validator)?{??
- ????????throw?new?UnsupportedOperationException("当前为叶子节点");??
- ????}??
- ??
- }??
- /**?
- ?*?
- ?*?@author?Lenovo?
- ?*?@version?$Id:?Client.java,?v?0.1?2014年9月24日?下午5:15:23?Lenovo?Exp?$?
- ?*/??
- public?class?Client?{??
- ??
- ????/**?
- ?????*?
- ?????*?@param?args?
- ?????*/??
- ????public?static?void?main(String[]?args)?{??
- ????????RemitModelValidator?remitModelValidator?=?new?RemitModelValidator();??
- ????????remitModelValidator.addValidator(new?RemarkValidator());??
- ????????remitModelValidator.addValidator(new?BankAccountValidator());??
- ????????PayeeValidator?payeeValidator?=?new?PayeeValidator();??
- ????????payeeValidator.addValidator(new?PayeeAddressValidator());??
- ????????remitModelValidator.addValidator(payeeValidator);??
- ????????remitModelValidator.validate(new?RemitModel());??
- ????}??
- ??
- }??
?
--
通过银行账户的校验
--
收款人地址校验通过
**
收款人校验通过
--
校验通过
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。