Bean এর প্রপার্টি গুলোতে Access এবং Manipulation

Bean Property Access (বিন প্রপার্টি অ্যাক্সেস) - জাভা বীনইউটিলস (Java BeanUtils) - Computer Programming

340

Java Bean হল একটি বিশেষ ধরনের ক্লাস যা সাধারণত ডেটা এনক্যাপসুলেশন, ডেটা প্রক্রিয়াকরণ এবং অবজেক্ট পাসিংয়ের জন্য ব্যবহৃত হয়। Java Bean-এ private fields (প্রপার্টি) থাকে এবং সেগুলির gettersetter মেথডের মাধ্যমে অ্যাক্সেস এবং ম্যানিপুলেশন করা হয়। এর মাধ্যমে কোডের নিরাপত্তা এবং স্থিরতা নিশ্চিত করা যায়।

1. Bean এর প্রপার্টি অ্যাক্সেস (Accessing Bean Properties)

Java Bean-এ সাধারণত private fields বা প্রপার্টি থাকে। এই প্রপার্টিগুলিকে অ্যাক্সেস করার জন্য getter মেথড ব্যবহার করা হয় এবং setter মেথড ব্যবহার করে তাদের মান পরিবর্তন (manipulate) করা হয়।

উদাহরণ:

public class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter method for name
    public String getName() {
        return name;
    }

    // Setter method for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter method for age
    public int getAge() {
        return age;
    }

    // Setter method for age
    public void setAge(int age) {
        this.age = age;
    }
}

ব্যাখ্যা:

  1. private fields: name এবং age ফিল্ড দুটি Person ক্লাসে প্রাইভেট হিসেবে ঘোষণা করা হয়েছে, যা বাইরের ক্লাস থেকে সরাসরি অ্যাক্সেস করা যাবে না।
  2. getter মেথড: getName() এবং getAge() মেথডগুলো name এবং age ফিল্ডগুলোর মান অ্যাক্সেস করার জন্য ব্যবহৃত হয়।
  3. setter মেথড: setName() এবং setAge() মেথডগুলো name এবং age ফিল্ডগুলোর মান সেট করার জন্য ব্যবহৃত হয়।

2. Bean Properties এর Manipulation (মান পরিবর্তন)

Java Bean-এ প্রপার্টি ম্যানিপুলেট (manipulate) করার জন্য setter মেথড ব্যবহার করা হয়। setter মেথডের মাধ্যমে Bean-এর প্রপার্টির মান পরিবর্তন করা যায়।

উদাহরণ:

public class BeanManipulationExample {
    public static void main(String[] args) {
        // Create a new Person object
        Person person = new Person("John", 30);
        
        // Access properties using getter methods
        System.out.println("Before Manipulation:");
        System.out.println("Name: " + person.getName()); // Output: John
        System.out.println("Age: " + person.getAge());   // Output: 30
        
        // Manipulate properties using setter methods
        person.setName("Alice");
        person.setAge(25);

        // Access the manipulated properties
        System.out.println("\nAfter Manipulation:");
        System.out.println("Name: " + person.getName()); // Output: Alice
        System.out.println("Age: " + person.getAge());   // Output: 25
    }
}

ব্যাখ্যা:

  • Before Manipulation: getName() এবং getAge() মেথড ব্যবহার করে person অবজেক্টের প্রপার্টি অ্যাক্সেস করা হয়েছে।
  • After Manipulation: setName() এবং setAge() মেথড ব্যবহার করে person অবজেক্টের প্রপার্টি পরিবর্তন (manipulate) করা হয়েছে।

3. Bean Properties এর Manipulation using BeanUtils

Apache Commons BeanUtils লাইব্রেরি ব্যবহার করে Bean-এর প্রপার্টি অ্যাক্সেস এবং ম্যানিপুলেশন করা সহজ হয়। BeanUtils.copyProperties() মেথড ব্যবহার করে এক Bean থেকে অন্য Bean-এ প্রপার্টি কপি করতে পারি, এবং BeanUtils.setProperty() এবং BeanUtils.getProperty() মেথড ব্যবহার করে প্রপার্টি ম্যানিপুলেটও করতে পারি।

উদাহরণ: BeanUtils ব্যবহার করে Property Manipulation

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsManipulationExample {
    public static void main(String[] args) {
        try {
            // Create a new Person object
            Person person = new Person("John", 30);
            
            // Access and manipulate properties using BeanUtils
            System.out.println("Before Manipulation:");
            System.out.println("Name: " + BeanUtils.getProperty(person, "name")); // Output: John
            System.out.println("Age: " + BeanUtils.getProperty(person, "age"));   // Output: 30
            
            // Manipulate the properties
            BeanUtils.setProperty(person, "name", "Alice");
            BeanUtils.setProperty(person, "age", 25);

            // Access the manipulated properties
            System.out.println("\nAfter Manipulation:");
            System.out.println("Name: " + BeanUtils.getProperty(person, "name")); // Output: Alice
            System.out.println("Age: " + BeanUtils.getProperty(person, "age"));   // Output: 25
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ব্যাখ্যা:

  1. BeanUtils.getProperty(): BeanUtils.getProperty(person, "name") এবং BeanUtils.getProperty(person, "age") ব্যবহার করে Bean-এর প্রপার্টি অ্যাক্সেস করা হয়েছে।
  2. BeanUtils.setProperty(): BeanUtils.setProperty(person, "name", "Alice") এবং BeanUtils.setProperty(person, "age", 25) ব্যবহার করে name এবং age প্রপার্টি পরিবর্তন (manipulate) করা হয়েছে।

4. Bean-এর Nested Properties Access এবং Manipulation

Java Bean-এ যদি nested properties থাকে, যেমন একটি Bean এর মধ্যে আরেকটি Bean থাকে, তবে BeanUtils এর মাধ্যমে সেই nested properties অ্যাক্সেস এবং ম্যানিপুলেট করা সম্ভব।

উদাহরণ: Nested Properties Access and Manipulation using BeanUtils

import org.apache.commons.beanutils.BeanUtils;

public class NestedBeanExample {
    public static void main(String[] args) {
        try {
            // Create Address object
            Address address = new Address("Street 123", "City A");
            
            // Create Person object with nested Address object
            Person person = new Person("John", 30, address);
            
            // Access nested properties using BeanUtils
            System.out.println("Before Manipulation:");
            System.out.println("Street: " + BeanUtils.getProperty(person, "address.street")); // Output: Street 123
            System.out.println("City: " + BeanUtils.getProperty(person, "address.city"));   // Output: City A
            
            // Manipulate nested properties using BeanUtils
            BeanUtils.setProperty(person, "address.street", "Street 456");
            BeanUtils.setProperty(person, "address.city", "City B");

            // Access manipulated nested properties
            System.out.println("\nAfter Manipulation:");
            System.out.println("Street: " + BeanUtils.getProperty(person, "address.street")); // Output: Street 456
            System.out.println("City: " + BeanUtils.getProperty(person, "address.city"));   // Output: City B
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person {
    private String name;
    private int age;
    private Address address;

    public Person() {}

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // Getter and setter methods
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

class Address {
    private String street;
    private String city;

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

ব্যাখ্যা:

  1. Nested Bean: এখানে Person ক্লাসের মধ্যে Address ক্লাস রয়েছে। BeanUtils.getProperty() এবং BeanUtils.setProperty() ব্যবহার করে nested address.street এবং address.city প্রপার্টি অ্যাক্সেস এবং ম্যানিপুলেট করা হয়েছে।
  2. Nested Properties Access and Manipulation: address.street এবং address.city কে BeanUtils এর মাধ্যমে অ্যাক্সেস এবং পরিবর্তন করা হয়েছে।

সারাংশ

  • Java Bean এর প্রপার্টি অ্যাক্স

েস এবং ম্যানিপুলেশন করার জন্য getter/setter মেথড ব্যবহার করা হয়।

  • Apache Commons BeanUtils ব্যবহার করে Bean-এ প্রপার্টি কপি করা, টাইপ কনভার্সন করা এবং nested properties ম্যানিপুলেট করা সহজ হয়।
  • BeanUtils.populate() এবং BeanUtils.copyProperties() মেথড ব্যবহার করে সহজভাবে ডেটা ট্রান্সফার এবং ম্যানিপুলেশন করা যায়।

এভাবে Java Bean-এর প্রপার্টি অ্যাক্সেস এবং ম্যানিপুলেশন করা যায়, যা কোডকে আরও পরিষ্কার এবং সঠিকভাবে পরিচালিত করতে সাহায্য করে।

Content added || updated By
Promotion

Are you sure to start over?

Loading...