If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Abstract Factory Pattern by Luca Bravo on Unsplash
अगर आप PHP में object-oriented programming कर रहे हैं, तो Abstract Factory Pattern को समझना जरूरी है, क्योंकि यह आपके code को highly modular और scalable बनाता है।
इस blog में हम Abstract Factory Pattern इन PHP को detail में समझेंगे, इसका use case देखेंगे, और एक example के through इसका implementation समझेंगे।
●●●
Abstract Factory Pattern एक creational pattern है जो एक Interface provide करता है जिसमे related objects को create करने के लिए methods define किये जाते हैं।
यह pattern एक factory of factories होता है, जिसमे एक factory class के अंदर multiple sub-factories हो सकती हैं, जो different types के objects create करते हैं। इसका major advantage यह है कि client को यह पता नहीं होता के कौनसा concrete object create हो रहा है, बस वो एक interface को use करता है जो उसके लिए required objects generate करता है।
Suppose , आप एक UI Library develop कर रहे हैं जो different operating systems के लिए components (like buttons, textboxes) generate करे। आप एक abstract factory create करेंगे जो Windows के लिए अलग components और Mac के लिए अलग components generate करे।
Flexibility and Scalability : जब आपको multiple related objects create करने हो, जो कि different platforms के लिए हो, तब Abstract Factory pattern आपको flexibility और scalability देता है।
Code Maintainability : अगर आप future में नया object या platform add करते हैं, तो आपको सिर्फ abstract factory को update करना होगा, client code को touch किये बिना।
Avoiding Code Duplication : इस pattern के through आप common logic को एक जगह handle कर सकते हैं, जो कि अलग-अलग concrete classes में duplicate नहीं होता।
●●●
चलिए अब हम एक UI Components का example लेते हैं जिसमे Windows और Mac के लिए buttons और textboxes create किये जा रहे हैं। हम Abstract Factory Pattern को इस context में समझते हैं।
पहले, हम Button और Textbox के लिए abstract Interfaces बनाएंगे, जो different platform-specific components को implement करेंगे।
interface Button {
public function render();
}
interface Textbox {
public function render();
}अब हम Windows और Mac के लिए concrete classes बनाएंगे जो Button और Textbox interfaces को implement करें।
class WindowsButton implements Button {
public function render() {
echo "Windows Button rendered.\n";
}
}
class MacButton implements Button {
public function render() {
echo "Mac Button rendered.\n";
}
}
class WindowsTextbox implements Textbox {
public function render() {
echo "Windows Textbox rendered.\n";
}
}
class MacTextbox implements Textbox {
public function render() {
echo "Mac Textbox rendered.\n";
}
}
अब हम एक Abstract Factory define करेंगे जिसमे methods होंगे जो Button और Textbox objects create करेंगे।
interface GUIFactory {
public function createButton(): Button;
public function createTextbox(): Textbox;
}अब हम Windows और Mac के लिए concrete factory classes बनाएंगे जो GUIFactory interface को implement करेंगे।
class WindowsFactory implements GUIFactory {
public function createButton(): Button {
return new WindowsButton();
}
public function createTextbox(): Textbox {
return new WindowsTextbox();
}
}
class MacFactory implements GUIFactory {
public function createButton(): Button {
return new MacButton();
}
public function createTextbox(): Textbox {
return new MacTextbox();
}
}Client code में हम Abstract Factory का use करेंगे, जिसमे platform के according से different objects create किये जायेंगे।
function renderUI(GUIFactory $factory) {
$button = $factory->createButton();
$textbox = $factory->createTextbox();
$button->render();
$textbox->render();
}
// Creating a Windows UI
$windowsFactory = new WindowsFactory();
renderUI($windowsFactory);
// Creating a Mac UI
$macFactory = new MacFactory();
renderUI($macFactory);Output
Windows Button rendered. Windows Textbox rendered. Mac Button rendered. Mac Textbox rendered.
●●●
Platform Independence : इस pattern के through आप easily different platforms के लिए objects create कर सकते हैं without changing the client code .
Easy To Add New Products : अगर आपको नया platform या product add करना हो, तो आपको सिर्फ एक new concrete factory implement करनी होगी।
Code Cleanliness : Client code को सिर्फ factory के methods को call करना होता है, जो कि code को clean और organized रखता है।
Increased Complexity : अगर आपको सिर्फ एक simple object create करना हो, तो Abstract Factory का use unnecessary complexity create कर सकता है।
Overhead of Multiple Factories : अगर आपको बहुत सारे different objects create करने हो, तो multiple factories का use करना थोड़ा overhead create कर सकता है।
●●●
PHP के Factory Pattern और Abstract Factory Pattern में कुछ clear differences हैं जो आपको use case select करने में help करते हैं। नीचे उनको explain किया है -
| Aspect | Factory Pattern | Abstract Factory Pattern |
Focus | Single product | Multiple related products (family) |
Abstraction | Low-level | High-level |
Complexity | Simple | Complex |
Flexibility | Less | High |
Implementation | Inheritance | Composition |
●●●
Abstract Factory Pattern एक बहुत ही useful design pattern है जब आपको एक complex system बनानी हो जिसमे multiple types के related objects create किये जाते हैं।
यह pattern आपको flexibility, scalability और maintainability provide करता है। अगर आपको future में different platforms के लिए objects create करने हैं, तो Abstract Factory Pattern को अपनी coding practices में ज़रूर include करना चाहिए।
Loading ...