I am in a situation where I want to use interfaces, but the second guessed it myself.
I am writing an application for processing insurance applications. There is an application object that stores all the data about the insurance application. This includes information about the insured person, the entity that owns the insurance policy, and the organization that will pay insurance coverage.
I use the term entity because policies can be owned (or paid for) by a person or trust. I know when I am dealing with an Owner or Payment, but I do not necessarily know if this Owner or Payer is a Person or Trust. I originally created the Owner interface to shorten the listing and instance-based logic. I planned to focus on the Payor interface, but now I have a second guess.
That is probably all you need to know now. Here is the code:
public class Application{ private Person insured; private Owner owner; private Payor payor; ... } public interface Owner{ public void setAddress(Address address); public Address getAddress(); public void setId(String id); public String getId(); public void setPIN(String pin); public String getPIN(); } public interface Payor{ public void setAccount(Account account); public Account getAccount(); } public class Person implements Owner, Payor{ ... } public class Trust implements Owner, Payor{ ... }
Am I on the right track, or should I do it differently? What gives me a pause is that not every Person will be the Owner or Payer.
As I think more about this, I feel that “Owner and Payer” are not “behaviors” just like “classifications”. Is this the wrong use of the interfaces here? If so, do you have any recommendations regarding alternative solutions? Preferably, those that allow me to continue to use Persons and Trusts transparently as Owners and Payments?
Part of my concern comes from less familiar developers confusing Person to the owner, as shown below.
Application app = new Application(); Person timmy = new Person(); Owner smithFamilyTrust = new Trust(); Payor steve = new Person(); app.setInsured(timmy); app.setOwner(smithFamilyTrust); app.setPayor(steve); ... //this would run, but would be wrong if(timmy instanceof Owner){ //Do Owner Stuff. } //this would run, and be correct Owner owner = app.getOwner(); //Do Owner Stuff
Edit to clarify Owner Property
At this point, the "owner material" is simple. Things like getting / setting the owner ID, PIN or address:
Initially, I thought I should go with some kind of Entity superclass, and Person and Trust will extend this class, but they store and retrieve identifiers and PIN codes in different ways. Different implementations of the same behavior led me to the interfaces.
I could still work with the Entity superclass, but I feel that I can’t accurately represent the “common” identifier or PIN for this Entity class. I would have to implement either an identity identifier, and PIN logic, or a trust identifier, and PIN logic, and then override this in another class. This seems to me wrong. I believe Entity may be an abstract class that is distributed by Person and Trust, but I'm not sure what is better than using an interface.