Understanding of Abstract Programming

Keywords: Programming

What is abstract oriented programming? I didn't pay attention to it for the first time. I don't think it's very important. But when I go to have a closer look, the function of abstract oriented programming is so powerful. I should sum it up again.

Abstract oriented programming, as the name implies, is the programming of a series of operations on abstract classes. That is to say, when designing an important class, it should not face the concrete class, but the abstract class. That is to say, the important data in the design class is the object of the abstract class, not the object declared by the concrete class.

A simple understanding is to define an abstract class and an abstract method, then define one or several subclasses of the abstract class, rewrite the abstract method in the abstract class in the subclass (the overridden method in the subclass is no longer the abstract method), then you can declare only one abstract class, and then use the transformation object to call the overridden method. In this way, one variable declared can cope with many different changes.  

Let's take a look at an application of an example: (calculating the volume of a cylinder)

abstract class and its subclasses:

public abstract class Geometry {
    public abstract double getArea();//It just constructs an abstract method without adding anything.
}

class Circle extends Geometry{
    double r;
    Circle(double r){//Constructor
        this.r = r;
    }
    public double getArea() {   / /Method override
        return r * r * 3.14;
    }
}
class Rectangle extends Geometry{
    double a,b;
    Rectangle(double a,double b){
        this.a = a;
        this.b = b;
    }
    public double getArea() {
        return a*b;
    }
}

The class that obtains the volume of the cylinder no longer depends on the specific class, but faces the Geometry class:

public class Pillar {

    Geometry bottom;//Declare a Geometry object (abstract object)
    double hight;
    Pillar(Geometry bottom,double h){//Constructor, get value
        this.bottom = bottom;
        this.hight = h;
    }
    
    public double getVolume(){//Return volume value
        if(bottom == null){
            System.out.println("There's no bottom, area can't be calculated");
            return 0;
        }
        return bottom.getArea()*hight;//The getArea() function called here is determined by the up transformation object to the bottom.
    }
}

Create the upper transformation object, determine its calling function, and output its value

public class Application {

    public static void main(String[] args) {
        Pillar pillar;
        Geometry bottom;
        
        bottom = null;
        pillar = new Pillar(bottom,5);
        System.out.println("Volume:" + pillar.getVolume());
        
        bottom = new Circle(2);//Upper transformation object
        pillar = new Pillar(bottom,1);
        System.out.println("Volume:" + pillar.getVolume());//The getVolume() in Circle is called.
        
        bottom = new Rectangle(5,2);//Upper transformation object
        pillar = new Pillar(bottom,1);
        System.out.println("Volume:" + pillar.getVolume());//The getVolume() in Rectangle is called.
    }
}

Abstract oriented programming aims to deal with the change of user's requirements, and separate the code that often needs to be changed in a class due to the change of requirements.

The core of abstract oriented programming is to let every possible change in the class be assigned to a subclass of the abstract class, so that the designer of the class can no longer care about the concrete implementation.

 

Posted by ShogunWarrior on Sun, 22 Dec 2019 08:28:21 -0800