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.
Factory Design Pattern by Ben Griffiths on Unsplash
Factory Design Pattern एक बहुत ही popular और widely used creational design pattern है जो आपको objects create करने का एक structured तरीका provide करता है। अगर आप PHP developer हैं, तो इस pattern को समझना जरूरी है, क्योंकि ये आपको efficient और maintainable code लिखने में help करता है।
इस blog में हम Factory Pattern इन PHP को समझेंगे, उसका importance जानेंगे , और एक example के through देखेंगे कि कैसे आप इसे implement कर सकते हैं।
●●●
Factory Design Pattern एक creational pattern है जो objects create करने का एक simple तरीका देता है without exposing थे object creation logic तो थे client. मतलब, जब client को object चाहिए होता है, तो उससे directly object का constructor call नहीं करना पड़ता।
उसकी जगह एक factory class या method का use करके object create होता है।
इसका main advantage यह है कि आप अपनी object creation कि logic को client code से hide कर सकते हो, जिससे कि आपका code flexible और maintainable रहे।
Encapsulation of Object Creation : आप object creation कि logic को encapsulate कर सकते हैं, client को इसके बारे में पता भी नहीं चलेगा।
Loose Coupling : Code को concrete classes से कम tightly couple करने में help मिलती है, जिससे code scalable और flexible ban जाता है।
Improved Maintainability : अगर आपको future में object creation logic को change करना हो, तो आप factory class में changes कर के client code को untouched रख सकते हो।
Multiple Object Types Support : अगर आपको multiple types के objects create करने हो, तो factory method का use करके आप complexity को handle कर सकते हो।
●●●
चलिए, अब हम Factory Design Pattern को एक example के through समझते हैं। Suppose आप एक Vehicle Factory बना रहे हैं जिसमे Car, Bike, और Truck जैसे vehicles बनाये जा रहे हैं।
आप इन objects को direct create नहीं करेंगे, बल्कि एक factory class का use करेंगे जो object creation को handle करेगी।
सबसे पहले, हम एक Vehicle interface define करेंगे जिसमे एक method drive() होगा। हर vehicle type (Car, Bike, Truck) को ये implement करना होगा।
interface Vehicle {
public function drive();
}अब हम vehicle types के concrete classes बनाते हैं जो Vehicle interface को implement करें, हर class में drive() method को define किया जाएगा।
class Car implements Vehicle {
public function drive() {
echo "Car is driving on the road.\n";
}
}
class Bike implements Vehicle {
public function drive() {
echo "Bike is riding on the road.\n";
}
}
class Truck implements Vehicle {
public function drive() {
echo "Truck is carrying goods.\n";
}
}अब एक Factory Class बनाएंगे जो different vehicles create कर सके. इस class में एक createVehicle() method होगा जो different vehicle types return करेगा।
class VehicleFactory {
public function createVehicle($type) {
switch ($type) {
case 'car':
return new Car();
case 'bike':
return new Bike();
case 'truck':
return new Truck();
default:
throw new Exception("Invalid vehicle type.");
}
}
}अब आप client code में VehicleFactory का use करेंगे और उससे object create करेंगे बिना direct constructor call किये।
$factory = new VehicleFactory();
// Create a car
$car = $factory->createVehicle('car');
$car->drive(); // Output: Car is driving on the road.
// Create a bike
$bike = $factory->createVehicle('bike');
$bike->drive(); // Output: Bike is riding on the road.●●●
Code Reusability : Factory method से आप एक common code लिख सकते हैं जो बार बार reuse हो सकता है।
Code Flexibility : अगर आप future में new vehicle types add करना चाहें , तो आपको सिर्फ factory class को update करना पड़ेगा , client code को नहीं।
Easy Testing : Factory pattern आपके code को modular बनाता है, जो testing को आसान बनाता है।
Complexity : अगर आप एक complex system बनाते हैं जिसमे बहुत सारे objects create हो रहे हैं, तो factory pattern थोड़ा complex हो सकता है।
Overuse : अगर आप unnecessary जगह पर factory pattern use करते हैं, तो code का over-abstraction हो सकता है, जो कि समझने में difficult हो सकता है।
●●●
Factory Design Pattern PHP में एक powerful tool है जो आपको object creation का logic encapsulate करने और maintainable code लिखने में help करता है। अगर आपको अपने PHP applications को scalable और flexible banana है, तो factory pattern को अपनी coding practices में include करना एक अच्छा decision हो सकता है।
Loading ...