Suppose that we are developing some application that requires us to be able to manage customers and that those customers can be both natural and legal person. There are some different ways on which we can deal with it and I'm not sure which of them is the best regarding extensibility, code reuse and so on.

The first thing I thought was to construct two classes "NaturalCustomer" and "LegalCustomer". Then, the shared properties would be moved to a base class "Customer" and both NaturalCustomer and LegalCustomer would inherit from it.

The second thing I thought was "consider every customer as a natural person, and consider that a legal person customer is just a natural person that has one or more companies". In that case I would create a class "Customer" and then a class "CustumerCompany". If there are other companies in the application that share properties with CustomerCompany I would build a new class Company and inherit from it. In this approach there would be a zero-to-many relationship between Customer and CustomerCompany: a customer can have one or none companies.

I'm not really sure how to deal with this kind of problem. I'm just beginning with object oriented analysis and design and I'm still confused. The main problem is that if I select the first approach I wouldn't be really reusing lots of code. For instance, there could be other kinds of natural person in the app like "Employee" and it would share lots of properties with NaturalCustomer, and the same for companies, there could be companies like "Supplier" and it would share lots of properties with LegalCustomer and still I wouldn't be reusing it - so if I have to make a change on data that should be stored for companies I would have to change everywhere.

How do we deal in this kind of situation when multiple inheritance isn't allowed (that is I cannot make LegalCustomer inherit both from Customer and LegalPerson)?

Thanks very much in advance!

  • 1
    Don't focus too much on properties to see if two objects should share code; they may not have much else in common.
    – JeffO
    Aug 16 '13 at 4:41
  • 2
    Best discussion I have seen on dealing with these kinds of things, including people being both employee, manager, client, or supplier is an article by Martin Fowler: Dealing with roles Aug 16 '13 at 8:52
  • See also: en.wikipedia.org/wiki/Role_Class_Model
    – Doc Brown
    Oct 20 '13 at 7:28

Look into the concept of "Involved Party".

These can be legal or natural persons. You can then define a set of roles which relate them to your other Customers e.g. "Owns", "Works For", "Manages","Supplies", "Major Customer", "Creditor", "Debtor" etc. etc.

This concept is commonly used in financial systems to evaluate the overall risk. If you know a person "Owns" a company which is performing badly then it increases the risk of lending to the owner etc.


What are you using Customer (legal or otherwise) for?

I suspect you might be confusing an account (id, balance, order history, etc..) and an authentication/person (name, form of address, etc.).

If you stretch things, it's a N-N relationship (although practically you can probably get away with a person having at most one account).

  • It's not an account, it's just to have stored in the database the list of customer and be able to register informations about transactions and so on. Aug 15 '13 at 23:10
  • 1
    @user1620696 - design your code structure for now and worry about the database later.
    – JeffO
    Aug 16 '13 at 4:36
  • +1 - design your model, then figure out a way to implement it given your language/database/whatever limitations.
    – ptyx
    Aug 16 '13 at 16:46

You're occluding CUSTOMER with CONTACT. The latter is whom you write contracts to and send bills to and if necessary send to a lawyer to sue. The former is who can manage the details of your relation with.

Note that, while customers may or may not be actual persons, contacts always are. And even customers who are legal persons may have someone else appointed as their contact. (spouse, guardian, poa, executor, etc.)

If you need to indicate the precise relation between a contract and a customer, include it as a property of the relation between them, which may be included in the contact object if you will not be sharing contracts between customers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.