谁应该实例化和分配运行时策略实现?

发布于 2024-08-15 17:46:15 字数 433 浏览 2 评论 0原文

实现策略模式时,如何确定哪个类负责 for:

  1. 选择要传递给 Context 类的具体具体策略实现(假设选择是基于一些复杂的业务逻辑而不是静态标志)

  2. 实例化上述具体实现并实际将其注入到 Context 类中

感觉应该有一些客观的指导来涵盖这一点。我已经阅读了各种 OOP 模式(即 GRASPSOLID),但我仍然对这个特定问题不太清楚。

When implementing the strategy pattern, how do you determine which class is responsible
for:

  1. Selecting the specific concrete strategy implementation to pass to the Context class (assuming that the selection is based on some complex business logic and not a static flag)

  2. Instantiating the aforementioned concrete implementation and actually injecting it into the Context class

It feels like there ought to be some objective guidance out there that covers this. I've done some reading on various OOP patterns (i.e. GRASP and SOLID) but I still don't have a lot of clarity around this particular question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是在用心讲痛 2024-08-22 17:46:15

这与我在 DI 容器中遇到的问题非常接近。看看这里,Mark Seemann 提供了一个很好的答案。

This is very close to the issues I was having with a DI container. Have a look here, Mark Seemann has provided a great answer.

空城仅有旧梦在 2024-08-22 17:46:15

如果您正在尝试解决特定情况,那么您正在寻找抽象工厂模式。工厂根据运行时信息确定如何实例化一系列类型。

如果您想知道如何抽象地解决这种情况,您正在寻找控制反转容器,管理依赖关系的组织和实现。策略的注册将包括条件逻辑。

If you are trying to address a particular situation, you are looking for the abstract factory pattern. A factory determines how to instantiate a family of types based on runtime information.

If you are wondering how to address this situation in the abstract, you are looking for an Inversion of Control container, which manages the organization and fulfillment of dependencies. The registration of the strategy would include the conditional logic.

关于从前 2024-08-22 17:46:15

这个决定是特定于上下文的,就像设计模式的其他所有内容一样。引用 GoF 书中的内容(斜体是我的):

什么是设计模式?

Christopher Alexander 说:“每种模式都描述了一个在我们的环境中反复出现的问题,然后描述了该问题解决方案的核心,这样您就可以使用该解决方案一百万次, 无需以同样的方式执行两次”[AIS+77,第 x 页]。尽管亚历山大谈论的是建筑物和城镇的模式,但他所说的面向对象的设计模式是正确的。我们的解决方案是用对象和界面而不是墙壁和门来表达的,但这两种模式的核心都是上下文中问题的解决方案

...

解决方案描述了构成设计的元素、它们的关系、职责和协作。该解决方案不描述特定的具体设计或实现,因为模式就像一个模板,可以应用于许多不同的情况。相反,该模式提供了设计问题的抽象描述以及元素(在我们的例子中为类和对象)的一般排列如何解决它。

That determination is context-specific, just like everything else with Desing Patterns. Quoting from the GoF book (italics mine):

What is a Design Pattern?

Christopher Alexander says, "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" [AIS+77, page x]. Even though Alexander was talking about patterns in buildings and towns, what he says is true about object-oriented design patterns. Our solutions are expressed in terms of objects and interfaces instead of walls and doors, but at the core of both kinds of patterns is a solution to a problem in a context.

...

The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations. Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文