简体   繁体   中英

Java class design issue

Here is a class design: http://pastebin.com/1RSdmtXi

If i put only A to class Expect I would like to see only A's getters and setters, or vica vera to B. If put in A and B , then all of getters and setters should be visible.

So an example for only A :

A[] aExampe = {new A("Tim",1)};
Expect exp = new Expect(aExampe);

exp.getA(); --> visible
exp.getB(); --> not visible

You may advice another design for this.

Try using this:

 Expect.class.getMethod("getA", null).setAccessible(true);
 Expect.class.getMethod("getB", null).setAccessible(false);

But I suggest you to change your architecture.

Why this is a need?

If you change the class Expect (by adding an removing fields), you can add and remove getters and setters too.

If you want to change the class without modifying and recompiling client classes, define an interface containing all needed methods, implement it by Expect class, and instead of removing the methods, just make them empty methods (without any body).

By this pattern, you will not use compile time checking, and won't need to use dirty reflection for normal method calls.

In the class Expect you should only have getA(), setA, getB and setB, not all the getters and setters for A and B's attributes. Those ones belong to those classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM