Understand object-oriented - polymorphism

Introduction

What is polymorphism? Let's first take a look at the answer given by Baidu Encyclopedia - In programming languages ​​and type theory, polymorphism (English: polymorphism) refers to providing a unified interface for entities of different data types. A polymorphic type can apply its supported operations to values ​​of other types. The polymorphism I understand is actually just one sentence - the implementation of the subclass points to a reference to the parent class . Let's learn about polymorphism in object-oriented through examples.

simple factory

What is a simple factory?

We leave the process of instantiating subclasses to a separate class to create instances. This separate class is what we call a factory. All the process of instantiating word classes is done by the factory, not on the client.

Construct operator class

Now we have a operator class, which has two subclasses, namely the addition class and the subtraction class. The following is their current class diagram: The following is the
Insert image description here
specific code implementation:
Operation class:

public class Operation{
    
    
	private double numberA = 0;
	private double numberB = 0;
	
	public double getNumberA() {
    
    
        return numberA;
    }

    public void setNumberA(double numberA) {
    
    
        this.numberA = numberA;
    }

    public double getNumberB() {
    
    
        return numberB;
    }

    public void setNumberB(double numberB) {
    
    
        this.numberB = numberB;
    }
    
    // 获取结构的抽象方法
    public abstract double GetResult();
}

Addition class:

public class OperationAdd extends Operation{
    
    
    @Override
    public double GetResult() {
    
    
        double result = 0;
        result = super.getNumberA() + super.getNumberB();
        return result;
    }
}

Subtraction class:

public class OperationSub extends Operation{
    
    
    @Override
    public double GetResult() {
    
    
        double result = 0;
        result = getNumberA() - getNumberB();
        return result;
    }
}

Construct a simple factory class

We add a simple factory on this basis, and use the simple factory to create subclasses. The class diagram changes to the following figure:
Insert image description here
Simple factory code:

public class OperationFactory {
    
    
    public static Operation createOperate(String operate) {
    
    
        Operation opera = null;
        switch (operate) {
    
    
            case "+":
                opera = new OperationAdd();
                break;
            case "-":
                opera = new OperationSub();
                break;
        }
        return opera;
    }
}

Client implementation

public class Client {
    
    
    public static void main(String[] args) {
    
    
        Operation opera;
        opera = OperationFactory.createOperate("+");
        opera.setNumberA(1);
        opera.setNumberB(2);
        double result = opera.GetResult();
        System.out.println(result);
    }
}

analyze

Why should we introduce simple factories when explaining polymorphism? First, the implementation of the simple factory well reflects the object-oriented polymorphic characteristics. We can see lines 3, 6, and 9 of the simple factory code implementation part above. This is a good example of the fact that the implementation of the subclass mentioned in the introduction points to the reference of the parent class,
Insert image description here
and the simple factory used here also reflects one of the benefits of polymorphism - scalability . If I want to add a multiplication class at this time, then I only need to add a separate multiplication class, and add or subtract an "x" symbol in the switch case to realize the expansion of multiplication, but this solution It still violates the opening and closing principle. Of course, there are other solutions to deal with this problem. You can use the factory method to solve it. I won't go into details here. I mainly want to discuss the polymorphic characteristics of object- oriented .

Guess you like

Origin blog.csdn.net/zwb568/article/details/125004777