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

Analysis:

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.

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.

What's the Object-Oriented?

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

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.

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.

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

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

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.

Simple Factory Pattern的更多相关文章

  1. Net设计模式实例之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...

  2. 【设计模式】简单工厂模式 Simple Factory Pattern

    简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...

  3. Golang设计模式—简单工厂模式(Simple Factory Pattern)

    Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...

  4. 大白话简单工厂模式 (Simple Factory Pattern)

    大白话简单工厂模式 (Simple Factory Pattern) 从买车经历说起 毕业两年,码农张小两口无法忍受挤公交,凌晨起床抢火车票的痛苦,遂计划买车.逛了多家4S店,最终定下日产某车型的轿车 ...

  5. [转]C#设计模式(4)-Simple Factory Pattern

    工厂模式专门负责将大量有共同接口的类实例化.工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类.工厂模式有以下几种形态: 简单工厂(Simple Factory)模式 工厂方法(F ...

  6. 六个创建模式之简单工厂模式(Simple Factory Pattern)

    定义: 定义一个工厂类,它可以根据参数的不同生成对应的类的实例:被创建的类的实例通常有相同的父类.因为该工厂方法尝尝是静态的,所以又被称为静态工厂方法(Static Factory Method) 结 ...

  7. 简单工厂模式(Simple Factory Pattern)

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂 ...

  8. 【java设计模式】【创建模式Creational Pattern】简单工厂模式Simple Factory Pattern(静态工厂方法模式Static Factory Method Pattern)

    public class Test { public static void main(String[] args){ try{ Factory.factory("A").doSt ...

  9. 简单工厂模式(Simple Factory Pattern)

    简单工厂模式概述 定义:定义一个工厂类,他可以根据参数的不同返回不同类的实例,被创建的实例通常都具有共同的父类 在简单工厂模式中用于被创建实例的方法通常为静态(static)方法,因此简单工厂模式又被 ...

随机推荐

  1. [POJ1050]To the Max (矩阵,最大连续子序列和)

    数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...

  2. FC105 FC106 Scale功能块使用说明

    有一点不明白,这个跟传感器本身的分辨率什么关系? 为什么定死 极性和非极性的值是7648 和27648 FC105是处理模拟量(1~5V.4~20MA等信号)输入的功能块: 其中管脚的定义如下:IN- ...

  3. ECMA Script 6_必须要知道的基础

    ES6 为了保持兼容性,var 命令和 function 命令声明的全局变量,依旧是顶层对象的属性: 另一方面规定,let 命令.const 命令.class 命令声明的全局变量,不属于 window ...

  4. requirejs的使用和快速理解

    样例来自https://www.jianshu.com/p/b8a6824c8e07 requirejs有以下功能 声明不同js文件之间的依赖 可以按需.并行.延时载入js库 可以让我们的代码以模块化 ...

  5. 九、JSP入门(2)

    day12 JSP指令 1 JSP指令概述 JSP指令的格式:<%@指令名 attr1=”” attr2=”” %>,一般都会把JSP指令放到JSP文件的最上方,但这不是必须的. JSP中 ...

  6. WcPro项目(WordCount优化)

    1 基本任务:代码编写+单元测试 1.1 项目GitHub地址 https://github.com/ReWr1te/WcPro 1.2 项目PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实 ...

  7. dtFindNearestPolyQuery :: process

    dtFindNearestPolyQuery :: process(const dtMeshTile* tile, dtPoly** polys, dtPolyRef* refs, int count ...

  8. ubuntu安装elasticsearch

    0x00安装jdk ElasticSearch需要安装jdk1.8以上版本的支持,所以需要先安装jdk.linux下如何安装可以查看另一篇博客 0x01 下载elasticsearch 在es官网下载 ...

  9. php 5.6,7.0静态方法调用和new调用方法性能差距

    windows7 64 机器I5 8G内存,128G SSD 吐槽一下,win10内存的消耗,真的可怕 测试代码 class staticTest { public function test() { ...

  10. centos7安装nodejs运行环境及卸载

    一.安装1.进入官网下载最新版本https://nodejs.org/en/ 选择下载后上传或直接使用wget下载 wget https://nodejs.org/dist/v8.11.2/node- ...