Detailed Explanation of the Richter Replacement Principle Example of java Object Oriented Design Principle
- 2021-11-29 07:06:54
- OfStack
Concept
The Richter substitution principle is that wherever the base class appears, subclass 1 can definitely replace it; It is the cornerstone of basic reuse based on abstraction, polymorphism and inheritance. This principle can ensure that the system has good extensibility, and at the same time realize the abstraction mechanism based on polymorphism, which can reduce code redundancy.
Realization
Richter substitution principle requires us to use base classes or interfaces to define object variables when coding, and assign values to specific implementation objects when using them, so as to realize the diversity of changes and complete the closure of code modification and the opening of expansion. For example, in the commodity settlement of the mall, the settlement interface Istrategy is defined, which has three specific implementation classes, namely PromotionalStrategy (full reduction activity, more than 200% off), RebateStrategy (discount activity) and ReduceStrategy (cashback activity);
public interface Istrategy {
public double realPrice(double consumePrice);
}
public class PromotionalStrategy implements Istrategy {
public double realPrice(double consumePrice) {
if (consumePrice > 200) {
return 200 + (consumePrice - 200) * 0.8;
} else {
return consumePrice;
}
}
}
public class RebateStrategy implements Istrategy {
private final double rate;
public RebateStrategy() {
this.rate = 0.8;
}
public double realPrice(double consumePrice) {
return consumePrice * this.rate;
}
}
public class ReduceStrategy implements Istrategy {
public double realPrice(double consumePrice) {
if (consumePrice >= 1000) {
return consumePrice - 200;
} else {
return consumePrice;
}
}
}
The caller is Context, and one object is defined in this class using the interface. The code is as follows:
public class Context {
// Define object variables using base classes
private Istrategy strategy;
// Inject concrete objects used by the current activity
public void setStrategy(Istrategy strategy) {
this.strategy = strategy;
}
// Calculate and return expenses
public double cul(double consumePrice) {
// Use specific commodity promotion strategies to obtain actual consumption amount
double realPrice = this.strategy.realPrice(consumePrice);
// Formatting preserves decimal point 1 Bit, that is, accurate to angle
BigDecimal bd = new BigDecimal(realPrice);
bd = bd.setScale(1, BigDecimal.ROUND_DOWN);
return bd.doubleValue();
}
}
The code in Context uses interface to define object variables, which can be any one of PromotionalStrategy, RebateStrategy and ReduceStrategy that realize lStrategy interface.
Expand
Richter substitution principle and dependency inversion principle constitute the basis of interface-oriented programming, and it is precisely because of Richter substitution principle that programs are diversified.
The above is the java object-oriented design principles of the Richter Replacement Principle example details, more about the java object-oriented design of the Richter Replacement Principle of information please pay attention to other related articles on this site!