使用创建模式进行延迟初始化 [英] Lazy Initialization with creational patterns

查看:57
本文介绍了使用创建模式进行延迟初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我正在重写一些类来实现延迟初始化,因为它们的创建需要几个查询,并且通常不需要完全初始化。

这是一个例子:



  class 产品
{
私人图片;
私人组件;

私人价格;
私人名称;
// 其他字段
}





假设并非总是需要images和components(通过组件管理器通过id检索的Components类的实例)。对于Product类来说,有用的是构建器的合理模式(因为构建Product需要不同类的合作以及不同类型的对象的创建),但是我如何在Product类中实现lazy init?

编写LoadImages和LoadComponents方法很简单,但此解决方案需要让Product类了解组件管理器或存储组件信息的表名以及如何创建组件对象。

我想出的唯一替代解决方案是在Product类中的product builder中添加一个(可能是静态的?)引用,让LoadImages和LoadComponents方法调用appriopriate函数在构建器类中。

但我想知道是否有更好的解决方案或更好的设计。



谢谢

解决方案

请参阅我对该问题的评论。显然,PHP缺乏严肃OOP语言的大部分表达能力。首先,没有使用setter / getters进行副作用实现的属性抽象。因此,一个显而易见的解决方案是使用显式函数而不是字段来表示您的属性。也就是说,非私有函数 getImages 可用于表示 images 。此函数将仅返回对 images 引用的图像容器的引用,但如果尚未创建容器,则创建它。以下是一些代码示例:

http:// stackoverflow。 com / questions / 19620868 / lazy-evaluation-container-for-dynamic-programming [ ^ ]。



我希望我能发明更优雅的东西,但PHP语言本身就是非常非常有限。当然,总有可能发明更复杂和更通用的东西,一些PropertyManager,但问题是:它值得努力吗?



-SA

Good Morning,
I'm rewriting some classes to implement a lazy initialization because their creation requires a couple of queries and often a full initialization is not required.
This is an example:

class Product
{
     private images;
     private components;

     private price;
     private name;
     //other fields
}



Suppose that "images" and "components" (instances of a Components class retrieved by id through a Components Manager) are not always required. For the Product class would be useful a builder crational pattern (because building a Product require the cooperation of different classes and the creation of different type of objects), but how I could implement lazy init inside the Product class?
Writing a "LoadImages" and "LoadComponents" methods is simple but this solution requires to let the Product class know about the Components Manager or about the tables names where the components infos are stored and how to create a Component object.
The only alternative solution I came up with is to add a (maybe static?) reference to the "product builder" in the in the Product class and let the "LoadImages" and "LoadComponents" methods call the appriopriate function in the builder class.
But I was wondering if there is a better solution or a better design.

Thank you

解决方案

Please see my comment to the question. Apparently, PHP lacks most of expressive power of "serious" OOP languages. First of all, there are no the property abstraction with side effect implementation using setters/getters. So, one obvious solution is to use explicit functions instead of fields to represent your properties. That is, a non-private function getImages can be used to represent images. This function will just return the reference to your image container referenced by images, but if the container is not yet created, create it. Here is some code sample:
http://stackoverflow.com/questions/19620868/lazy-evaluation-container-for-dynamic-programming[^].

I wish I could invent something more elegant, but PHP language itself is very, very limiting. Of course it's always possible to invent something way more complex and generic, some "PropertyManager", but then the question would be: does it worth the effort?

—SA


这篇关于使用创建模式进行延迟初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆