জেনেরিক ইন্টারফেস জাভায় এমন একটি ইন্টারফেস যা বিভিন্ন টাইপ প্যারামিটার ব্যবহার করে কাজ করতে পারে। এটি টাইপ-সেইফ কোড লেখার জন্য এবং পুনঃব্যবহারযোগ্যতা বাড়ানোর জন্য ব্যবহৃত হয়। জেনেরিক ইন্টারফেসে টাইপ প্যারামিটার ডিফাইন করার জন্য <T> ব্যবহার করা হয়।
Generic Interface এর সাধারণ Syntax
public interface InterfaceName<T> {
// Methods with the generic type T
void method(T t);
T anotherMethod();
}
এখানে:
<T>: টাইপ প্যারামিটার, যা ইন্টারফেসে টাইপের ডেটা নির্ধারণ করতে ব্যবহৃত হয়।T: টাইপ প্যারামিটার যা ইন্টারফেসের মেথডগুলোর মধ্যে ব্যবহৃত হবে।
Generic Interface এর উদাহরণ
উদাহরণ ১: একটি সাধারণ Generic Interface
// Defining a generic interface
public interface GenericInterface<T> {
void display(T t);
}
উদাহরণ ২: Generic Interface এর বাস্তবায়ন (Implementation)
// Implementing the generic interface with a specific type
public class StringPrinter implements GenericInterface<String> {
@Override
public void display(String t) {
System.out.println("String: " + t);
}
}
// Implementing the generic interface with another type
public class IntegerPrinter implements GenericInterface<Integer> {
@Override
public void display(Integer t) {
System.out.println("Integer: " + t);
}
}
ব্যবহারের উদাহরণ
public class Main {
public static void main(String[] args) {
GenericInterface<String> stringPrinter = new StringPrinter();
stringPrinter.display("Hello Generics!");
GenericInterface<Integer> integerPrinter = new IntegerPrinter();
integerPrinter.display(123);
}
}
আউটপুট:
String: Hello Generics!
Integer: 123
Generic Interface with Generic Implementation
একটি জেনেরিক ইন্টারফেস বাস্তবায়ন করার সময়, বাস্তবায়নকারী ক্লাস নিজেও জেনেরিক হতে পারে।
// Generic Interface Definition
public interface GenericInterface<T> {
void display(T t);
}
// Generic Class Implementing Generic Interface
public class GenericPrinter<T> implements GenericInterface<T> {
@Override
public void display(T t) {
System.out.println("Data: " + t);
}
}
// Using the generic implementation
public class Main {
public static void main(String[] args) {
GenericPrinter<String> stringPrinter = new GenericPrinter<>();
stringPrinter.display("Generic Implementation");
GenericPrinter<Integer> integerPrinter = new GenericPrinter<>();
integerPrinter.display(456);
}
}
আউটপুট:
Data: Generic Implementation
Data: 456
Multiple Type Parameters
জেনেরিক ইন্টারফেসে একাধিক টাইপ প্যারামিটার ব্যবহার করা যায়।
// Defining a generic interface with multiple type parameters
public interface Pair<K, V> {
K getKey();
V getValue();
}
// Implementing the interface
public class KeyValuePair<K, V> implements Pair<K, V> {
private K key;
private V value;
public KeyValuePair(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
}
// Using the implementation
public class Main {
public static void main(String[] args) {
Pair<String, Integer> pair = new KeyValuePair<>("Age", 25);
System.out.println("Key: " + pair.getKey());
System.out.println("Value: " + pair.getValue());
}
}
আউটপুট:
Key: Age
Value: 25
Generic Interface এর সুবিধা
- টাইপ-সেইফটি: টাইপ সংক্রান্ত ভুল কম্পাইল টাইমেই ধরা পড়ে।
- পুনঃব্যবহারযোগ্যতা: একই ইন্টারফেস বিভিন্ন টাইপের জন্য ব্যবহার করা যায়।
- ডাটা টাইপ নির্ভরতা হ্রাস: টাইপ নির্ধারণের সময় ডেভেলপারকে ম্যানুয়াল কাস্টিং করতে হয় না।
Generic Interface কোডে ফ্লেক্সিবিলিটি এবং কার্যকারিতা বাড়ায়। এটি বিশেষত বড় আকারের প্রজেক্টে টাইপ সেফ, ক্লিন এবং পুনঃব্যবহারযোগ্য কোড লিখতে অপরিহার্য।
Content added By
Read more