I’m trying to create an API so that the implementation and class names are not needed when other programmers use my service. The service performs a variety of operations on “items” but the main contributing factor to how the service should deal with a request for an item is based on the user type. So far I have an Interface (Item) and an implementing service (ItemService) and then several user type classes that extend from this (e.g. CompanyItemService). The user types also have a structure with the super class being User and subclasses such as Company:
public class ItemService<T extends User> extends Item {
User user;
public ItemService(User user) {
this.user = user;
}
@Override
public boolean belongsToUser() {
//Check all records
}
}
public class CompanyItemService<T extends Company> extends ItemService<Company> {
public CompanyItemService(Company user) {
super(user);
}
@Override
public boolean belongsToUser() {
//Check company specific records
}
}
public class User {
//Common user stuff
}
public class Company extends User {
//Company specific stuff
}
And so I want to be able to use my service with a call like:
User company = new Company();
//stuff
Item item = new ItemService<Company>(company)
item.belongsToUser();
However when I do this the method in the ItemService gets called instead of the overriding method in the CompanyItemService. Is it possible to get this overriding method without needed to know it belongs in the class CompanyItemService?
Cheers,
Alexei Blue.
The general idea to all this is that when another user type is invented another user specific service can be added if needed leaving the existing code untouched. And also if there are lots of user types I only want people using the API to have to remember to use ItemService and for generics to do the rest.
You need to use the abstract factory pattern. In short, you need a static method someplace which knows about all the possible
ItemServicesubclasses and, given anItemobject or aClasswhich is a subclass ofUser, returns an instance of the correctItemServicesubclass. In other words, something likeThen you can say
Alternatively, you could build a map:
Then
instance()could just look like