{"title":"使用命令模式时的常见错误及如何避免","authors":"A. Zarras","doi":"10.1145/3424771.3424773","DOIUrl":null,"url":null,"abstract":"Command, is a behavioral pattern from the Gang of Four catalog that allows us to structure an application with respect to primitive actions that can be easily managed and executed. The main idea is to decouple the objects that invoke actions from the objects that know how to perform them, by encapsulating everything that is needed for executing the actions in corresponding command objects. The application can comprise different classes of command objects that realize different actions. The different classes of command objects implement the same interface. Therefore, command objects can be passed as parameters to other objects that use them to execute the respective actions, without knowing how this is actually done. This paper revisits the Command pattern, focusing on the configuration of command objects, when the data that are needed for the execution of the actions become available as soon as the actions should be executed. In this case, it is not clear which class is responsible for configuring the command objects and how this should be done. The paper reports common mistakes when dealing with this problem, as anti-patterns, observed during the project of a software engineering course. The observed mistakes invalidate the benefits of the Command pattern because the invoking objects are explicitly or implicitly coupled with the concrete classes of command objects that realize the different actions. The paper further introduces a pattern that deals with the problem.","PeriodicalId":216842,"journal":{"name":"Proceedings of the European Conference on Pattern Languages of Programs 2020","volume":"35 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2020-07-01","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"3","resultStr":"{\"title\":\"Common Mistakes When Using the Command Pattern and How to Avoid Them\",\"authors\":\"A. Zarras\",\"doi\":\"10.1145/3424771.3424773\",\"DOIUrl\":null,\"url\":null,\"abstract\":\"Command, is a behavioral pattern from the Gang of Four catalog that allows us to structure an application with respect to primitive actions that can be easily managed and executed. The main idea is to decouple the objects that invoke actions from the objects that know how to perform them, by encapsulating everything that is needed for executing the actions in corresponding command objects. The application can comprise different classes of command objects that realize different actions. The different classes of command objects implement the same interface. Therefore, command objects can be passed as parameters to other objects that use them to execute the respective actions, without knowing how this is actually done. This paper revisits the Command pattern, focusing on the configuration of command objects, when the data that are needed for the execution of the actions become available as soon as the actions should be executed. In this case, it is not clear which class is responsible for configuring the command objects and how this should be done. The paper reports common mistakes when dealing with this problem, as anti-patterns, observed during the project of a software engineering course. The observed mistakes invalidate the benefits of the Command pattern because the invoking objects are explicitly or implicitly coupled with the concrete classes of command objects that realize the different actions. The paper further introduces a pattern that deals with the problem.\",\"PeriodicalId\":216842,\"journal\":{\"name\":\"Proceedings of the European Conference on Pattern Languages of Programs 2020\",\"volume\":\"35 1\",\"pages\":\"0\"},\"PeriodicalIF\":0.0000,\"publicationDate\":\"2020-07-01\",\"publicationTypes\":\"Journal Article\",\"fieldsOfStudy\":null,\"isOpenAccess\":false,\"openAccessPdf\":\"\",\"citationCount\":\"3\",\"resultStr\":null,\"platform\":\"Semanticscholar\",\"paperid\":null,\"PeriodicalName\":\"Proceedings of the European Conference on Pattern Languages of Programs 2020\",\"FirstCategoryId\":\"1085\",\"ListUrlMain\":\"https://doi.org/10.1145/3424771.3424773\",\"RegionNum\":0,\"RegionCategory\":null,\"ArticlePicture\":[],\"TitleCN\":null,\"AbstractTextCN\":null,\"PMCID\":null,\"EPubDate\":\"\",\"PubModel\":\"\",\"JCR\":\"\",\"JCRName\":\"\",\"Score\":null,\"Total\":0}","platform":"Semanticscholar","paperid":null,"PeriodicalName":"Proceedings of the European Conference on Pattern Languages of Programs 2020","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/3424771.3424773","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 3
摘要
Command是一种来自Gang of Four目录的行为模式,它允许我们根据易于管理和执行的基本操作来构建应用程序。其主要思想是通过将执行动作所需的一切封装在相应的命令对象中,将调用动作的对象与知道如何执行动作的对象解耦。应用程序可以包含实现不同操作的不同类的命令对象。命令对象的不同类实现相同的接口。因此,可以将命令对象作为参数传递给使用它们执行相应操作的其他对象,而不知道实际是如何完成的。本文重新审视了Command模式,重点关注命令对象的配置,当执行操作所需的数据在应该执行操作时变得可用时。在这种情况下,不清楚哪个类负责配置命令对象,以及应该如何配置。本文报告了在处理这个问题时常见的错误,如在软件工程课程的项目中观察到的反模式。观察到的错误使Command模式的优点失效,因为调用对象显式或隐式地与实现不同操作的命令对象的具体类耦合在一起。本文进一步介绍了一种处理该问题的模式。
Common Mistakes When Using the Command Pattern and How to Avoid Them
Command, is a behavioral pattern from the Gang of Four catalog that allows us to structure an application with respect to primitive actions that can be easily managed and executed. The main idea is to decouple the objects that invoke actions from the objects that know how to perform them, by encapsulating everything that is needed for executing the actions in corresponding command objects. The application can comprise different classes of command objects that realize different actions. The different classes of command objects implement the same interface. Therefore, command objects can be passed as parameters to other objects that use them to execute the respective actions, without knowing how this is actually done. This paper revisits the Command pattern, focusing on the configuration of command objects, when the data that are needed for the execution of the actions become available as soon as the actions should be executed. In this case, it is not clear which class is responsible for configuring the command objects and how this should be done. The paper reports common mistakes when dealing with this problem, as anti-patterns, observed during the project of a software engineering course. The observed mistakes invalidate the benefits of the Command pattern because the invoking objects are explicitly or implicitly coupled with the concrete classes of command objects that realize the different actions. The paper further introduces a pattern that deals with the problem.