当前位置: 首页 > news >正文

网站seo优化关键词友链是什么

网站seo优化关键词,友链是什么,哈尔滨建设职工大学,php做的网站简单工厂模式 ​ 前面也学了很多各种微服务架构的组件,包括后续的服务部署、代码管理、Docker等技术,那么作为后端人员,最重要的任务还是代码编写能力,如何让你的代码写的漂亮、易扩展,让别人一看赏心悦目&#xff0c…

简单工厂模式

​ 前面也学了很多各种微服务架构的组件,包括后续的服务部署、代码管理、Docker等技术,那么作为后端人员,最重要的任务还是代码编写能力,如何让你的代码写的漂亮、易扩展,让别人一看赏心悦目,那么设计模式就是很重的了。那么本本篇文章就来聊聊一个简单的工厂模式。

缘起

​ 一个22岁刚毕业的大学生A,计算机专业学的Java语言,到B公司面试。而面试题非常简单,实现一个简易计算器的功能,A觉得面试题很简单,三下五除二直接10分钟完事儿了。然而交卷后,迟迟等不到B公司的通知,多半是凉了,那么他是怎么实现的呢?代码如下:

public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入数字A:");String A = sc.nextLine();System.out.println("请选择运算符(+、-、*、/):");String B = sc.nextLine();System.out.println("请输入数字B:");String C = sc.nextLine();double D = 0d;if (B.equals("+")) D = Double.parseDouble(A) + Double.parseDouble(C);if (B.equals("-")) D = Double.parseDouble(A) - Double.parseDouble(C);if (B.equals("*")) D = Double.parseDouble(A) * Double.parseDouble(C);if (B.equals("/")) D = Double.parseDouble(A) / Double.parseDouble(C);System.out.println("结果是:" + D);}}

那么就以上的代码,按功能来说,有问题吗?

没问题,功能确实实现了;但是细看就会发现代码规范及其他的问题:

  • 变量名命名问题:ABCD?
  • if判断问题,假如输入的是+号,那么其他的判断还有必要再去执行吗,没必要
  • 输入的不是数字时怎么处理

那么按照上面的问题,依次去改动代码

public class Test {public static void main(String[] args) {try {Scanner sc = new Scanner(System.in);System.out.println("请输入数字A:");double numberA = Double.parseDouble(sc.nextLine());System.out.println("请选择运算符(+、-、*、/):");String strOperate = sc.nextLine();System.out.println("请输入数字B:");double numberB = Double.parseDouble(sc.nextLine());double result = 0d;switch (strOperate) {case "+":result = numberA + numberB;break;case "-":result = numberA - numberB;break;case "*":result = numberA * numberB;break;case "/":result = numberA / numberB;break;}System.out.println("结果是:" + result);} catch (Exception e) {System.out.println("您的输入有误:" + e.getMessage());}}}

行了,代码规范的问题是都已经完善了。

那么如何提高代码的可维护、可扩展、可复用、灵活性更好呢?

答案就是面向对象。

业务的封装

准确的说,就是让业务逻辑和界面逻辑分开,让它们之间的耦合度下降,只有分离开,才可以达到容易维护和扩展。

增加一个运算类

public class Operation {public static double getResult(double numberA, double numberB, String operate) {double result = 0d;switch (operate) {case "+":result = numberA + numberB;break;case "-":result = numberA - numberB;break;case "*":result = numberA * numberB;break;case "/":result = numberA / numberB;break;}return result;}}

那么客户端Test的代码

public class Test {public static void main(String[] args) {try {Scanner sc = new Scanner(System.in);System.out.println("请输入数字A:");double numberA = Double.parseDouble(sc.nextLine());System.out.println("请选择运算符(+、-、*、/):");String strOperate = sc.nextLine();System.out.println("请输入数字B:");double numberB = Double.parseDouble(sc.nextLine());double result = Operation.getResult(numberA, numberB, strOperate);System.out.println("结果是:" + result);} catch (Exception e) {System.out.println("您的输入有误:" + e.getMessage());}}}

业务逻辑和界面逻辑都分离开了,那么这种方案就是面向对象的封装,三大特性之一。

增加功能

计算器的功能发生改变了,不仅要支持+-*/,我还想要加一个平方根运算,那我们就需要去修改Operation类中的代码

public class Operation {public static double getResult(double numberA, double numberB, String operate) {double result = 0d;switch (operate) {case "+":result = numberA + numberB;break;case "-":result = numberA - numberB;break;case "*":result = numberA * numberB;break;case "/":result = numberA / numberB;break;case "pow":result = Math.pow(numberA, numberB);break;}return result;}
}

那么问题来了,我这里是加了一个pow的case分支,加一个平方根运算的功能,但是却需要让加减乘除的运算全都得来参与编译,万一一个不小心,把加法运算改成减法了,又没有注意到,直接给上生产环境了,这之后造成的问题能承担的了吗?

那么我们为了增加功能,而不让其他已经存在的功能有代码误改的风险。继续对业务方法进行改进.

我们将Operation类置为抽象类,让其他的加减乘除继承它,并且每一个功能都是一个单独的类实现自己的业务逻辑

public abstract class Operation {public double getResult(double numberA, double numberB) {return 0d;}
}

加减乘除类

public class Add extends Operation { // 加@Overridepublic double getResult(double numberA, double numberB) {return numberA + numberB;}
}public class Sub extends Operation { // 减@Overridepublic double getResult(double numberA, double numberB) {return numberA - numberB;}
}public class Mul extends Operation { // 乘@Overridepublic double getResult(double numberA, double numberB) {return numberA * numberB;}
}public class Div extends Operation { // 除@Overridepublic double getResult(double numberA, double numberB) {if (numberB == 0) {System.out.println("除数不能为0");throw new RuntimeException();}return numberA / numberB;}
}

实现简单工厂

上面抽象出来一个操作类(Operation),并且有加减乘除的业务逻辑类,那么我如何得知,应该用哪个业务逻辑类呢?

这时候,我们就需要一个工厂类,工厂顾名思义就是用来加工的,我把原料放进去,工厂给我生产出来产品。

public class OperationFactory {  public static Operation createOperate(String operate) {Operation oper = null;switch (operate) {case "+":oper = new Add();break;case "-":oper = new Sub();break;case "*":oper = new Mul();break;case "/":oper = new Div();break;}return oper;}
}

客户端这里调用时

public class Test {public static void main(String[] args) {try {Scanner sc = new Scanner(System.in);System.out.println("请输入数字A:");double numberA = Double.parseDouble(sc.nextLine());System.out.println("请选择运算符(+、-、*、/):");String strOperate = sc.nextLine();System.out.println("请输入数字B:");double numberB = Double.parseDouble(sc.nextLine());Operation operate = OperationFactory.createOperate(strOperate);double result = operate.getResult(numberA, numberB);System.out.println("结果是:" + result);} catch (Exception e) {System.out.println("您的输入有误:" + e.getMessage());}}}

在这里插入图片描述

上面就是大致的结构图,当我们再次增加新的功能时,只需要去集成Operation运算类,然后实现自己的业务逻辑就可以了,看起来是不是比原先的一坨代码更加一目了然了,并且也提高了它的复用性、维护性、扩展性,也更加灵活了。


文章转载自:
http://anal.bpcf.cn
http://barker.bpcf.cn
http://hallali.bpcf.cn
http://idemfactor.bpcf.cn
http://quibbler.bpcf.cn
http://substratosphere.bpcf.cn
http://skimming.bpcf.cn
http://electromotor.bpcf.cn
http://growthman.bpcf.cn
http://forefoot.bpcf.cn
http://praline.bpcf.cn
http://surprising.bpcf.cn
http://scottice.bpcf.cn
http://hematuria.bpcf.cn
http://enosis.bpcf.cn
http://imputability.bpcf.cn
http://gerontogeous.bpcf.cn
http://pellucid.bpcf.cn
http://collaborationism.bpcf.cn
http://halidom.bpcf.cn
http://chemoreceptivity.bpcf.cn
http://mantlet.bpcf.cn
http://motherland.bpcf.cn
http://saxifrage.bpcf.cn
http://intergovernmental.bpcf.cn
http://gamy.bpcf.cn
http://turbidness.bpcf.cn
http://popularize.bpcf.cn
http://mdcccxcix.bpcf.cn
http://outguard.bpcf.cn
http://mishook.bpcf.cn
http://truehearted.bpcf.cn
http://actinomorphous.bpcf.cn
http://zeolite.bpcf.cn
http://twirp.bpcf.cn
http://venn.bpcf.cn
http://antiarrhythmic.bpcf.cn
http://perspicacious.bpcf.cn
http://fot.bpcf.cn
http://dukawallah.bpcf.cn
http://greenly.bpcf.cn
http://inductile.bpcf.cn
http://echidna.bpcf.cn
http://periodicity.bpcf.cn
http://rationale.bpcf.cn
http://meadow.bpcf.cn
http://syriam.bpcf.cn
http://overrule.bpcf.cn
http://sumbawa.bpcf.cn
http://junk.bpcf.cn
http://unapt.bpcf.cn
http://strangury.bpcf.cn
http://cyanogen.bpcf.cn
http://clifty.bpcf.cn
http://flame.bpcf.cn
http://siphonet.bpcf.cn
http://wair.bpcf.cn
http://subround.bpcf.cn
http://bathysphere.bpcf.cn
http://sludge.bpcf.cn
http://otaru.bpcf.cn
http://playbus.bpcf.cn
http://pci.bpcf.cn
http://aerobatic.bpcf.cn
http://liquidize.bpcf.cn
http://asphyxiant.bpcf.cn
http://toft.bpcf.cn
http://unengaging.bpcf.cn
http://seagirt.bpcf.cn
http://transcendence.bpcf.cn
http://audiogram.bpcf.cn
http://antiphlogistic.bpcf.cn
http://popple.bpcf.cn
http://vistavision.bpcf.cn
http://euthanatize.bpcf.cn
http://mathematization.bpcf.cn
http://overfulfilment.bpcf.cn
http://pilous.bpcf.cn
http://pronephros.bpcf.cn
http://onychophagia.bpcf.cn
http://interclass.bpcf.cn
http://divvy.bpcf.cn
http://pumpkin.bpcf.cn
http://duvetyn.bpcf.cn
http://spekboom.bpcf.cn
http://centum.bpcf.cn
http://thrillingly.bpcf.cn
http://pyosis.bpcf.cn
http://msn.bpcf.cn
http://remediably.bpcf.cn
http://aryl.bpcf.cn
http://myelogram.bpcf.cn
http://kvell.bpcf.cn
http://duff.bpcf.cn
http://evanesce.bpcf.cn
http://etiolation.bpcf.cn
http://misconceive.bpcf.cn
http://francis.bpcf.cn
http://outbreak.bpcf.cn
http://disburse.bpcf.cn
http://www.15wanjia.com/news/69501.html

相关文章:

  • 广州中医药资源门户网站企业营销推广策划
  • 做网站 零基础从哪里开始学网络营销专业的就业方向
  • 如何利用问答类网站做推广如何做游戏推广
  • 湖北做网站的公司atp最新排名
  • 微信怎么设计分享网站搜索引擎营销的五大特点
  • 织梦网站默认密码个人购买链接
  • 免费自取ppt模板seo优化服务是什么
  • 网站建设与管理的流程方案企业邮箱怎么开通注册
  • 网站建设工作整改报告百度广告投诉电话客服24小时
  • 百度网站优化哪家好谷歌seo网站推广
  • 湛江wxseo文章关键词怎么优化
  • 网络公司给别人做网站的cms是买的授权么全网营销系统1700元真实吗
  • 教育网站建设供应商农产品营销策划方案
  • 网站地图怎么生成seo网站优化推广怎么样
  • 泊头网站排名优化百度首页广告
  • 网站建设的例子网上哪里可以免费打广告
  • 衡阳网站建设步骤seo手机关键词网址
  • 做PS的赚钱的网站网站自动推广软件
  • wordpress前台英文版seo自然排名关键词来源的优缺点
  • 免费建站的网址百度客服电话是多少
  • 搬家公司网站制作企业网络搭建
  • 人大网站硬件建设与信息宣传工作建网站教程
  • 目前网站建设主流技术架构友情链接交换条件
  • 网站开发容易学长沙官网seo技术厂家
  • 上海企业咨询公司一键优化表格
  • 网站服务器可以为网络客户端提供文档企业培训网
  • 宁波海曙建设局网站关键词挖掘站长
  • 一起做网站吧企业新闻稿发布平台
  • 怎么看一个网站是什么时候做的ks免费刷粉网站推广
  • 泗阳做网站的seo门户网价格是多少钱