工厂方法模式的现实世界示例

发布于 2024-08-24 04:01:07 字数 152 浏览 4 评论 0原文

我刚刚读了工厂方法。我知道它提供了一种将实例化委托给子类的方法。但我无法理解在现实场景中可能的用途。

谁能给出一个典型的例子来展示如何使用工厂方法模式,以便我能够与我所读到的内容联系起来。

对于哪个工厂方法模式是最佳解决方案的问题陈述就足以清楚地说明这一点。

I just read Factory Method. I understand that it provides a way to delegate the instantiation to sub-classes. But I couldn't understand the possible uses in a real-world scenario.

Can anyone give one typical example showing how Factory method pattern can be used so that I can relate to what I have read.

A problem statement for which factory method pattern is the best solution would be sufficient to make it clear.

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

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

发布评论

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

评论(5

几味少女 2024-08-31 04:01:07

实现工厂设计模式的类充当多个类之间的桥梁。考虑使用多个数据库服务器(例如 SQL Server 和 Oracle)的示例。如果您正在开发一个使用SQL Server数据库作为后端的应用程序,但将来需要将后端数据库更改为oracle,如果您没有按照工厂设计模式编写代码,则需要修改所有代码。

在工厂设计模式中,您只需做很少的工作即可实现这一点。实现工厂设计模式的类会照顾你并减轻你的负担。从数据库服务器切换根本不会打扰您。您只需要在配置文件中进行一些小的更改。

A class implementing factory design pattern works as bridge between multiple classes. Consider an example of using multiple database servers like SQL Server and Oracle. If you are developing an application using SQL Server database as backend, but in future need to change backend database to oracle, you will need to modify all your code, if you haven’t written your code following factory design pattern.

In factory design pattern you need to do very little work to achieve this. A class implementing factory design pattern takes care for you and lessen your burden. Switching from database server won’t bother you at all. You just need to make some small changes in your configuration file.

决绝 2024-08-31 04:01:07

静态创建的 php 代码示例:

interface DbTable
{
    public function doSomething(): void;
}

class MySqlTable implements DbTable
{
    public function doSomething(): void
    { }
}

class OracleTable implements DbTable
{
    public function doSomething(): void
    { }
}

class TableFactory
{
    public static function createTable(string $type = null): DbTable
    {
        if ($type === 'oracle') {
            return new OracleTable();
        }
        return new MySqlTable(); // default is mysql
    }
}

// client
$oracleTable = TableFactory::createTable('oracle');
$oracleTable->doSomething();

为了使其更加动态(稍后修改较少):

interface DbTable
{
    public function doSomething(): void;
}

class MySqlTable implements DbTable
{
    public function doSomething(): void
    { }
}

class OracleTable implements DbTable
{
    public function doSomething(): void
    { }
}

class TableFactory
{
    public static function createTable(string $tableName = null): DbTable
    {
        $className = __NAMESPACE__ . $tableName . 'Table';
        if (class_exists($className)) {
            $table = new $className();
            if ($table instanceof DbTable) {
               return $table;
            }
        }
        throw new \Exception("Class $className doesn't exists or it's not implementing DbTable interface");
    }
}

$tbl = TableFactory::createTable('Oracle');
$tbl->doSomething();

An example php code with static creation:

interface DbTable
{
    public function doSomething(): void;
}

class MySqlTable implements DbTable
{
    public function doSomething(): void
    { }
}

class OracleTable implements DbTable
{
    public function doSomething(): void
    { }
}

class TableFactory
{
    public static function createTable(string $type = null): DbTable
    {
        if ($type === 'oracle') {
            return new OracleTable();
        }
        return new MySqlTable(); // default is mysql
    }
}

// client
$oracleTable = TableFactory::createTable('oracle');
$oracleTable->doSomething();

To make it more dynamic (less modification later):

interface DbTable
{
    public function doSomething(): void;
}

class MySqlTable implements DbTable
{
    public function doSomething(): void
    { }
}

class OracleTable implements DbTable
{
    public function doSomething(): void
    { }
}

class TableFactory
{
    public static function createTable(string $tableName = null): DbTable
    {
        $className = __NAMESPACE__ . $tableName . 'Table';
        if (class_exists($className)) {
            $table = new $className();
            if ($table instanceof DbTable) {
               return $table;
            }
        }
        throw new \Exception("Class $className doesn't exists or it's not implementing DbTable interface");
    }
}

$tbl = TableFactory::createTable('Oracle');
$tbl->doSomething();
青瓷清茶倾城歌 2024-08-31 04:01:07

从我现在正在开发的 API:

WebGalleryFactory factory = WebGalleryFactory.newInstance (WebGalleryType.PICASA);
WebAlbum album = factory.createAlbum (title, description);

在这个示例中,我使用工厂方法来创建某种类型的抽象工厂(示例中为 PICASA)。

这两种模式经常一起使用。

From API I'm developing right now:

WebGalleryFactory factory = WebGalleryFactory.newInstance (WebGalleryType.PICASA);
WebAlbum album = factory.createAlbum (title, description);

In this example I use Factory Method to create Abstract Factory of a certain type (PICASA in the example).

These two patterns are often used together.

゛时过境迁 2024-08-31 04:01:07

Zend_Db 在它的 Zend_Db_Adapter 类中使用它来允许创建基于从配置对象传递的数据库设置的不同类型的数据库对象。

Zend_Db uses it in it's Zend_Db_Adapter class to allow the creation of different kinds of database objects based on database settings passed through from a configuration object.

只涨不跌 2024-08-31 04:01:07

.NET 基类库 (BCL) 中的一个示例是Control.CreateControlsInstance,它由(Windows 窗体)Control 类的许多其他成员使用。

您可以重写此受保护的方法来提供您自己的控件集合,例如,当您实现自定义控件时。

One example from the .NET Base Class Library (BCL) is Control.CreateControlsInstance, which is is used by many other members of the (Windows Forms) Control class.

You can override this protected method to provide your own collection of controls, e.g. when you are implementing a custom control.

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