Should I use such interfaces?

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:

 //I want this app.getOwner().getId(); app.getOwner().getPIN(); app.getOwner().getAddress(); //I don't want this if(app.getOwner() instanceof Person){ Person owner = app.getOwner(); owner.getId(); owner.getPIN(); owner.getAddress(); } else if(app.getOwner() instanceof Trust){ Trust owner = app.getOwner(); owner.getId(); owner.getPIN(); owner.getAddress(); } 

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.

+4
source share
6 answers

Using interfaces in this way may or may not be appropriate. If you are second guessing, then you probably submitted them too soon.

The practical purpose of interfaces is to allow a piece of code to work against multiple implementations, for example.

 interface Contactable Address getAddress() end class Person implements Contactable Address getAddress(){ ... } end class Company implements Contactable Address getAddress() { ... } end class RenewalReminderJob extends CronJob { public void perform() { for(Contactable upForRenewal : listOfCompaniesAndPeople) { remind(upForRenewal) } } } 

As a rule, it is better to start with specific classes and then introduce interfaces (using refactoring) when you find that you want to perform operations with different classes that contain the same information. In the above example, it will be an Address.

The process of actively searching for interfaces as your code base develops can be very powerful; it allows you to reveal concepts that may not be immediately obvious.

+3
source

The correct way to implement this application has “role objects” (ie, actors play different roles in the application)

An entity may be a person / organization / Trustee, etc.

So, if you structure your classes this way, you will not have a problem.

As an example, I might have an application with

  entity1 (insured ) - person entity2 (payer ) - person entity3 (beneficiary) - trustee entity4 (broker) - organisation 
+1
source

you answered your question

Am I on the right track, or should I do it differently? The thing is that for me a pause is a fact that not every person will be the Owner or payer.

As I think more about this, I feel like Owner and Payer are not “behaviors”, therefore, as “classifications”. Does this make my use of interfaces wrong here?

Interfaces are designed to define behavior.

Have you tried the subclass approach?

0
source

"The owner and the payer do not behave like a classification."

Are there any related behaviors? For example, you might need a method

 sendInvoice(Payor payor) 

or

 Payor getAddressForInvoice() 

If so, you probably need these interfaces, especially if they can be either individuals or corporations.

0
source

The only thing you can do is move from inheritance to deterrence. Instead of saying, “A person or Trust may be an Owner or a Payment”, you could say: “Individuals and trusts may contain owners or payers”:

 public class Person { public Owner getOwner() { /* will return null if this person is not an Owner */ } public Payor getPayor() { /* will return null if this person is not an Payor */ } } public class Trust { public Owner getOwner() { /* will return null if this trust is not an Owner */ } public Payor getPayor() { /* will return null if this trust is not an Payor */ } } 

I’m not sure if this is the right answer or not, although I’m not sure about the business logic of the "Owner" and "Payer" classifications. If a person is the Owner, does this mean that he can own any Application? Or instead, does it simply mean that this particular Person owns this particular Application? If the latter is true, then you can extend both Person and Trust from a common base interface (f.ex. "LegalEntity") and use a visitor template instead of instanceof checks to ensure subclass behavior.

0
source

There is nothing wrong with your interfaces. Owner interface simply means being able to own material, and if you don’t want to be terribly unfair to poor Timmy , I don’t see the reason why he should be excluded from possession.

As for your concern, I think you are a little re-protective: there will be several instances of Owner in your system at the same time, and I do not see a potential developer, believing that they can simply select any one and consider this as the owner of a particular application.

0
source

All Articles