当前位置:网站首页>Simple factory mode
Simple factory mode
2022-06-28 04:13:00 【The roaring Conan】
Simple factory model
Introducing problems
Using any object-oriented language to implement a calculator console program , Two numbers and operation symbols are required , Get the results .
【 Common code 】

Although such a program solves the problem , But not easy to maintain , It's not easy to expand , It's not easy to reuse . So it can't meet the requirement of high quality code .
explain : The picture comes from the big talk design pattern , For learning purposes only .
【 Factory mode code 】
The title mentions the use of object orientation , Then the three characteristics of object-oriented are encapsulation 、 Inheritance and polymorphism .
We can try to encapsulate the computing class , And divide different operations to write , Be decoupled , And use the factory model .
The structure is as follows :

【Java】
Operation class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: Operation * @Description: Operation parent class * @author: Cowherd Conan * @date: 2022-06-22 19:10 */
public class Operation {
private double numA;
private double numB;
public double getNumA() {
return numA;
}
public void setNumA(double numA) {
this.numA = numA;
}
public double getNumB() {
return numB;
}
public void setNumB(double numB) {
this.numB = numB;
}
public double getResult() throws Exception {
double res = 0;
return res;
}
}
Add class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: OperationAdd * @Description: Add operation * @author: Cowherd Conan * @date: 2022-06-22 19:13 */
public class OperationAdd extends Operation{
@Override
public double getResult() {
return getNumA() + getNumB();
}
}
Subtraction class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: OperationSub * @Description: Subtraction operation * @author: Cowherd Conan * @date: 2022-06-22 19:18 */
public class OperationSub extends Operation{
@Override
public double getResult() {
return getNumA() - getNumB();
}
}
Multiplication class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: OperationMul * @Description: Multiplication * @author: Cowherd Conan * @date: 2022-06-22 19:19 */
public class OperationMul extends Operation{
@Override
public double getResult() {
return getNumA() * getNumB();
}
}
Divide the class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: OperationDiv * @Description: In addition to the operation * @author: Cowherd Conan * @date: 2022-06-22 19:20 */
public class OperationDiv extends Operation{
@Override
public double getResult() throws ArithmeticException {
if(getNumB()==0){
throw new ArithmeticException(" The divisor cannot be zero 0!");
}
return getNumA() / getNumB();
}
}
Simple factory class
package keafmd.accumulate.designmode.factorymode;
/** * Keafmd * * @ClassName: OperationFactory * @Description: Computing factory * @author: Cowherd Conan * @date: 2022-06-22 19:23 */
public class OperationFactory {
public static Operation createOperation(String operate) throws Exception {
Operation operation = null;
switch (operate){
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
default:
throw new Exception(" Illegal input operator !");
}
return operation;
}
}
client
package keafmd.accumulate.designmode.factorymode;
import java.util.Scanner;
/** * Keafmd * * @ClassName: ComputingClient * @Description: Computing client * @author: Cowherd Conan * @date: 2022-06-22 19:29 */
public class ComputingClient {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter two numbers :");
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.println(" Please enter operator (+,-,*,/):");
String s = sc.next();
Operation operation = OperationFactory.createOperation(s);
operation.setNumA(a);
operation.setNumB(b);
System.out.println(" The result of operation is :" + operation.getResult());
}
}
effect
Please enter two numbers :
2
3
Please enter operator (+,-,*,/):
*
The result of operation is :6.0
Process finished with exit code 0
That's all about the simple factory model
Copyright notice :
Original Blogger : Cowherd Conan
Blogger original link :https://keafmd.blog.csdn.net/
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on the like support below !
[ ha-ha ][ Huai Quan ]
come on. !
Joint efforts !
Keafmd
边栏推荐
- 窗帘做EN 1101易燃性测试过程是怎么样的?
- 从零到一,教你搭建「以文搜图」搜索服务(一)
- Door level modeling - learning notes
- Detailed explanation of iptables firewall rules and firewalld firewall rules
- applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
- 03 summary of various additions, updates and deletions of mongodb documents
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- 抖音实战~关注博主
- La norme européenne en 597 - 1 pour les meubles est - elle la même que les deux normes en 597 - 2 pour les ignifuges?
- Reverse a stack with recursive functions and stack operations only
猜你喜欢

RT-Thread 双向链表(学习笔记)

Secouer le son et se battre ~ prêter attention au blogueur

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

Tiktok practice ~ pay attention to bloggers

公司领导说,个人代码超10个Bug就开除,是什么体验?

多项目设计开发·类库项目引入入门

歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?

Pinda general permission system (day 5~day 6)

Lingge leangoo agile Kanban tool adds the functions of exporting card documents and pasting shared brain map nodes

揭开SSL的神秘面纱,了解如何用SSL保护数据
随机推荐
11_刻意练习精讲
Chapter 1 Introduction to bash
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
GCD maximum common divisor
Secouer le son et se battre ~ prêter attention au blogueur
软件测试报告怎么编写?第三方性能报告范文模板来了
《性能之巅第2版》阅读笔记(二)--CPU监测
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
云厂商为什么都在冲这个KPI?
MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
视频爆炸时代,谁在支撑视频生态网高速运行?
第一章 Bash 入门
Building log analysis system with elk (II) -- deployment and installation
设计一个有getMin功能的栈
Unity C # e-learning (11) -- custom protocol generation tool
MSc 307 (88) (2010 FTPC code) Part 9 bedding test
关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
Several important physical concepts
02 MongoDB数据类型、重要概念以及shell常用指令
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?