工厂/抽象工厂混乱

发布于 2024-10-12 05:36:50 字数 951 浏览 4 评论 0原文

经过大约 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.
}

我也对一些编写良好的课程将给我带来的灵活性感到兴奋。

不过,设计模式的想法让我感到困惑。它看起来像工厂模式,但我对 FAF 之间的区别感到困惑。我读过其他专门提出这个问题的问题,并且我读过 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 技术交流群。

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

发布评论

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

评论(2

似狗非友 2024-10-19 05:36:50

工厂抽象工厂之间的区别非常简单。在后者中,工厂本身是抽象的(!),不能直接实例化,而必须进行子类化。

例如,工厂:

class Document {
   public function createPage() {
       return new Page;
   }
}

class LandscapeDocument extends Document {
   public function createPage() {
       return new LandscapePage;
   }
}

在抽象工厂中:

abstract class Document {
   abstract public function createPage();
}

class PortraitDocument extends Document {
   public function createPage() {
      return new PortraitPage;
   }
}

class LandscapeDocument extends Document {
   public function createPage() {
      return new LandscapePage;
   }
}

简而言之,工厂模式在工厂类本身中有一个默认实现。抽象工厂要求所有子类实现自己版本的工厂方法。

这就是全部内容了。

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:

class Document {
   public function createPage() {
       return new Page;
   }
}

class LandscapeDocument extends Document {
   public function createPage() {
       return new LandscapePage;
   }
}

In Abstract Factory:

abstract class Document {
   abstract public function createPage();
}

class PortraitDocument extends Document {
   public function createPage() {
      return new PortraitPage;
   }
}

class LandscapeDocument extends Document {
   public function createPage() {
      return new LandscapePage;
   }
}

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.

人疚 2024-10-19 05:36:50

您可以用另一种方式来看待它:

清除灌木丛:
工厂模式是一种创建模式。即用于创建实例以供使用。

工厂模式

  • 一种创建模式,其中创建实例的逻辑掌握在工厂类手中。
  • 工厂模式仅创建一种类型的对象实例。在您的情况下,它将创建 LibraryObject 类型的对象,假设 LibraryObject 是层次结构树中最顶层的对象。

抽象模式(工厂的工厂)

  • 一种创建模式,其中创建实例的逻辑掌握在实现工厂接口/抽象类的类手中。
  • 抽象工厂模式可以创建对象不同类型的对象,因此您可以使用 Factory 接口/抽象类的具体实现来创建您想要的类型的对象。这就是为什么它被称为工厂的工厂。

下面的链接是一个很好的参考,我建议您也阅读工厂方法模式:
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.

Factory Pattern

  • A creational pattern where the logic for creating the instance lies in the hands of the Factory class.
  • A Factory Pattern creates only one type of object instance. In your case it would create objects of type LibraryObject assuming that the LibraryObject is top most object in the hierarchy tree.

Abstract Pattern (Factory of Factories)

  • A creational pattern where the logic for creating the instance lies in the hands of the classes that implement the Factory interface / abstract class.
  • An Abstract Factory pattern can create objects of different types, so you can use the concrete implementations of Factory interface / abstract class to create objects of the types that you desire. That is why it is called a Factory of Factories.

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/

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