Design principle one : single principle of responsibility
Definition: There should be only one reason for a class to cause it to change.
Advantages: Reduce the complexity of the class, improve the readability of the class, improve the maintainability of the system.
Example:
Super Human public class Superman {public void Maintains world peace () {//Superman maintains world Peace}} Extension: public class Animal {Publi c void Breathe (String animal) {System.out.println (animal + "breathes in Air"); The public void Breathe2 (String animal) {System.out.println (animal + "breathes in water"); } }
A condition that can violate a single responsibility principle: it requires simple enough logic to violate the principle of single responsibility at the code level .
The number of methods required in a class is small enough to violate the principle of single responsibility at the method level .
Design principles Two : the principle of substitution on the Richter scale (Liskov Substitution Principle, LSP)
Definition: subtypes must be able to replace their parent types.
This principle reflects the relationship between the parent class and the subclass, and is one of the most important and basic principles in object-oriented design.
Pros: Make code reuse easier
Old Carpenter class public class Oldcarpenter {public void Makefurniture () {System.out.println ("Make a chair!"); }}//Old Carpenter Apprentice class public class Youngfurniture () extends oldcarpenter{//Override parent class method @Override public void make Furniture () {super.makefurniture (); System.out.println ("Decorate the chair!"); } }
Initial Android design principles, Design patterns