设计模式
我希望对工作中现有的组件进行一些更改,并开始看到使用设计模式的优势。问题是我有一些具体的例子,我试图使用我在设计模式中学到的东西,但在这样做时遇到了问题。这就是我正在尝试做的事情。
我有一个现有的组件,可以监视 n 个不同软件包的性能,每个软件包都适合特定类型。即现有组件
- 包 A 用于类型 1
- 包 B 用于类型 2
- 包 C 用于类型 3 等等...
每个包都会进行一些性能监控,并有自己的数据日志、记录等。每个包现在只有一种类型。
将来我们可能会推出
- 包含分析日志的新功能。这将具有已经存在的数据日志,并在其之上添加一些其他数据。
- 支持一个包的多种类型。即包 A 用于类型 1,类型 2 (Bitype) 包 B 用于类型 1,类型 2,类型 3 (tritype)
看着这个,我认为适配器模式可以在这里使用。我可以使用现有组件作为适配者。然后在适配器类中执行新功能。我的目标是添加两个功能的最终组件。
由于我在这里处理现有代码,我认为我应该添加一个适配器类来适应新功能。但我重新考虑了,因为适配器是为了使一个接口可与另一个接口一起使用。这里我有现有的接口,但我还没有目标接口。我还必须创建适配器和目标。所以我很困惑这个定义是否符合我的要求。
有设计模式经验的人可以在这里提出您的建议吗?谢谢
该软件是为运动员设计的。现有系统跟踪这些运动员的训练细节。它包含训练日志、他们喜欢的品牌和个人详细信息等详细信息。还有一个针对某类运动员的单独包。运动员类型有网球运动员、高尔夫球运动员、游泳运动员等。因此,运动员只能属于一种类型,要么是网球运动员,要么是高尔夫球运动员,不能同时是两者。将来将需要包含更多功能
- 来包含性能日志(即采用现有日志并在其上添加性能日志详细信息)。
- 运动员的兴趣可能有重叠,即一名运动员可以参加不止一项运动。一名运动员可以参加网球、金牌和游泳比赛。
希望现在已经清楚了。
I am looking to do some changes to my existing component at work and am starting to see the advantages of using design patterns. The problem is I have some specific examples for which I am trying to use what I learned in design patterns and am having issues doing that. Here is what I am trying to do.
I have a existing component that monitors the performance of say n different software packages each catering to a specific type. i.e Existing component
- package A for type 1
- package B for type 2
- package C for type 3 and so on....
Each package does some performance monitorings and has its own data logs, records, etc.. Each package has only one type now.
In the future we may have a new feature coming up for
- including a analysis log. This will have the data log that is already existing and add some other data on top of that.
- Supporting multiple types for a package. i.e package A for type1, type 2 (Bitype) package B for type 1,type 2, type 3 (tritype)
Looking at this, I was thinking adapter pattern could be of use here. I could use the existing component as the adaptee . Then do the new functionality in the adapter class. My target will be the final component with the 2 functionalities added.
Since, I am dealing with existing code here, I think I should add a adapter class to adapt the new features. But I am having second thoughts because, adapter is for making one interface usable with another interface. Here I have existing interface, but I do not have a target interface already present. I will have to create both the adapter and the target too. So am confused if this definition fits my requirement or not.
Could someone who has some experience with design patterns make your suggestions here? Thanks
The software is for sportsmen. The existing system tracks the training details of these sportsmen. It has details like training log, what brands they prefer and personal details etc. There is a separate package created for a type of sportsmen. sportsmen types are tennis player, golf player, swimmer etc.So a sportsmen can be in one type only , either tennis player or golf player and not both. In the future there will be a requirement to include more functionality
- To include a performance log (i.e take the already existing logs and add performance log details on top of it).
- There can be overlapping sportsmen interests i.e a single sportsmen can be participating in more than one sport. A single sportsmen could be participating in tennis, gold and swimming.
Hope it is clear now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,您无法实现此处建议的模式。您必须至少熟悉最常见的那些,并了解何时使用它们。您甚至可能为某些特定情况创建自己的模式。
您的描述对我来说不是很清楚,但我可以建议您研究 Bridge 模式的方向并尝试制作类似的内容:
我会向您推荐一本关于模式的很棒的免费在线书籍:http://gameprogrammingpatterns.com/
祝你好运!
Unfortunately, you can't implement patterns that would be suggested here. You must be familiar at least with the most common ones and understand when to use each of them. It can be happened that you even create your own patterns for some particular cases.
Your description is not very clear for me, but I can suggest you to look into direction of Bridge pattern and try to make something like:
I would recommend you a great free online book about patterns: http://gameprogrammingpatterns.com/
Good luck!
这看起来是装饰器模式的一个很好的例子。
您可以拥有一个抽象数据记录器,从中您可以派生出初始包 A、B、C 等。
此外,您还需要创建一个“装饰器”,它是一个从基本数据记录器派生的抽象类,并且还具有一个基本数据记录器作为其成员。
您可以在此成员数据记录器提供的功能之上进行构建,并将其公开为该装饰器的功能。 (例如 AnalysisDecorator 的 LogData 方法除了记录数据之外还会记录一些分析信息)
您可以为要添加的每一项功能创建一个装饰器。
现在,如果您想向包 A 添加一些附加功能,您可以使用所需功能的装饰器来包装它。
通过这种方式,您可以将添加的功能任意组合到您的基础包中。
希望这有帮助!
This looks like a good case for the Decorator pattern.
You can have an abstract data logger from which you would derive your initial package A, B, C etc.
In addition, you'd create a 'decorator' which is an abstract class deriving from your base data logger, and also having a base data logger as its member.
You can build on top of the functionality provided by this member datalogger and expose it as the functionality of this decorator. (e.g. the LogData method of the AnalysisDecorator would log some analysis information in addition to the data that it logs)
You can create one decorator for every functionality that you want to add.
Now if you want to add some additional functionality to package A, you can wrap it with the decorator of the desired functionality.
This way you can have any combinations of added functionality to your base package.
Hope this helps!