Interpreting six principles of SOLID with code

Six principles

Single responsibility principle

definition

definition:

Ensure that the singleton class has only one instance, and the singleton class provides a function interface for other classes to obtain the unique instance.

Explanation: a class is only responsible for one responsibility. There should be no more than one reason for class change.

Code interpretation

For example, a class records the names of some foods, but also records the practices of foods. The former belongs to business objects and the latter belongs to business logic. According to the principle of single responsibility, we need to separate business and data

Failure to follow the single principle

public class Foods {
    private String fish;
    private String meat;

    public String getFish() {
        return fish;
    }

    public void setFish(String fish) {
        this.fish = fish;
    }

    public String getMeat() {
        return meat;
    }

    public void setMeat(String meat) {
        this.meat = meat;
    }
    public void RedCookedFish(){
        //do something...
    }
    public void RedCookedMeat(){
        //do something...
    }
}

Adhere to the single principle

public class Foods {
    private String fish;
    private String meat;

    public String getFish() {
        return fish;
    }

    public void setFish(String fish) {
        this.fish = fish;
    }

    public String getMeat() {
        return meat;
    }

    public void setMeat(String meat) {
        this.meat = meat;
    }
}
public class Practices {
    public void RedCookedFish(){
    //do something...
    }
    public void RedCookedMeat(){
    //do something...
    }
}

Opening and closing principle

definition

Definition: a software entity such as class, module and function should be open to extension and closed to modification.

When a system has new needs, we don't want to modify the original function class, resulting in the destruction of the original logic and the emergence of new bugs. When designing the system, we try to realize new functions by expanding. In short, let's make good use of inheritance and interfaces.

Code interpretation

For example, an animal has two attributes: name and movement mode. When it grows up, it needs to make friends with the opposite sex and increase the function of reproduction. We expand new functions through inheritance or interface and abide by the opening and closing principle.

public class Animal {
    private String mName;
    private String mMovementMode;
    public Animal(String mName,String mMovementMode){
        this.mName = mName;
        this.mMovementMode = mMovementMode;
    }

    public String getmName() {
        return mName;
    }

    public String getmMovementMode() {
        return mMovementMode;
    }
}
public class Multiply extends Animal {
    public Multiply(String mName, String mMovementMode) {
        super( mName, mMovementMode );
    }
    public void MultiplyMethod(){
    //do something...
    }
}

Richter substitution principle

definition

Definition 1: if each type is T1 Object of o1,Are of type T2 Object of o2,Make with T1 All programs defined
P In all objects o1 Replace them with o2 When, program P The behavior has not changed, then the type T2 Is type T1 Subclass of
 Type.
Definition 2: all references to the base class must be able to use the objects of its subclasses transparently

Explanation: for example, an abstract method of an abstract class. Subclasses must be implemented

Code interpretation

For example, an animal class has an attribute of movement mode. Other specific animals, such as fish and eagle, need to inherit their methods to realize their own movement mode.

public abstract class Animal {
    public abstract void MultiplyMethod(String Multiply);
}
public class Fish extends Animal {
    @Override
    public void MultiplyMethod(String Multiply) {
        // set Multiply Method
    }
}

Dependency Inversion Principle

definition

Definition: high-level modules should not rely on low-level modules, and both should rely on their abstraction; Abstractions should not rely on details; Details should rely on abstraction.

The core idea of dependency inversion principle is interface oriented programming.

Code interpretation

For example, the popular programming languages at this stage, such as java,c,python, etc., but with the passage of time and the development of science and technology, new languages will be produced according to different needs. If we need to add one by one in a class, it will be too cumbersome. It will be much simpler to use the interface for configuration.

Failure to comply with the principle of dependency

class C{
    public String get(){
        return "C";
    }
}
class Java{
    public String get(){
        return "Java";
    }
}
class Python{
    public String get(){
        return "Python";
    }
}
class GetLanguage{
    public GetLanguage(){

    }
    public void getLanguage(C c){
        Log.d( "Language",c.get() );
    }
    public void getLanguage(Java java){
        Log.d( "Language",java.get() );
    }
}
 GetLanguage language = new GetLanguage();
        language.getLanguage( new C() );
        language.getLanguage( new Java() );

Adherence to the dependency lead principle

Define interface

public interface ILanguage {
    String get();
}
public class Language {
   public void getLanguage(ILanguage iLanguage){
       Log.d( "Language",iLanguage.get() );
   }
}
 Language language = new Language();
        language.getLanguage( new ILanguage() {
            @Override
            public String get() {
                return "C";
            }
        } );

Interface isolation principle

definition

Definition: the client should not rely on interfaces it does not need; The dependence of one class on another should be based on the smallest interface.

In short, a class only needs to implement its own methods, and irrelevant methods do not need to be implemented

Code interpretation

For example, an interface realizes all movement modes of animals, such as running, climbing, swimming, flying and jumping. The eagle only needs to fly and the dog only needs to run. However, if this interface is implemented, all methods must be implemented, which will be very cumbersome, and some methods do not need to be implemented. We only need to split them into four interfaces, different animals and different movement modes.

Failure to observe interface isolation principle

public interface IAnimal {
    void run();
    void swim();
    void climb();
    void fly();
}
class Dog implements IAnimal{

    @Override
    public void run() {

    }

    @Override
    public void swim() {

    }

    @Override
    public void climb() {

    }

    @Override
    public void fly() {

    }
}

Observe the principle of interface isolation

public interface  Swim{
        void swim();
    }
    public interface  Run{
        void run();
    }
    public interface  Climb{
        void climb();
    }
    public interface  Fly{
        void fly();
    }
class Dog implements Run{

        @Override
        public void run() {

        }
    }

Dimitri principle

definition

Definition: an object should have the least understanding of other objects.

Code interpretation

Suppose a class implements the four methods of addition, subtraction, multiplication and division, and judges the type of the numerical value in the addition, subtraction, multiplication and division method. Range judgment is implemented in type judgment. We only need to expose the four methods of addition, subtraction, multiplication and division, and the other methods do not need to be exposed.

public class Operation {
    public Object Add(Object num1,Object num2){
       Object flag = JudgeType(num1,num2);
       int num = (Integer) flag;
       switch (num){
           case 0:
               return (Integer)num1 + (Integer)num2;
           case 1:
               return (Double)num1 + (Double) num2;
            default:
                return null;
       }
    }
    private void Sub(Object num1,Object num2){

    }
    private void Ride(Object num1,Object num2){

    }
    private void Division(Object num1,Object num2){

    }
    private Object JudgeType(Object num1,Object num2){
        if (num1 instanceof Integer){
          return 0;
        }else if (num1 instanceof Double){
            return 1;
        }
        return 3;
    }
//    private boolean JudgeIntRange(int num){
//        if (num < 65535 && num > -65535){
//            intFlag = true;
//            return true;
//        }
//        intFlag = false;
//        return false;
//    }
}
Operation operation = new Operation();
Log.d( "Result=",operation.Add( 1,1 )+"" );
2021-10-27 21:27:32.893 25595-25595/com.franzliszt.solid D/Result=: 2

Tags: Android Design Pattern

Posted on Wed, 27 Oct 2021 09:45:43 -0400 by ryansmith44