TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:software-architecture:design-patterns:factory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:software-architecture:design-patterns:factory [2024/08/16 03:48] – [Simple Factory Pattern] tungntdevelopment:software-architecture:design-patterns:factory [2024/08/19 09:29] (current) tungnt
Line 128: Line 128:
  
 {{ :development:software-architecture:design-patterns:factory-method-en.png |}} {{ :development:software-architecture:design-patterns:factory-method-en.png |}}
 +
 +**Tóm lại:** Method Factory dùng khi có nhiều cách thức khác nhau để đạt được 1 mục tiêu, và để khi thêm một cách thức mới thì chúng ta chỉ cần thêm một Class mới tương ứng không phải sửa quá nhiều code trong function xử lý.
 +
 +**Ví dụ 1:**
 +
 +{{ :development:software-architecture:design-patterns:method-factory-diagram.png |}}
  
 <file php> <file php>
Line 243: Line 249:
 echo "App: Launched with the ConcreteCreator2.\n"; echo "App: Launched with the ConcreteCreator2.\n";
 clientCode(new ConcreteCreator2()); clientCode(new ConcreteCreator2());
 +</file>
 +
 +**Ví dụ 2:**
 +
 +{{ :development:software-architecture:design-patterns:method-factory-social.png |}}
 +
 +<file php>
 +<?php
 +
 +namespace RefactoringGuru\FactoryMethod\RealWorld;
 +
 +/**
 + * The Creator declares a factory method that can be used as a substitution for
 + * the direct constructor calls of products, for instance:
 + *
 + * - Before: $p = new FacebookConnector();
 + * - After: $p = $this->getSocialNetwork;
 + *
 + * This allows changing the type of the product being created by
 + * SocialNetworkPoster's subclasses.
 + */
 +abstract class SocialNetworkPoster
 +{
 +    /**
 +     * The actual factory method. Note that it returns the abstract connector.
 +     * This lets subclasses return any concrete connectors without breaking the
 +     * superclass' contract.
 +     */
 +    abstract public function getSocialNetwork(): SocialNetworkConnector;
 +
 +    /**
 +     * When the factory method is used inside the Creator's business logic, the
 +     * subclasses may alter the logic indirectly by returning different types of
 +     * the connector from the factory method.
 +     */
 +    public function post($content): void
 +    {
 +        // Call the factory method to create a Product object...
 +        $network = $this->getSocialNetwork();
 +        // ...then use it as you will.
 +        $network->logIn();
 +        $network->createPost($content);
 +        $network->logout();
 +    }
 +}
 +
 +/**
 + * This Concrete Creator supports Facebook. Remember that this class also
 + * inherits the 'post' method from the parent class. Concrete Creators are the
 + * classes that the Client actually uses.
 + */
 +class FacebookPoster extends SocialNetworkPoster
 +{
 +    private $login, $password;
 +
 +    public function __construct(string $login, string $password)
 +    {
 +        $this->login = $login;
 +        $this->password = $password;
 +    }
 +
 +    public function getSocialNetwork(): SocialNetworkConnector
 +    {
 +        return new FacebookConnector($this->login, $this->password);
 +    }
 +}
 +
 +/**
 + * This Concrete Creator supports LinkedIn.
 + */
 +class LinkedInPoster extends SocialNetworkPoster
 +{
 +    private $email, $password;
 +
 +    public function __construct(string $email, string $password)
 +    {
 +        $this->email = $email;
 +        $this->password = $password;
 +    }
 +
 +    public function getSocialNetwork(): SocialNetworkConnector
 +    {
 +        return new LinkedInConnector($this->email, $this->password);
 +    }
 +}
 +
 +/**
 + * The Product interface declares behaviors of various types of products.
 + */
 +interface SocialNetworkConnector
 +{
 +    public function logIn(): void;
 +
 +    public function logOut(): void;
 +
 +    public function createPost($content): void;
 +}
 +
 +/**
 + * This Concrete Product implements the Facebook API.
 + */
 +class FacebookConnector implements SocialNetworkConnector
 +{
 +    private $login, $password;
 +
 +    public function __construct(string $login, string $password)
 +    {
 +        $this->login = $login;
 +        $this->password = $password;
 +    }
 +
 +    public function logIn(): void
 +    {
 +        echo "Send HTTP API request to log in user $this->login with " .
 +            "password $this->password\n";
 +    }
 +
 +    public function logOut(): void
 +    {
 +        echo "Send HTTP API request to log out user $this->login\n";
 +    }
 +
 +    public function createPost($content): void
 +    {
 +        echo "Send HTTP API requests to create a post in Facebook timeline.\n";
 +    }
 +}
 +
 +/**
 + * This Concrete Product implements the LinkedIn API.
 + */
 +class LinkedInConnector implements SocialNetworkConnector
 +{
 +    private $email, $password;
 +
 +    public function __construct(string $email, string $password)
 +    {
 +        $this->email = $email;
 +        $this->password = $password;
 +    }
 +
 +    public function logIn(): void
 +    {
 +        echo "Send HTTP API request to log in user $this->email with " .
 +            "password $this->password\n";
 +    }
 +
 +    public function logOut(): void
 +    {
 +        echo "Send HTTP API request to log out user $this->email\n";
 +    }
 +
 +    public function createPost($content): void
 +    {
 +        echo "Send HTTP API requests to create a post in LinkedIn timeline.\n";
 +    }
 +}
 +
 +/**
 + * The client code can work with any subclass of SocialNetworkPoster since it
 + * doesn't depend on concrete classes.
 + */
 +function clientCode(SocialNetworkPoster $creator)
 +{
 +    // ...
 +    $creator->post("Hello world!");
 +    $creator->post("I had a large hamburger this morning!");
 +    // ...
 +}
 +
 +/**
 + * During the initialization phase, the app can decide which social network it
 + * wants to work with, create an object of the proper subclass, and pass it to
 + * the client code.
 + */
 +echo "Testing ConcreteCreator1:\n";
 +clientCode(new FacebookPoster("john_smith", "******"));
 +echo "\n\n";
 +
 +echo "Testing ConcreteCreator2:\n";
 +clientCode(new LinkedInPoster("john_smith@example.com", "******"));
 </file> </file>
  
development/software-architecture/design-patterns/factory.1723780112.txt.gz · Last modified: 2024/08/16 03:48 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki