我的工厂对象是否引入了全局状态?

发布于 2024-10-30 21:10:55 字数 526 浏览 3 评论 0原文

所以,这就是交易。我成功地在不使用全局变量或静态类/函数的情况下创建了一个框架。

我正在使用工厂的依赖注入形式。由于该框架将用于各种事物,因此我正在创建一个更通用的工厂来构建您的类,并递归地使用它的依赖项。

问题是,为了节省内存,每次实例化一个对象时,Factory 都会存储对其的引用,因此如果另一个对象依赖于该对象,Factory 只需要返回该引用。这样我们就不需要实例化同一个对象两次。

这意味着,在许多类中,我们将对同一对象有许多不同的引用。例如,如果我声明 Blog_model、Blog_controller、Blog_view、Form_validation 需要 Config 对象,那么它们中的每一个都将通过对同一 Config 对象的引用来实例化,尽管是通过注入进行的。

我不熟悉单元测试或任何类型的自动测试。我刚刚发现使用全局变量和静态变量是不好的(这就是我重写我使用的框架的原因)。我想问的是,这是否引入了全局状态?它会以任何方式妨碍测试吗?

---- 更新 ------

这是一个用 PHP 编写的 MVC 框架。

So, here's the deal. I've managed to make a framework without using globals or static classes/functions.

I am using a form of dependency injection using a Factory. Since the framework will be used for various things, I'm creating a more generic Factory that will build your class, with it's dependencies recursively.

The thing is, to conserve memory, each time an object gets instantiated, the Factory stores a reference to it, so if another object has a dependency to that object, the Factory will only need to return the reference. That way we don't need to instantiate the same object twice.

This means, in lots of classes, we will have many different references to the same object. For example, if I declare Blog_model, Blog_controller, Blog_view, Form_validation to require the Config object, each of them will be instantiated with a reference to the same Config object, albeit with injection.

I'm not familiar with unit testing or any kind of automatic testing. I've just discovered that using globals and statics are bad (which is why I'm rewriting the framework I use). What I want to ask is, does this introduce a global state? Does it hinders testing in any way?

---- Update ------

It's an MVC framework written in PHP.

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

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

发布评论

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

评论(2

西瓜 2024-11-06 21:10:55

据我读到这个问题,您基本上创建了一个仅支持单一生活方式的依赖注入容器

与 DI 密切相关的是生命周期管理的概念。如果我们多次请求特定类型的实例,我们每次都会得到相同的引用,还是每次都会得到一个新的实例?

如果我们每次调用它时都得到相同的实例单例生活方式 - 不要与单例设计模式混淆。

如果我们每次都会得到一个新实例,我们称之为瞬态生活方式

还有其他类型的生活方式,例如范围化、池化等,但以上两种是最基本的生活方式。

在我看来,您的 DI 容器仅支持 Singleton 生活方式。此与全局状态不同,但状态在容器的单个实例中共享。然而,如果你扔掉容器实例,你也会扔掉共享状态,所以它比全局状态更容易摆脱。

As far as I read this question, you have essentially created a Dependency Injection Container that only supports a single lifestyle.

Closely related to DI is the concept of lifetime management. If we ask for an instance of a specific type multiple times, do we get the same reference every time, or do we get a fresh instance each time?

If we get the same instance every time we call it the Singleton lifestyle - not to be confused with the Singleton design pattern.

If we get a new instance each time we call it the Transient lifestyle.

There are also other types of lifestyles, such as scoped, pooled, etc. but the above two are the most basic lifestyles.

It sounds to me that your DI container only supports the Singleton lifestyle. This isn't the same as Global state, but state is shared within a single instance of the container. However, if you throw away the container instance, your also throw away the shared state, so it's much easier to get rid of than global state.

梦一生花开无言 2024-11-06 21:10:55

是的,它确实引入了全局状态,因为您的工厂返回对刚刚创建的对象的引用。

你没有说你正在使用哪种语言,但如果你使用的是c++,你的工厂方法应该返回一个shared_ptr(shared_ptr的类型应该是你正在创建的对象的基类)。

Yes, it does introduce a global state, since your factory returns a reference to just created object.

You didn't say which language you are using, but if you are using c++, your factory method should return a shared_ptr (the type for the shared_ptr should be the base class of the object you are creating).

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