java - how to create wrapper class for any user defined class - Stack Overflow
Example for Wrapper Classes
Keep in mind that there is overlap between design patterns and sometimes not everyone agrees on what an implementation looks like. That being said, I would say that a wrapper encloses some class and allows that class to work with some protocol or interface that it originally did not accommodate.
class Ice-T {
sing() {... }
act() { ... }
compose() { ... }
};
But now we have our class in production and we suddenly want it to do the things it already does but in China, where the laws are different. So, we create a wrapper class that encloses our Ice-T class and enforces Chinese laws before permitting calls to sing(), act(), and compose().
class RapperWrapper()
private Ice-T iceT;
void checkAndSing();
void checkAndAct();
void checkAndCompose();
};
But, wait, why don't we just add the logic to our original class and be done with it? Well, we want to maintain separation of concerns. Our wrapper class doesn't know how to sing but it does know how to make sure that Chinese law are complied with before singing is permitted. Also, our original class is already in production and we can't change it: perhaps we're not even allowed to change it because someone else provides it to us.
More on reddit.comI don't understand wrapper classes
Integer b = new Integer(8); b=b+1; System.out.println(b);
here I added 1 to b and I got 9 on the console, I could change the Integer object within the main method
You're not actually "changing" the Integer object here. What you're doing is creating a new Integer, with a value of 9, and then storing it in the b variable, throwing away the old object which had the value 8.
i.e. you're changing the variable to point to a new object, rather than modifying the object in-place.
Modifying / mutating the original object would look something like this - it's not something valid which you can do:
Integer b = new Integer(8); b.increaseBy(1); System.out.println(b);
The difference is subtle, but it also explains why the code in 'main' couldn't "see" the changes made in the 'add' method.
I also used valueOf and parseInt methods, valueOf method returns an Integer object but I assigned an int primitive type to it and it worked. Similarly, I declared an Integer object and assigned it to the result of parseInt method which returns a primitive type and it also worked.
Basically Java will automatically convert between Integer and int for you, based on what's required in any given context.
This is called "automatic boxing / unboxing".
The main reason Integer exists is:
-
It's possible to have a 'null' Integer, but not a null int - if you want to allow a null value, you need to use the object wrapper type
-
Collections like List, Set, Map etc do not support primitive types, only objects - so these object wrapper / boxed versions need to exist to allow you to store primitive values in collections
Any reason to use a class instead of a primitive when declaring fields?
Videos
The term 'wrapping' sometimes means the same thing as encapsulation, where an object or type is used internally by a class as part of its implementation details, and doesn't expose it to outside code. However, wrapping often refers specifically to the act of encapsulating a class in another class which implements the same interface as the wrapped class but changes its behaviour slightly or adds new features (Decorator Pattern), or the outer class implements a different interface, essentially converting the wrapped class to make it compatible with another program (Adapter Pattern). Both of these types of wrapping are nearly always done manually, and must be done at compile-time (by writing code).
You can also generate dynamic proxies for virtually any object at runtime using java.lang.reflect.Proxy.newProxyInstance(...). You can read the official guide on Dynamic Proxy Classes to learn how to use it. However, you haven't given any use cases yet, so this might not be what you're looking for. Proxies are usually reserved for protecting objects or delegating to a remote server via RPC, and can be very complex.
Sample code for creating wrapper :
import java.util.ArrayList;
import java.util.List;
class IntVal {
private int i;
public IntVal(int a) {
i = a;
}
public int getVal() {
return i;
}
public void setValue(int a) {
this.i = a;
}
public void increment() {
i++;
}
@Override
public String toString() {
return Integer.toString(i);
}
}
public class WrapperClass {
public static void main(String[] args) {
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
list.add(new IntVal(i));
}
System.out.println(list);
for (int i = 0; i < list.size(); i++) {
((IntVal) list.get(i)).increment();
}
System.out.println(list);
}
}