Java Tuples হলো একাধিক ডেটা টাইপ একত্রে সংরক্ষণ করার একটি ডেটা স্ট্রাকচার। Tuples-এর Immutable এবং Compact প্রকৃতির কারণে এটি Observer Pattern এবং Command Pattern-এর মতো ডিজাইন প্যাটার্নে কার্যকরভাবে ব্যবহৃত হয়।
১. Observer Pattern এ Tuple এর Integration
Observer Pattern হলো একটি Behavioral Design Pattern, যেখানে একটি অবজেক্ট (Subject) এর অবস্থা পরিবর্তন হলে সেটি তার সাথে সংযুক্ত Observers-কে নোটিফাই করে।
Tuple Integration ব্যবহার করলে:
- Observers সহজে একাধিক ডেটা পেতে পারে।
- কমপ্যাক্ট এবং রিডেবল কোড তৈরি হয়।
- একাধিক ডেটার পরিবর্তন একত্রে পাস করা যায়।
Example: Observer Pattern with Tuples
Step 1: Create an Observer Interface
import io.vavr.Tuple2;
public interface Observer {
void update(Tuple2<String, Integer> data);
}
Step 2: Create a Subject Class
import io.vavr.Tuple;
import io.vavr.Tuple2;
import java.util.ArrayList;
import java.util.List;
public class Subject {
private final List<Observer> observers = new ArrayList<>();
private Tuple2<String, Integer> state;
public void attach(Observer observer) {
observers.add(observer);
}
public void setState(String name, int value) {
state = Tuple.of(name, value);
notifyObservers();
}
private void notifyObservers() {
for (Observer observer : observers) {
observer.update(state);
}
}
}
Step 3: Implement Observer Classes
public class ConcreteObserver implements Observer {
private final String observerName;
public ConcreteObserver(String observerName) {
this.observerName = observerName;
}
@Override
public void update(Tuple2<String, Integer> data) {
System.out.println(observerName + " received update: " + data._1 + " -> " + data._2);
}
}
Step 4: Use the Observer Pattern
public class ObserverPatternExample {
public static void main(String[] args) {
Subject subject = new Subject();
Observer observer1 = new ConcreteObserver("Observer1");
Observer observer2 = new ConcreteObserver("Observer2");
subject.attach(observer1);
subject.attach(observer2);
subject.setState("Temperature", 30);
subject.setState("Humidity", 70);
}
}
Output:
Observer1 received update: Temperature -> 30
Observer2 received update: Temperature -> 30
Observer1 received update: Humidity -> 70
Observer2 received update: Humidity -> 70
২. Command Pattern এ Tuple এর Integration
Command Pattern হলো একটি Behavioral Design Pattern, যা কোনো নির্দিষ্ট অনুরোধকে একটি অবজেক্ট হিসেবে ইনক্যাপসুলেট করে এবং এই অনুরোধকে প্যারামিটার হিসেবে পাস করার সুবিধা দেয়।
Tuple Integration ব্যবহার করলে:
- Command এর প্যারামিটার সহজে একত্রে সংরক্ষণ করা যায়।
- Command এর Execution Context কমপ্যাক্ট হয়।
Example: Command Pattern with Tuples
Step 1: Create a Command Interface
import io.vavr.Tuple2;
public interface Command {
void execute(Tuple2<String, Integer> data);
}
Step 2: Implement Concrete Command Classes
public class PrintCommand implements Command {
@Override
public void execute(Tuple2<String, Integer> data) {
System.out.println("Executing command: " + data._1 + " with value " + data._2);
}
}
public class MultiplyCommand implements Command {
@Override
public void execute(Tuple2<String, Integer> data) {
System.out.println("Result of multiplication: " + (data._2 * 2));
}
}
Step 3: Create an Invoker Class
import io.vavr.Tuple2;
import java.util.ArrayList;
import java.util.List;
public class Invoker {
private final List<Command> commandHistory = new ArrayList<>();
public void executeCommand(Command command, Tuple2<String, Integer> data) {
command.execute(data);
commandHistory.add(command);
}
}
Step 4: Use the Command Pattern
import io.vavr.Tuple;
public class CommandPatternExample {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Command printCommand = new PrintCommand();
Command multiplyCommand = new MultiplyCommand();
Tuple2<String, Integer> data1 = Tuple.of("Task1", 10);
Tuple2<String, Integer> data2 = Tuple.of("Task2", 20);
invoker.executeCommand(printCommand, data1);
invoker.executeCommand(multiplyCommand, data2);
}
}
Output:
Executing command: Task1 with value 10
Result of multiplication: 40
Observer এবং Command Pattern এ Tuple Integration এর সুবিধা
- Compact and Clean Code:
- একাধিক প্যারামিটার Tuples-এ একত্রিত করে পাস করার মাধ্যমে কোড ছোট এবং পরিষ্কার হয়।
- Immutable Data Structure:
- Tuples Immutable হওয়ায় Observers বা Commands-এ ডেটা পরিবর্তনের ঝুঁকি থাকে না।
- Multiple Parameters Handling:
- সহজে একাধিক ডেটা সংরক্ষণ এবং প্রসেস করা যায়।
- Reduced Boilerplate Code:
- Aligned ডেটা মডেলের প্রয়োজন নেই; Tuples সরাসরি ব্যবহার করা যায়।
Observer এবং Command Pattern এ Tuple Integration এর সীমাবদ্ধতা
- Readability Issues:
- Tuples এর
_1,_2এর মতো নামবিহীন ফিল্ড ব্যবহারের কারণে কোড কম রিডেবল হয়।
- Tuples এর
- Complex Scenarios:
- জটিল ডেটা মডেলের জন্য Tuples এর পরিবর্তে কাস্টম ক্লাস বা
POJOব্যবহার করা ভালো।
- জটিল ডেটা মডেলের জন্য Tuples এর পরিবর্তে কাস্টম ক্লাস বা
- Dependency Requirement:
- Tuples ব্যবহার করতে হলে তৃতীয় পক্ষের লাইব্রেরি (যেমন Vavr) প্রয়োজন।
Observer Pattern:
- Tuples একাধিক ডেটা পাস করার জন্য আদর্শ।
- Observers সহজে কমপ্লেক্স ডেটা প্রসেস করতে পারে।
Command Pattern:
- Tuples Commands-এ প্রয়োজনীয় প্যারামিটার সংরক্ষণ সহজ করে।
- Immutable Tuples ডেটা নিরাপদ রাখে।
Best Practice:
- Tuples ছোট এবং সিম্পল ডেটা মডেলের জন্য ব্যবহার করুন।
- বড় এবং জটিল ডেটা মডেলের ক্ষেত্রে কাস্টম ক্লাস বা
Recordব্যবহার করা ভালো।
Read more