Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrapping an Object

I have an Object which comes and has a bunch of public attributes and no getters and setters. BAD! So I created a class with the attributes and created getters and setters for them. My plan is to wrap the object in my class so it means no direct access to attributes. I'm kind of unsure how to do this. I understand casting fine. How exactly could I wrap the class with my safe class with getters and setters and gain access to the attributes via my getters and setters?

like image 930
Will Avatar asked May 26 '11 12:05

Will


People also ask

What is a wrapped object?

Wrapping an object means we are covering that object, say for example� in Java we can say�String is a class as well as�String is a data type.String s;�� // here s is object of String class ��� String.valueOf(12) // here String is a class , valueOf() is a method, valueOf() method works without�called by String Object.

What is wrapping of object in Java?

A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Need of Wrapper Classes.


3 Answers

Maybe like this?

class MyCar implements ICar{

    private final Car car;
    public MyCar(Car car)
    {
         this.car = car;
    }

    public string getModel()
    {
          return car.model;
    }

    public void setModel(string value)
    {
          car.model = value;
    }

}

now instead of passing around an instance of Car, you can either pass around MyCar instance which has getters and setters or a reference to ICar which will let you control exactly what you would like to expose (for example you could just expose the getters).

like image 79
Bala R Avatar answered Oct 15 '22 02:10

Bala R


Use composition. If you class with public properties is called Exposed, just do

public class ExposedProtector {
    private Exposed exposed;  // private means it can't be accessed directly from its container

    //public/protected methods here to proxy the access to the exposed.



}

Note that nothing will prevent other people from creating instances of Exposed. You will have to modify the actual exposed class itself, which might be the better way to do this, if possible.

You should look at the java access modifiers. There are varying levels of access, from private to protected to public.

like image 21
hvgotcodes Avatar answered Oct 15 '22 01:10

hvgotcodes


If you want your class to be plug-compatible with the original class (meaning that client code does not need to change variable types), then your class will have to be a subclass of the class that the client code expects. In that case, you cannot hide the public variables, although you can easily add getters and setters. Even if you subclass, however, that won't help if the original class has other subclasses; they won't see those getters and setters.

If you can introduce an unrelated class, then the solution is to delegate everything:

public class BetterThing {
    private Thing thing;
    public BetterThing(Thing thing) {
        this.thing = thing;
    }
    public int getIntProperty1() {
        return thing.property1;
    }
    public void setIntProperty1(int value) {
        thing.property1 = value;
    }
    // etc.
}
like image 40
Ted Hopp Avatar answered Oct 15 '22 02:10

Ted Hopp