In object-oriented design, another frequently occurring scenario is the use of c
ID: 3828502 • Letter: I
Question
In object-oriented design, another frequently occurring scenario is the use of conditional statements, for example, an if-else chain or a switch statement to implement functionalities that should have been implemented in distinct concrete subclasses. This anti-pattern causes the code to become brittle and prevents extension. An example can be seen on page 34 of your reading for refactoring submodule.
A solution to this anti-pattern is conditional elimination. From your textbook, "Conditional elimination. This refactoring applies when the structure and behavior of a class is heavily dependent upon a conditional statement. The steps are: (a) create new subclasses corresponding to each condition, (b) migrate the action code from the conditional to the new subclasses, and (c) redirect class references to refer to the subclass types as appropriate. This last change can affect constructors, type declarations, and invocations of overloaded methods. The modified references should maintain the original logical states from the conditional, as invariant assertions on the new classes."
Show an example object-oriented design, distinct from examples presented in your textbook, that illustrates this anti-pattern and another object-oriented design that fixes it by applying the conditional elimination. Your example may be modeled after other examples. For this homework, along with the side of your designs (using diagrams and class descriptions), you may include programming code if it is necessary to illustrate your design.
Explanation / Answer
Ans
Problem with the conditional code:-
If we want to incorparate new functionality ,so without modifying existing conditional code we can not apply changes for new functionality.
If there are multiple classes then we have to use similar algorithm to use similar classes
Solution-
Use strategy pattern
1.)create new abstract class or interface
2.)for each identified algorithm create new subclass
3.)create method implementation in sub class
4.)add a instance vaiable in context class to access the sub class
5.)now we can plugin new algorithm in existing one
Example
public class LoanAccount
public double capitalValue() {
if (expiryTime == null && maturity != null)
return commitmentValue * duration() * riskFactor();
if (expiryTime != null && maturity == null) {
if (getUnusedPercentage() != 1.0)
return commitmentValue * getUnusedPercentage() * duration() * riskFactor();
else
return (outstandingRiskAmount() * duration() * riskFactor())
+ (unusedRiskAmount() * duration() * unusedRiskFactor());
}
return 0.0;
}
Refactor Code
public class LoanAccount
private double outstandingRiskAmount() {
return outstanding;
}
private double unusedRiskAmount() {
return (commitmentValue - outstanding);
}
public double duration() {
if (expiryTime == null && maturity != null)
return weightedAverageDuration();
else if (expiryTime != null && maturity == null)
return yearsTo(expiry);
return 0.0;
}
private double weightedAverageDuration() {
double duration = 0.0;
double weightedAverage = 0.0;
double sumOfPayments = 0.0;
Iterator loanPayments = payments.iterator();
while (loanPayments.hasNext()) {
Payment payment = (Payment)loanPayments.next();
sumOfPayments += payment.amount();
weightedAverage += yearsTo(payment.date()) * payment.amount();
}
if (commitment != 0.0)
duration = weightedAverage / sumOfPayments;
return duration;
}
private double yearsTo(Date endDate) {
Date beginDate = (today == null ? start : today);
return ((endDate.getTime() - beginDate.getTime())
}
private double riskFactor() {
return RiskFactor.getFactors().forRating(riskRating);
}
private double unusedRiskFactor() {
return UnusedRiskFactors.getFactors().forRating(riskRating);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.