Real world examples of Factory Method pattern

后端 未结 6 1964
醉话见心
醉话见心 2020-12-13 02:03

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 sc

相关标签:
6条回答
  • 2020-12-13 02:41

    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.

    0 讨论(0)
  • 2020-12-13 02:49

    Simple Factory Design Pattern doesn’t belong to the Gangs of Four. A Simple Factory Pattern is one of that returns an instance of one of several possible classes, depending on the data provided to it. This implies that the classes it returns have same parent class and methods, but each of them perform task differently for different kind of data. Here is the complete post with real world example

    0 讨论(0)
  • 2020-12-13 02:56

    An example php code with static creation:

    interface DbTable
    {
        public function doSomthing(): void;
    }
    
    class MySqlTable implements DbTable
    {
        public function doSomthing(): void
        {
        }
    }
    
    class OracleTable implements DbTable
    {
        public function doSomthing(): 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->doSomthing();
    

    To make it more dynamic (less modification later):

    interface DbTable
    {
        public function doSomthing(): void;
    }
    
    class MySqlTable implements DbTable
    {
        public function doSomthing(): void
        {
        }
    }
    
    class OracleTable implements DbTable
    {
        public function doSomthing(): 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->doSomthing();
    
    0 讨论(0)
  • 2020-12-13 02:57

    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.

    0 讨论(0)
  • 2020-12-13 02:58

    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.

    0 讨论(0)
  • 2020-12-13 03:04

    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.

    0 讨论(0)
提交回复
热议问题