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

来源:互联网 发布:黑暗之魂3白色头发数据 编辑:程序博客网 时间:2024/05/22 06:31

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.

 

原创粉丝点击