Dahua design pattern - Reading Notes - technology - updating

Posted by spillage on Fri, 24 Sep 2021 02:50:31 +0200

Clue barNote bar
Write a calculator programSee Appendix 1
Why is movable type printing called one of the four great inventions in ancient China?As shown in Figure 1-1
1. Words can be directly replaced and maintainable.
2. The completed words can be reused.
3. Words can be expanded freely
4. The characters can be arranged horizontally and vertically at will, which is flexible
What is the difference between replication and reuse?Copy paste and copy the existing code to other places, and then modify it to apply to the new situation.
Reuse is to extract the existing code into a public function or public object, so that it can be called in other places without writing code repeatedly. [for example, we often use tool classes, such as FileUtil, to extract frequent complex operations that require File into functions, direct external calls, etc.]
What is encapsulation?Encapsulation, generally speaking, is to show only the necessary things to outsiders, and hide the unnecessary things without showing them.

[for example, the plug-in of your home only shows you a double or three plug-in. You can plug it directly into the plug-in if you want to charge. You can use it without knowing how the plug-in is powered on.
Another example is the human body. You don't need to know how the blood flows, how the heart pumps blood, and how the nutrients in the body are digested and absorbed. Ordinary people can live. The human body hides a lot of things for you. It tells you that you only need to eat, sleep and exercise, and the body is generally no problem.]
How to encapsulate businessTry to only provide necessary data or methods to the outside, and hide them unnecessarily. And it's best to divide them according to their responsibilities.
[for example, your body doesn't tell you that it has kidneys, lungs and heart, which are hidden. Moreover, according to the division of responsibilities, it is the mouth that eats outside, the eyes that look at things, and it is also encapsulated inside. The lungs are responsible for breathing, and the kidneys are responsible for filtering, etc.]
What is coupling?Coupling refers to the degree of connection between things, which can also be called the degree of closeness. [for example, three things are seriously coupled, which means that the three things are closely related. If one goes wrong, it is likely to affect the other two.
Tight coupling means that everyone is connected very closely. You can imagine that Xiao Ming has a close relationship with his family. If one person gets sick, others will worry
Loose coupling means that people are not connected very closely. For example, if you are ill, it will not particularly affect your boss -# -, which will pierce your heart]
Advantages and disadvantages of tight coupling and loose couplingTight coupling:
Advantages: we are closely linked. One change will affect others, which is convenient for unified modification.
Disadvantages: it is closely related. One modification will affect others and cause unnecessary impact.
Loose coupling:
Advantages: we have loose contact. One change will not affect others. It is convenient to add, delete and modify any member.
Disadvantages: we are not closely connected. If you want to make unified modifications, you need to find and modify them all, which is troublesome
Simple factory pattern and code implementationThe advantage of a simple factory is that an intermediate layer is added between the upstream and downstream. The changes mainly focus on the factory and various sub parts, which remain unchanged externally.
See Appendix 3 for codes
[we will introduce the middle tier and its advantages and disadvantages in Appendix 2.]
UML diagram

Figure 1

appendix

  1. For the first time, I didn't read the code:

    public class Calculate {
        SignCalculateFactory signCalculateFactory;
    
        public int calculateInt(int a, int b, char sign) {
            return signCalculateFactory.getSignCalculateBySign(sign)
                                        .calculateInt(a, b);
        }
    
        public double calculateDouble(double a, double b, char sign) {
            return signCalculateFactory.getSignCalculateBySign(sign)
                                        .calculateDouble(a, b);
        }
    }
    
    class SignCalculateFactory {
        private AddCalculate addCalculate;
        private ReduceCalculate reduceCalculate;
        private MultiplyCalculate multiplyCalculate;
        private DivCalculate divCalculate;
        private EmptyCalculate emptyCalculate;
    
        public SignCalculate getSignCalculateBySign(char sign) {
            switch (sign) {
                case '+' : return addCalculate;
                case '-' : return reduceCalculate;
                case '*' : return multiplyCalculate;
                case '/' : return divCalculate;
                default: return emptyCalculate;
            }
        }
    }
    
    public interface SignCalculate {
        int calculateInt(int a, int b);
    
        double calculateIntToDouble(int a, int b);
    
        double calculateDouble(double a, double b);
    }
    
    class EmptyCalculate implements SignCalculate {
        public int calculateInt(int a, int b) {
            throw new RuntimeException("emptyCalculate can not be use to calculate");
        }
    
        public double calculateIntToDouble(int a, int b) {
            throw new RuntimeException("emptyCalculate can not be use to calculate");
        }
    
        public double calculateDouble(double a, double b) {
            throw new RuntimeException("emptyCalculate can not be use to calculate");
        }
    }
    
    class AddCalculate implements SignCalculate {
        public int calculateInt(int a, int b) {
            return a + b;
        }
    
        public double calculateIntToDouble(int a, int b) {
            return a * 1.0 + b;
        }
    
        public double calculateDouble(double a, double b) {
            return a + b;
        }
    }
    
    class ReduceCalculate implements SignCalculate {
        public int calculateInt(int a, int b) {
            return a - b;
        }
    
        public double calculateIntToDouble(int a, int b) {
            return a * 1.0 - b;
        }
    
        public double calculateDouble(double a, double b) {
            return a - b;
        }
    }
    
    class MultiplyCalculate implements SignCalculate {
        public int calculateInt(int a, int b) {
            return a * b;
        }
    
        public double calculateIntToDouble(int a, int b) {
            return a * 1.0 * b;
        }
    
        public double calculateDouble(double a, double b) {
            return a * b;
        }
    }
    
    class DivCalculate implements SignCalculate {
        public int calculateInt(int a, int b) {
            return a / b;
        }
    
        double calculateIntToDouble(int a, int b) {
            return a * 1.0 / b;
        }
    
        public double calculateDouble(double a, double b) {
            return a / b;
        }
    }
    
  2. Middle layer

    The middle tier is very useful. Let's take an example. For example, if you want to buy clothes overseas, you find an agent who helps many people buy clothes. Let's assume that there are three people in ABC who want to buy clothes of three brands: 123. At this time, if there is a new brand of clothes called 4 and everyone likes it very much, we just need to ask the agent to find clothes of brand 4, and then ABC can directly find the agent to get them.

    Similarly, if you add a customer named D, you just need to find an agent and get clothes from there.

    Among them, only agents need to be modified. We don't need customers ABCD to go to overseas brand stores to buy things. We just need customers to find agents.

    Here, the same thing remains: agents provide services, and agents only sell clothes.
    The change is: the agent looks for new brand clothes, and the agent receives new customers.

Topics: Java Design Pattern