工厂/抽象工厂混乱
经过大约 10 个月的过程性 PHP 学习后,我现在正在尝试了解基本的 OOP 原则和设计模式。这是一种爱好,我没有那么多时间去追求它,所以请原谅这个问题的水平相当低。
我的网站(目前 100% 程序化)本质上是一个图书馆。访问者向库脚本发送 2 个数据点 - 项目类型
和项目代码
。
Library.php
使用项目类型来选择包含,包含抓取代码以访问数据库,然后构建页面。
一些示例:
[type] [code]
game RoTo
map 32
unit 216
示例链接为 library.php?type=game&code=RoTo
一切都按原样运行良好,但当我开始使用 OOP 时,我看到了“明显的简单入口点和继承路径”客观化”这个系统。
class LibraryObject
{
protected $_name;
protected $_description;
}
class Game extends LibraryObject
{
protected $_releaseDate;
etc.
}
我也对一些编写良好的课程将给我带来的灵活性感到兴奋。
不过,设计模式的想法让我感到困惑。它看起来像工厂模式,但我对 F 和 AF 之间的区别感到困惑。我读过其他专门提出这个问题的问题,并且我读过 OODesign 上的示例,但我觉得它们是用不同的语言编写的,这相当令人沮丧。
也许如果有人可以使用我自己的数据结构来解释它,这对我来说会更有意义?
抱歉给您带来麻烦了。
After ~10 months of procedural PHP, I'm now trying to wrap my head around basic OOP principles and design patterns. This is a hobby, and I haven't nearly as much time as I'd like to pursue it, so please forgive the rather low level of this question.
My site (currently 100% procedural) is at heart a library. Visitors send the Library script 2 datapoints - an item type
and item code
.
Library.php
uses the item type to select an include, and the include grabs the code to hit the database and then build the page.
Some examples:
[type] [code]
game RoTo
map 32
unit 216
An example link would be library.php?type=game&code=RoTo
Everything works nicely as is, but as I get started with OOP I see obvious easy entry points and inheritance paths for "objectifying" this system.
class LibraryObject
{
protected $_name;
protected $_description;
}
class Game extends LibraryObject
{
protected $_releaseDate;
etc.
}
I'm also excited about the flexibility some well-written classes will give me.
The design pattern idea is tripping me up, though. It seems like a Factory pattern, but I'm confused about the differences between F and AF. I've read other SO questions specifically asking that question, and I've read the examples on OODesign but I feel like they're written in a different language and it's rather frustrating.
Perhaps if someone could explain it using my own data structures it would make more sense to me?
Sorry for the trouble.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工厂和抽象工厂之间的区别非常简单。在后者中,工厂本身是抽象的(!),不能直接实例化,而必须进行子类化。
例如,工厂:
在抽象工厂中:
简而言之,工厂模式在工厂类本身中有一个默认实现。抽象工厂要求所有子类实现自己版本的工厂方法。
这就是全部内容了。
The difference between Factory and Abstract Factory is pretty simple. In the latter, the factory itself is abstract (!) and cannot be instantiated directly, but must be sub-classed.
Per example, Factory:
In Abstract Factory:
In short, the Factory pattern has a default implementation in the factory class itself. The Abstract Factory requires all sub-classes to implement their own version of the factory methods.
That's all there is to it.
您可以用另一种方式来看待它:
清除灌木丛:
工厂模式是一种创建模式。即用于创建实例以供使用。
下面的链接是一个很好的参考,我建议您也阅读工厂方法模式:
http://www.oodesign.com/creational-patterns/
Here is another way you can look at it:
To clear the bushes:
A factory pattern is a creational pattern. That is it is used to create instances for use.
A good reference would be this link below, I would suggest you read the Factory Method pattern as well:
http://www.oodesign.com/creational-patterns/