ফ্যাক্টরি মেথড হল ক্রিয়েশনাল ডিজাইন প্যাটার্ন যেটা এমন একটা ইন্টারফেস থাকে superclass এ অবজেক্ট তৈরি করার জন্যে কিন্তু subclass এ অবজেক্ট টাইপ পরিবর্তন করা যায় প্রয়োজন অনুসারে।
উদাহরন
১. java.util.Calendar ক্লাস দুইটি বিল্ডার প্যাটার্ন ফলো করে তৈরি করা।
সমস্যা
আমাদেরকে কোন কোন সময় বিভিন্ন ধরনের ক্লাস অবজেক্ট তৈরি করার প্রয়োজন পড়ে। সেই ক্ষেত্রে সাধারণভাবে আমরা যখন দরকার পড়বে তখন সেই অবজেক্ট তৈরি করে নেব। কিন্তু এখন আমার যদি প্রয়োজন পড়ে আমার যত অবজেক্ট আছে আমি একটা ক্লাসের মাধ্যমে তৈরি করতে পারবো। যেমনঃ আমার মটরসাইকেল, ট্রাক, কার ইত্যাদি অবজেক্ট আছে এইগুলো আমি একটা ক্লাস থেকে কল করে তৈরি করবো। যাতে একটি ক্লাসে আমার একই ধরনের অবজেক্টগুলো তৈরি করতে পারি। এই ক্ষেত্রে আমাদেরকে এমন একটি ডিজাইন করতে হবে যেখানে আমরা এক জায়গাতেই আমাদের সব অবজেক্টগুলো পাব।
সমাধান
এই ক্ষেত্রে আমরা একটা parent ক্লাস তৈরি করবো যেটা extend করে আমরা অন্যান্য ক্লাস তৈরি করবো। এবং তারপর আমরা একটা অবজেক্ট তৈরি করার একটা ক্লাস তৈরি করবো মানে সেইটা হবে অবজেক্ট ফ্যাক্টরি ক্লাস। এবং তার ভেতরে একটা ফ্যাক্টরি মেথড থাকবে যেটা if-else এর মাধ্যমে বিভিন্ন ধরনের অবজেক্ট তৈরি করতে পারবে।
ইমপ্লিমেন্টেশন
public abstract class Car {
public Car(CarType model) {
}
public Car(CarType luxury, Location location) {
}
private void arrangeParts() {
// Do one time processing here
}
// Do subclass level processing in this method
protected abstract void construct();
private CarType model = null;
public CarType getModel() {
return model;
}
public void setModel(CarType model) {
this.model = model;
}
}
public class CarFactory {
public static Car buildCar(CarType model) {
Car car = null;
switch (model) {
case SMALL:
car = new SmallCar();
break;
case SEDAN:
car = new SedanCar();
break;
case LUXURY:
car = new LuxuryCar();
break;
default:
// throw some exception
break;
}
return car;
}
}
public enum CarType {
SMALL, SEDAN, LUXURY
}
public class LuxuryCar extends Car {
public LuxuryCar() {
super(CarType.LUXURY);
construct();
}
public LuxuryCar(Location location) {
super(CarType.LUXURY, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building luxury car");
// add accessories
}
}
public class SedanCar extends Car {
public SedanCar() {
super(CarType.SEDAN);
construct();
}
public SedanCar(Location location) {
super(CarType.SEDAN, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building sedan car");
// add accessories
}
}
public class SmallCar extends Car {
public SmallCar() {
super(CarType.SMALL);
construct();
}
public SmallCar(Location location) {
super(CarType.SMALL, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building small car");
// add accessories
}
}
public class TestFactory {
public static void main(String[] args) {
System.out.println(CarFactory.buildCar(CarType.SMALL));
System.out.println(CarFactory.buildCar(CarType.SEDAN));
System.out.println(CarFactory.buildCar(CarType.LUXURY));
}
}
সুবিধা - অসুবিধা
১. এই প্যাটার্ন ব্যবহার করলে creator এবং ক্লাসগুলো loose coupling হয়।
২. এই প্যাটার্নের ইমপ্লিমেন্টাশন একটু জটিল ধরনের হয় যদি আমাদের অনেক ধরনের অবজেক্ট হয়।