Abstract Factory Pattern in PHP | Complete Guide

Other Blogs

Blogs ❯❯ PHP

Image could not load

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 क्या है?

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 करता है।

Use Case Example :

Suppose , आप एक UI Library develop कर रहे हैं जो different operating systems के लिए components (like buttons, textboxes) generate करे। आप एक abstract factory create करेंगे जो Windows के लिए अलग components और Mac के लिए अलग components generate करे।

Abstract Factory Pattern PHP में क्यों Use करें?

  • 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 नहीं होता।

Abstract Factory Pattern Example in PHP

चलिए अब हम एक UI Components का example लेते हैं जिसमे Windows और Mac के लिए buttons और textboxes create किये जा रहे हैं। हम Abstract Factory Pattern को इस context में समझते हैं।

Step 1: Create Abstract Product Interface

पहले, हम Button और Textbox के लिए abstract Interfaces बनाएंगे, जो different platform-specific components को implement करेंगे।

interface Button { public function render(); } interface Textbox { public function render(); }
Step 2: Create Concrete Products

अब हम 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"; } }
Step 3 : Create Factory Interface

अब हम एक Abstract Factory define करेंगे जिसमे methods होंगे जो Button और Textbox objects create करेंगे।

interface GUIFactory { public function createButton(): Button; public function createTextbox(): Textbox; }
Step 4 : Concrete Factories

अब हम 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(); } }
Step 5 : Client Code

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.

Advantages of Abstract Factory Pattern

  1. Platform Independence : इस pattern के through आप easily different platforms के लिए objects create कर सकते हैं without changing the client code .

  2. Easy To Add New Products : अगर आपको नया platform या product add करना हो, तो आपको सिर्फ एक new concrete factory implement करनी होगी। 

  3. Code Cleanliness : Client code को सिर्फ factory के methods को call करना होता है, जो कि code को clean और organized रखता है।

Limitations of Abstract Factory Pattern

  1. Increased Complexity : अगर आपको सिर्फ एक simple object create करना हो, तो Abstract Factory का use unnecessary complexity create कर सकता है।

  2. Overhead of Multiple Factories : अगर आपको बहुत सारे different objects create करने हो, तो multiple factories का use करना थोड़ा overhead create कर सकता है।

Factory Vs Abstract Factory Pattern

PHP के Factory Pattern और Abstract Factory Pattern में कुछ clear differences हैं जो आपको use case select करने में help करते हैं। नीचे उनको explain किया है -

AspectFactory PatternAbstract Factory Pattern

Focus

Single product

Multiple related products (family)

Abstraction

Low-level

High-level

Complexity

Simple

Complex

Flexibility

Less

High

Implementation

Inheritance

Composition

Conclusion

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 करना चाहिए।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !