To transform the Customer-class from a data object, we can ask ourselves the following questions about the data fields:
How do we want to use {data field}? Where is {data field} used? Can and should the use of {data field} be moved to the class?
E.g.:
What is the purpose of
Customer.Name?Possible answers, display the name in a login web page, use the name in mailings to the customer.
Which leads to methods:
- Customer.FillInTemplate(…)
- Customer.IsApplicableForMailing(…)
What is the purpose of
Customer.DOB?Validating the customer's age. Discounts on the customerscustomer's birthday. Mailings.
- Customer.IsApplicableForProduct()
- Customer.GetPersonalDiscount()
- Customer.IsApplicableForMailing()
Given the comments, the example object Customer – both as a data object and as "real" object with its own responsibilities – is too broad; i.e. it has too much properties/responsibilities. Which leads to either lots of components depending on Customer (by reading its properties) or to Customer depending on lots of components. Perhaps there exist different views of customer, perhaps each should have its own distinct class1:
The customer in the context of
Accountand monetary transactions is probably only used to:- help humans identify that their money transfer goes to the correct person; and
- group
Accounts.
This customer does not need fields like
DOB,FavouriteColour,Tel, and perhaps not evenAddress.The customer in the context of a user logging in to a banking website.
Relevant fields are:
FavouriteColour, whcihwhich might come in the form of personalised theming;LanguagePreferences, andGreetingName
Instead of properties with getters and setters, these might be captured in a single method:
- PersonaliseWebPage(Template page);
The customer in the context of marketing and and personalised mailing.
Here not relying on the properties of a dataobject, but instead starting from the responsibilities of the object; e.g.:
- IsCustomerIntrestedInActionIsCustomerInterestedInAction(); and
- GetPersonalDiscounts().
The fact that this customer object has a
FavouriteColourproperty, and/or anAddressproperty becomes irrelevant: perhaps the implementation uses these properties; but it might also use some machine learning techniques and use previous interactions with the customer to discover in which products the customer might be interrestedinterested.
1. OfcourseOf course, the `Customer` and `Account` classes wherewere examples, and for a simple example or homework exercise, splitting this cusomercustomer might be overkill, but with the example of splitting, I hope to demonstrate that the method of turning a data object into an object with responsibilities will work.