development:software-architecture:design-patterns:factory
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:software-architecture:design-patterns:factory [2024/08/16 03:48] – [Simple Factory Pattern] tungnt | development:software-architecture:design-patterns:factory [2024/08/19 09:29] (current) – tungnt | ||
---|---|---|---|
Line 128: | Line 128: | ||
{{ : | {{ : | ||
+ | |||
+ | **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:** | ||
+ | |||
+ | {{ : | ||
<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()); | ||
+ | </ | ||
+ | |||
+ | **Ví dụ 2:** | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | <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-> | ||
+ | * | ||
+ | * This allows changing the type of the product being created by | ||
+ | * SocialNetworkPoster' | ||
+ | */ | ||
+ | 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' | ||
+ | */ | ||
+ | abstract public function getSocialNetwork(): | ||
+ | |||
+ | /** | ||
+ | * When the factory method is used inside the Creator' | ||
+ | * subclasses may alter the logic indirectly by returning different types of | ||
+ | * the connector from the factory method. | ||
+ | */ | ||
+ | public function post($content): | ||
+ | { | ||
+ | // Call the factory method to create a Product object... | ||
+ | $network = $this-> | ||
+ | // ...then use it as you will. | ||
+ | $network-> | ||
+ | $network-> | ||
+ | $network-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * This Concrete Creator supports Facebook. Remember that this class also | ||
+ | * inherits the ' | ||
+ | * classes that the Client actually uses. | ||
+ | */ | ||
+ | class FacebookPoster extends SocialNetworkPoster | ||
+ | { | ||
+ | private $login, $password; | ||
+ | |||
+ | public function __construct(string $login, string $password) | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function getSocialNetwork(): | ||
+ | { | ||
+ | return new FacebookConnector($this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * This Concrete Creator supports LinkedIn. | ||
+ | */ | ||
+ | class LinkedInPoster extends SocialNetworkPoster | ||
+ | { | ||
+ | private $email, $password; | ||
+ | |||
+ | public function __construct(string $email, string $password) | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function getSocialNetwork(): | ||
+ | { | ||
+ | return new LinkedInConnector($this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The Product interface declares behaviors of various types of products. | ||
+ | */ | ||
+ | interface SocialNetworkConnector | ||
+ | { | ||
+ | public function logIn(): void; | ||
+ | |||
+ | public function logOut(): void; | ||
+ | |||
+ | public function createPost($content): | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * This Concrete Product implements the Facebook API. | ||
+ | */ | ||
+ | class FacebookConnector implements SocialNetworkConnector | ||
+ | { | ||
+ | private $login, $password; | ||
+ | |||
+ | public function __construct(string $login, string $password) | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function logIn(): void | ||
+ | { | ||
+ | echo "Send HTTP API request to log in user $this-> | ||
+ | " | ||
+ | } | ||
+ | |||
+ | public function logOut(): void | ||
+ | { | ||
+ | echo "Send HTTP API request to log out user $this-> | ||
+ | } | ||
+ | |||
+ | public function createPost($content): | ||
+ | { | ||
+ | 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-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function logIn(): void | ||
+ | { | ||
+ | echo "Send HTTP API request to log in user $this-> | ||
+ | " | ||
+ | } | ||
+ | |||
+ | public function logOut(): void | ||
+ | { | ||
+ | echo "Send HTTP API request to log out user $this-> | ||
+ | } | ||
+ | |||
+ | public function createPost($content): | ||
+ | { | ||
+ | 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' | ||
+ | */ | ||
+ | function clientCode(SocialNetworkPoster $creator) | ||
+ | { | ||
+ | // ... | ||
+ | $creator-> | ||
+ | $creator-> | ||
+ | // ... | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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 " | ||
+ | clientCode(new FacebookPoster(" | ||
+ | echo " | ||
+ | |||
+ | echo " | ||
+ | clientCode(new LinkedInPoster(" | ||
</ | </ | ||
development/software-architecture/design-patterns/factory.1723780112.txt.gz · Last modified: 2024/08/16 03:48 by tungnt