For a project we are developing, we are in need of a lightweight and a heavyweight object of the same entity class. We are having a simple solution, but need to know if there is any better solutions from the expert perspective. Our solution is as follows:
public interface User{
//Some variables and methods
}
public class LightWeightUser implements User{
//Lightweight code goes here
}
public class HeavyWeightUser implements User{
//HeavyWeight code goes here
}
We are pla开发者_如何转开发nning to use hibernate for database handling part.
Here's one possible alternative:
public class User {
//Light-weight code goes here.
}
public class HeavyWeightUser extends User {
// Heavy-weight code goes here.
}
I would use a decorator pattern for this, if you have the non functional requirement that your code should be easily upgradeable. It would look as the following:
//first define a basic layout for your class
public abstract class User{
public abstract String foo(String bar);
}
//then extend it and implement real behavior
public class concreteUser extends User {
public String foo(String bar) {
...
}
}
//now comes the interesting part... the decorator. At first we need to define a layout for our decorators, that extends your implementation
public abstract class UserDecorator extends User {
@Override
public abstract String foo(String bar);
}
//now you are ready to do everything you want
With this 3 classes you can now start to "decorate" the heavyweight and lightweight behaviour in every way possible to your class. Lets have an example and create a decorator:
public class AsdfUserDecorator extends UserDecorator {
private final User user;
public AsdfUserDecorator(User user) {
this.user = user;
}
@Override
public String foo(String bar) {
//do stuff
...
//propagate everything to other decorators (this is the magic)
return foo(user.foo(bar));
}
private String additionalHeavyweightStuff(String asdasd) {
return blubb;
}
}
//and another one
public class QwerUserDecorator extends UserDecorator {
//no changes in the class in this example... its the same as AsdfUserDecorator....
private final User user;
public AsdfUserDecorator(User user) {
this.user = user;
}
@Override
public String foo(String bar) {
//do stuff
...
//propagate everything to other decorators (this is the magic)
return foo(user.foo(bar));
}
private String additionalHeavyweightStuff(String asdasd) {
return blubb;
}
}
Now you can decorate a user with the following code:
public static void main(String args[]) {
User user = new concreteUser();
user = new AsdfUserDecorator(user);
user = new QwerUserDecorator(user);
user.foo("sadf");
}
This is a really powerful pattern, I hope I could help you.
Have a nice day.
Another aproach to consider would be to use delegation insted of inheritance.
public class HeavyWeightUser {
//HeavyWeightUser properties here
//....
private User user;
//exposing user properties here
public Something getUserProperty(){return something;}
public void serUserProperty(Something something){this.user.setSomething(something);}
}
This has advantages like:
- you are free to inherit form parent class because you didnt use up this option jet
- you can expose only properties that make sence
- your object are not so tightly coupled as with inheritance, which usualy means you can change your implementation with more ease.
Disadvantages:
- More coding
精彩评论