用通俗的话理解设计模式-Command pattern

Given this situation, the way to create a new and much powerful functionality maybe impossible to avoid the two following steps:

 first, to utilize the existing code or method in other classes

 second, in order to use other classess' method, you have to create loads of corresponding class instance.

 

however, if the utlized class has modified some code, what you have to do is to modify your code to make the utlized class compatible and make the maintence. This issue is typically named as your class is coupled with the utlized classes.

what'more, if the number of the classes you have reused is a large number.  It would be complex to invoke your class.

 

The Command pattern can also trackle the above situation and get around the potential troubles.

Instead of instantiating a lot of classes, programming to the interface can make your class invokation be clear and clean.  Your class can just invoke an execute() method. The implementation could be carried out by diverse varity of classes that has been done by others.

 

after making an interface for your class, the remaining job is to implement the execute() method.

the intent of the command pattern is encapusulating a request as an object. So it's inavoidable to create the utlized classes' instances since you have to invoke the method in these classes.  maybe the method implementation is always a dirty and hard work that must be done.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值