Question
Feature envy is one of the bad smells in Martin Fowlers?s codesmells. Feature envy describes a situation in which one object getsat the fields of
Feature envy is one of the bad smells in Martin Fowlers?s codesmells. Feature envy describes a situation in which one object getsat the fields of another object to perform some sort of computationor make a decision, rather than asking the object to do thecomputation itself. Inthe Customer class below, themethod getMobilePhoneNumber()provides a NorthAmerican-formattedmobile Phone number.
Note: Suppose the customer class is createdseparately.
Note: Suppose the customer class is createdseparately.
Rewritethe Phone and Customer classto remove the ?feature envy? bad smell improving thecode. [12-Marks]
public class Phone { private final String unformatted Number; public Phone (String unformattedNumber) { this.unformattedNumber=un formattedNumber; } } public String getAreaCode() { return unformatted } public String getPrefix() { return } Number.substring(0,3); public String getNumber() { } public class Customer{ unformattedNumber.substring(3,6); return unformatted Number.substring(6, 10); } private Phone mobilePhone; public String getMobile PhoneNumber() { return "(" + } mobile Phone.getAreaCode() + ") + mobilePhone.getPrefix() + "_" + mobile Phone.getNumber();
Step by Step Solution
3.51 Rating (154 Votes )
There are 3 Steps involved in it
Step: 1
To remove the feature envy code smell in the given Phone and Customer classes we can refactor the co...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started