Simple Factory Pattern

Question: How do you achieve the functions of calculator by Object-Oriented ?java

 

Analysis:app

1,The function of calculator consists of 4 basic operations:Plus, Substraction, Multify, Division. Generally speaking, we can put the 4 operations into one Java class, but this doesn't accord with the thinking of Object-Oriented in Java. Some reasons I will share with you as following.ui

Firstly, now we have 4 basic operations, if we need to add some extra operations, you could do nothing but updating the class you have finished, it's not reasonable.If we put the service code mixed with the business logictic layer code, it's not the thinking of Object-Oriented.this

 What's the Object-Oriented?spa

Firstly,in java programing, you have to know  the three basic typical characteristics:Encapsulation,Inheritance,Polymorphism.code

Encapsulation: we encapsulate the properties of one Object by 'private', in other classes, you can't reach access to this class directly.But if you want to gain access to these properies which you have defined the setter and getter methods, you can call on it by the methods.three

Inheritance : This characteristic means that  the subclass can  extend the non-private  properties and non-private methods. You can use the properties or methods directly which are in the superclass, and you can also overwrite the methods in the subclass.ip

Polymorphism:This characteristic is very important in java programming.Here has  three preconditions about the Polymorphism.ci

1: relationship between subclass and superclass. This means the subclass must extend the superclass.get

2: the superclass must overwrite the functions from the superclass.

3:object reference of superclass must appoint to the object of subclass.

 So, by the Inheritance, in this example ,we can define A class of 'Operation',which have two private properties :numberA and numberB,and a public general function of getting result by input parameters of numberA and numberB,this is a superclass. 

public class Operation {  

  public double numberA = 0.0;  

  public double numberB = 0.0; 

  public Operation(){     }  

  public Operation(double numberA,double numberB){   

        this.numberA = numberA;  

         this.numberB = numberB;

   } 

  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 double getResult() throws Exception{   

  double result = 0.0;     

 return result;

  }

}

 

Next, you can define some subclasses such as plus, substraction, multify and division and extend to the superclass respectively. Then in every class, you can overwirte the function of  'getResult' and give them specific function.If this class will be used to make the calculation of adding, you can achieve the function in it by using numberA and numberB, make them do the plus.Similarly,you can finish the functions of substraction,multify,division.

 Factory? Generally speaking, it's a class to Instantiate the object, according to the principle of Polymorphism, it can instantiate some different objects  which have ths functions of supeerclass. So in this class, we can define a static method with a parameter which stand for the different different operation.

 public class Factory {

 public static Operation createOperation(String operator) {   

       Operation oper = null;   

       switch (operator) {   

       case "+":    oper = new Add(); 

       break;   

       case "——":    

       oper = new Subtraction();   

       break;   

       case "*":

       oper = new Mul();   

       break;   

       case "/":   

       oper = new Div();   

       break;  

      }   

   return oper;

  }

}

 

Speaking of which, the simple factory have been almost built already.

相關文章
相關標籤/搜索