Factory Method জানার আগে জানতে হবে Method কি ?
শর্তমতো Class Diagram টা দাঁড়ায় -
আমরা চাইলে abstract class Service টা বাদ দিতে পারি । যেহেতু আমাদের প্রত্যেকটা সার্ভিসের সার্ভিস মানি ও কারেন্ট অবস্থা জানতে হবে তাই একটা abstract class এর মধ্যে কমন মেথড লিখে অন্যান্য সার্ভিস ক্লাসে override করিয়ে যার যার মতো ইমপ্লিমেন্ট করে নিয়েছি । এই কাজ টা একটা Interface তৈরি করেও করা যেতে পারে ।
When to use abstract classes and when to use interfaces ?
নিচে দেখুন, আমরা এখানে যখন যার অবজেক্ট দরকার হচ্ছে সেটা তৈরি করিয়ে নিচ্ছি । আগে থেকে সবগুলোর অবজেক্ট তৈরি করে সিস্টেম ভারী করে রাখছি না । এই টেকনিক -কে বলে loose coupling । যখন দরকার হবে তখনই তৈরি করে নিবো, আগে থেকে তৈরি করে মেমোরি ভর্তি করে রাখবো না । loose coupling এর আরো অনেক খুঁটিনাটি বিষয় আছে যেগুলো ভিন্ন আর্টিকেলে বিস্তারিত আসবে আপাতত এটুক জানলেই চলবে ।
আউটপুট :
পরামর্শ
Method একটা ছাঁচের মতো, আপনার প্রয়োজনীয় উপকরণ(প্যারামিটার) ছাঁচের মধ্যে দিবেন, ছাঁচ সেটা নিজের ফর্মুলা মতো উপকরণ প্রসেস করে একটা সুগঠিত ব্যবহারযোগ্য ফলাফল ফেরত(রিটার্ন) দিবে । তাহলে বলতে পারি একটা নির্দিষ্ট প্রসেসের কাজেই আমরা Method লিখি ।
Factory Method হচ্ছে বিশেষ ধরনের Method, যেটা রিকোয়েস্টেড সার্ভিস ক্লাসের Object Return করে ।
আমরা আবার সেই TeamPossible ইভেন্ট ম্যানেজমেন্ট ফার্মের উদাহরণে ফিরে যায় যেটা Facade Pattern এ ব্যবহার করছিলাম, যারা নিচের সার্ভিস গুলো দিয়ে থাকে
আমরা আবার সেই TeamPossible ইভেন্ট ম্যানেজমেন্ট ফার্মের উদাহরণে ফিরে যায় যেটা Facade Pattern এ ব্যবহার করছিলাম, যারা নিচের সার্ভিস গুলো দিয়ে থাকে
- খাবার আয়োজন (বাবুর্চির কাজ)
- চেয়ার টেবিল সাজানো, খাবার পরিবেশন (ডেকোরেটরের কাজ)
- বউ সাজানো (পার্লারের কাজ)
কিন্তু এবার আর আমাদের একসাথে সব সার্ভিসের দরকার নেই । যার যেটা দরকার সেটা অর্ডার করলেই TeamPossible সরবরাহ করবে ।
শর্তমতো Class Diagram টা দাঁড়ায় -
Factory Method Class Diagram |
এখানে আসল ম্যাজিক হচ্ছে GetService ক্লাসের callingService() মেথড । এটাই আমাদের Factory Method যেখানে প্রয়োজনমতো সার্ভিসের প্যারামিটার দিলে সেই সার্ভিস ক্লাসের Object create করে রিটার্ন করবে ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Service { | |
double cost; | |
abstract double getCost(); | |
abstract String serviceStatus(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BeautyParlor extends Service { | |
@Override | |
double getCost() { | |
return 5000; | |
} | |
@Override | |
String serviceStatus() { | |
return "Bride is ready and "+getCost()+" is your bill \n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cook extends Service { | |
@Override | |
double getCost() { | |
return 20000; | |
} | |
@Override | |
String serviceStatus() { | |
return "Items are ready and "+getCost()+" is your bill \n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Decorator extends Service { | |
@Override | |
double getCost() { | |
return 10000; | |
} | |
@Override | |
String serviceStatus() { | |
return "Decoration is ready and "+getCost()+" is your bill \n"; | |
} | |
} |
আমরা চাইলে abstract class Service টা বাদ দিতে পারি । যেহেতু আমাদের প্রত্যেকটা সার্ভিসের সার্ভিস মানি ও কারেন্ট অবস্থা জানতে হবে তাই একটা abstract class এর মধ্যে কমন মেথড লিখে অন্যান্য সার্ভিস ক্লাসে override করিয়ে যার যার মতো ইমপ্লিমেন্ট করে নিয়েছি । এই কাজ টা একটা Interface তৈরি করেও করা যেতে পারে ।
When to use abstract classes and when to use interfaces ?
নিচে দেখুন, আমরা এখানে যখন যার অবজেক্ট দরকার হচ্ছে সেটা তৈরি করিয়ে নিচ্ছি । আগে থেকে সবগুলোর অবজেক্ট তৈরি করে সিস্টেম ভারী করে রাখছি না । এই টেকনিক -কে বলে loose coupling । যখন দরকার হবে তখনই তৈরি করে নিবো, আগে থেকে তৈরি করে মেমোরি ভর্তি করে রাখবো না । loose coupling এর আরো অনেক খুঁটিনাটি বিষয় আছে যেগুলো ভিন্ন আর্টিকেলে বিস্তারিত আসবে আপাতত এটুক জানলেই চলবে ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetService { | |
public Service callingService(String serviceName) { | |
switch (serviceName) { | |
case "cook": | |
return new Cook(); | |
case "beauty parlor": | |
return new BeautyParlor(); | |
case "decorator": | |
return new Decorator(); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TeamPossibleEMFarm { | |
public static void main(String[] args) throws Exception { | |
String serviceName = null; | |
GetService getService = new GetService(); | |
do{ | |
System.out.print("==== Service Menu ====\n"); | |
System.out.print(" 1. cook. \n"); | |
System.out.print(" 2. beauty parlor. \n"); | |
System.out.print(" 3. decorator. \n"); | |
System.out.print(" 4. exit \n"); | |
System.out.print("Please make a Order: "); | |
Scanner sc = new Scanner(System.in); | |
serviceName = sc.nextLine(); | |
switch (serviceName) { | |
case "cook":{ | |
Service callingCook = getService.callingService("cook"); | |
System.out.println(callingCook.serviceStatus()); | |
} | |
break; | |
case "beauty parlor":{ | |
Service callingBeautyParlor = getService.callingService("beauty parlor"); | |
System.out.println(callingBeautyParlor.serviceStatus()); | |
} | |
break; | |
case "decorator":{ | |
Service callingDecorator = getService.callingService("decorator"); | |
System.out.println(callingDecorator.serviceStatus()); | |
} | |
break; | |
case "exit":{ | |
System.out.println("### Thank you sir. Hope we will be invited later \n"); | |
} | |
return; | |
default:{ | |
System.out.println("### Sir, "+serviceName+" service is not available \n"); | |
} | |
break; | |
}//end of switch | |
}while(serviceName != "exit"); | |
} | |
} |
ধরুন আমরা Cook এর সার্ভিস চাচ্ছি । তাহলে Cook এর অবজেক্ট তৈরি হবে । এবং নিচের মতো আউটপুট পাবো । একইভাবে এই callingService() মেথড দিয়ে অন্য সার্ভিসগুলোও পেতে পারি ।
আউটপুট :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==== Service Menu ==== | |
1. cook. | |
2. beauty parlor. | |
3. decorator. | |
4. exit | |
Please make a Order: cook | |
Items are ready and 20000.0 is your bill | |
==== Service Menu ==== | |
1. cook. | |
2. beauty parlor. | |
3. decorator. | |
4. exit | |
Please make a Order: exit | |
### Thank you sir. Hope we will be invited later |
পরামর্শ
প্রোগ্রামিং এ নতুন কিছু শিখে ভালো মতো আয়ত্ত করতে চাইলে কোড লেখা বা ঐ বিষয়ের problem solving এর আর কোন বিকল্প নেই । এখন HackerRank থেকে Factory Method Pattern এর প্রব্লেমটা সল্ভ করে ফেলুন । মনে রাখবেন, যায় শিখুন আর যতবারই পড়ুন না কেনো কোড না লিখলে সেটা আপনার আয়ত্তে আসবে না ।
THE END
Factory Method Pattern
Reviewed by hnjaman
on
July 08, 2018
Rating:
No comments: