Design Mode Simple Factory Mode (Talk about Westward Journey Learning Notes)

1. Object-Oriented Programming Benefits

Object-oriented programming reduces program coupling by encapsulation, inheritance, and polymorphism.

Encapsulation: hides implementation details and attributes, controls access to attributes through a public interface provided externally, and prevents code and data of this class from being randomly accessed by code defined by the external class.Encapsulation allows you to modify your code without affecting the program that calls our code.

(2) Inheritance: based on an existing class, copy it, and then create a new class by adding and modifying this copy.

(3) Polymorphism: Polymorphism is also known as dynamic binding, late binding, or runtime binding; method binding is performed by assigning a subclass object reference to a parent class reference, and then by the type of object when the program is running.

 

2. Design Mode Benefits

Using design patterns makes programs more flexible, easy to modify, and easy to reuse.

Maintainability (easy to modify): Modify only what needs to be changed, without affecting the logic of other functions.

(2) Reuse: Use the properties or methods of an existing class by inheriting.

 

 

 

3. Simple Factory Mode

The factory class conditionally creates subclass objects that implement different functions and then returns a parent reference to the corresponding subclass object.Benefit: To add new functionality, you only need to create a new subclass that implements another functionality, and for the old code, you only need to modify the code of the abstract factory.

(1) Picture class contains all method declarations (similar functions)

(2) Each subclass corresponds to a specific method implementation (functional implementation)

(3) Factory classes are responsible for the creation of all subclass objects and assign references to parent objects, using polymorphism for late method body binding.

Class Diagram:

 

 

Simple factory class:

public class SimpleFactory {

    public static Operation createOperation(String operater) {
        Operation operation = null;
        switch (operater) {
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OpertionSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
        }
        return operation;
    }
}

Add, subtract, multiply and divide four subclasses:

//base class
public abstract class Operation {
    protected double operationA = 0.0;
    protected double operationB = 0.0;

    public double getOperationA() {
        return operationA;
    }

    public void setOperationA(double operationA) {
        this.operationA = operationA;
    }

    public double getOperationB() {
        return operationB;
    }

    public void setOperationB(double operationB) {
        this.operationB = operationB;
    }

    public abstract double getResult() throws Exception;
}







//Addition class
public class OperationAdd extends Operation {
    @Override
    public double getResult() {
        return operationA + operationB;
    }
}



//Subtraction class

public class OpertionSub extends Operation {
    @Override
    public double getResult() {
        return operationA - operationB;
    }
}





//multiplicative
public class OperationMul extends Operation {
    @Override
    public double getResult() {
        return operationA * operationB;
    }
}




//Division Class
public class OperationDiv extends Operation {
    @Override
    public double getResult() throws Exception {
        if (operationB == 0.0) {
            throw new Exception("The divisor cannot be zero!");
        }
        return operationA / operationB;
    }
}

Client Code:

public class Main {

    public static void main(String[] args) {

        Operation operation = SimpleFactory.createOperation("+");
        operation.setOperationA(100);
        operation.setOperationB(150);
        try {
            System.out.println(operation.getResult());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}

 


In calculator-like designs, any calculation subclass (e.g., square operation, redundancy operation, etc.) can be added flexibly by using simple factory mode, and the process of adding new functions has no effect on the original functions (no modification is required), only the factory class needs to be modified so that it can create new subclasses.Object.

 

 

Keywords: Programming calculator

Added by kmutz22 on Sat, 07 Sep 2019 05:13:52 +0300