Frage

I have below the class.

SomeClass.java

   public class SomeClass {

         private String name;
         private String id;
         private String access;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getAccess() {
            return access;
        }
        public void setAccess(String access) {
            this.access = access;
        }

      public void doSomeFunctionality(){

       //does some logic using name and id

       }



}

Now I have to expose only few methods of above class to clients. I can do as below.

SomeOtherClass.java

public class SomeOtherClass {
    private SomeClass someClass = new SomeClass();

    public void setName(String name) {
        someClass.setName(name);
    }


    public void setId(String id) {
        this.setId(id);
    }

}

By doing like above i can restrict one method not accessible to clients and am exposing only required methods. Now client instantiates SomeClass and populates the data into someClass property of SomeOtherClass.java.

But now how can i get the populated data from SomeOtherClass.java?

public SomeClass getSomeClass() {
        return someClass;
    }

If i provide getter method to get SomeClass in SomeOtherClass.java like above then even clients can also access it which i dont want to do.

But if i can get populated someClass instance directly then i can simply call doSomeFunctionality() on the same instance. like getSomeClass().doSomeFunctionality(); How can i overcome above scenario?

Thanks!

War es hilfreich?

Lösung

Your question seems confused, but I think this is the answer you are looking for,

Your approach to letting the client get the information should be exactly the same as letting them set it. You should not provide access to the inner SomeClass - that defeats the whole point. So if you want to provide both read and write access to only the name property, your outer class should look like:

public class SomeOtherClass {
  private SomeClass someClass = new SomeClass();

  public void setName(String name) {
      someClass.setName(name);
  }


  public String getName() {
      return someClass.getName();
  }

}

You don't have to make the properties they can get the same as the properties they can set. You could also allow them to 'get' the ID, for example, but not set it. If you want them to have access to all the properties, provide getters for all properties in SomeOtherClass. If that is the case you might want to consider creating an interface which specifies all the getter methods and is implemented by both SomeClass and SomeOtherClass, with both classes also implementing the setter methods they require.

There are other architectural approaches if you always want to provide full 'read' access but restricted 'write' access, which we can go into if you ask.

Andere Tipps

If you create an interface that is implemented by the class that will be used by the client, but then the client obtains its reference to the instantiation of the class using the interface, even if you have public properties, members, ...etc., the client will only see those provided/defined by the interface.

Don't return the SomeClass object in a getter, just create a getter for the field(s) you want to make available:

public String getName() {
    return someClass.getName();
}

I would suggest thinking about how you would separately test the pieces involved here. If we look at SomeOtherClass, right away we notice that it has a dependency on SomeClass--this is fine. Unfortunately, you hardwire the dependency to SomeClass by using the new operator. If you used a dependency injection framework (e.g., Spring or Guice) you could simply have the framework provide a fully configured instance of SomeClass to SomeOtherClass so that the instance could be used directly and SomeClass is no longer in the business of creating its own dependencies, but rather it goes about doing its useful work. Calling new on SomeClass in SomeOtherClass impedes your ability to test SomeOtherClass independently of SomeClass--you'll be integration testing unless you employ something like PowerMock, which really is better used in emergency situations like when you can't control third-party code. Hope it helps! P.S. all those setters and none for your dependency on SomeClass!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top