Simple এবং Complex Beans এর প্রপার্টি কপি করা

Copying Properties (প্রপার্টি কপি করা) - জাভা বীনইউটিলস (Java BeanUtils) - Computer Programming

352

Java Beans-এর মধ্যে ডেটা কপি করার জন্য Apache Commons BeanUtils একটি শক্তিশালী লাইব্রেরি। এটি Simple Beans (যেখানে প্রপার্টি সরাসরি থাকে) এবং Complex Beans (যেখানে nested বা composition প্রপার্টি থাকে) এর মধ্যে ডেটা কপি করতে পারে। চলুন, এই দুই ধরনের প্রপার্টি কপি করার উদাহরণ দেখি।


1. Simple Beans এর প্রপার্টি কপি করা

উদাহরণ:

import org.apache.commons.beanutils.BeanUtils;

public class SimpleBeanExample {
    public static void main(String[] args) {
        try {
            // Source Bean
            SimpleBean source = new SimpleBean("John", 25);

            // Target Bean
            SimpleBean target = new SimpleBean();

            // Copy properties from source to target
            BeanUtils.copyProperties(target, source);

            // Output the copied properties
            System.out.println("Target Name: " + target.getName());  // Output: John
            System.out.println("Target Age: " + target.getAge());    // Output: 25
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class SimpleBean {
    private String name;
    private int age;

    // Default Constructor
    public SimpleBean() {}

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

    // Getters and Setters
    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;
    }
}

ব্যাখ্যা:

  1. SimpleBean এর প্রপার্টি সরাসরি name এবং age ফিল্ডে রয়েছে।
  2. BeanUtils.copyProperties() ব্যবহার করে source থেকে target এ ডেটা কপি করা হয়েছে।

2. Complex Beans এর প্রপার্টি কপি করা

Complex Beans এ nested properties বা composition properties থাকে। BeanUtils এই ধরনের nested properties কপি করতে পারে।

উদাহরণ:

import org.apache.commons.beanutils.BeanUtils;

public class ComplexBeanExample {
    public static void main(String[] args) {
        try {
            // Nested object (Address)
            Address address = new Address("Street 123", "City A");

            // Source Bean with nested object
            Person source = new Person("John", 30, address);

            // Target Bean
            Person target = new Person();

            // Copy properties from source to target
            BeanUtils.copyProperties(target, source);

            // Output the copied properties
            System.out.println("Target Name: " + target.getName());          // Output: John
            System.out.println("Target Age: " + target.getAge());            // Output: 30
            System.out.println("Target Address Street: " + target.getAddress().getStreet());  // Output: Street 123
            System.out.println("Target Address City: " + target.getAddress().getCity());      // Output: City A
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

    // Default Constructor
    public Person() {}

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

    // Getters and Setters
    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;

    // Default Constructor
    public Address() {}

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

    // Getters and Setters
    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. Person ক্লাসে একটি nested প্রপার্টি Address রয়েছে।
  2. BeanUtils source থেকে target এ সমস্ত প্রপার্টি কপি করে, এমনকি Address ক্লাসের street এবং city এর মতো nested প্রপার্টিগুলিও।

Simple এবং Complex Bean প্রপার্টি কপির মধ্যে পার্থক্য

বৈশিষ্ট্যSimple BeansComplex Beans
প্রপার্টি ধরণসরাসরি প্রপার্টি (e.g., String, int)Nested প্রপার্টি বা composition properties
কপি করা সহজহ্যাঁহ্যাঁ, তবে BeanUtils nested properties হ্যান্ডল করে।
Nested Propertiesথাকে নাথাকে
BeanUtils Compatibilityসরাসরি প্রপার্টি কপি করা সহজ।Nested প্রপার্টি কপি করতে সক্ষম।

BeanUtils কপি করার সীমাবদ্ধতা

  1. Deep Copy না হওয়া:
    • BeanUtils একটি shallow copy তৈরি করে। অর্থাৎ, nested properties এর reference কপি হয়, কিন্তু মূল object নয়। যদি deep copy দরকার হয়, তবে অন্য পদ্ধতি ব্যবহার করতে হবে।
  2. নাল ভ্যালু সমস্যা:
    • যদি কোনো প্রপার্টি null হয়, তবে এটি runtime exception ছুড়তে পারে। তাই null values হ্যান্ডল করার জন্য অতিরিক্ত চেক প্রয়োজন।
  3. Typed Properties:
    • টাইপ মিসম্যাচের ক্ষেত্রে BeanUtils কাজ করবে না, যেমন একটি প্রপার্টি যদি String টাইপের হয় এবং অন্যটি int, তবে এটি exception ছুড়তে পারে।

সারাংশ

  • Simple Beans এ BeanUtils সরাসরি ডেটা কপি করতে পারে যেখানে কোনো nested properties নেই।
  • Complex Beans এ BeanUtils nested properties কপি করার ক্ষমতা রাখে।
  • এটি দ্রুত এবং সহজ পদ্ধতিতে property manipulation করার জন্য একটি শক্তিশালী টুল।
  • তবে, BeanUtils ব্যবহার করার সময় deep copy এবং null value সমস্যা বিবেচনায় রাখতে হবে।

আপনার প্রোজেক্টের জটিলতার ওপর নির্ভর করে আপনি BeanUtils ব্যবহার করে প্রপার্টি কপি করতে পারবেন।

Content added || updated By
Promotion

Are you sure to start over?

Loading...