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

上海市工程质量建设管理协会网站厦门软件园网站开发

上海市工程质量建设管理协会网站,厦门软件园网站开发,网站开发网页gif设计公司,工业设计网站象目录 1 背景1.1 题目描述1.2 输入描述1.3 输出描述1.4 输入示例1.5 输出示例 2 简单工厂模式3 工厂方法模式4 思考4.1 改进工厂方法模式 1 背景 题目源自#xff1a;【设计模式专题之工厂方法模式】2.积木工厂 1.1 题目描述 小明家有两个工厂#xff0c;一个用于生产圆形积木… 目录 1 背景1.1 题目描述1.2 输入描述1.3 输出描述1.4 输入示例1.5 输出示例 2 简单工厂模式3 工厂方法模式4 思考4.1 改进工厂方法模式 1 背景 题目源自【设计模式专题之工厂方法模式】2.积木工厂 1.1 题目描述 小明家有两个工厂一个用于生产圆形积木一个用于生产方形积木请你帮他设计一个积木工厂系统记录积木生产的信息。 1.2 输入描述 输入的第一行是一个整数 N1 ≤ N ≤ 100表示生产的次数。 接下来的 N 行每行输入一个字符串和一个整数字符串表示积木的类型。积木类型分为 “Circle” 和 “Square” 两种。整数表示该积木生产的数量 1.3 输出描述 对于每个积木输出一行字符串表示该积木的信息。 1.4 输入示例 3 Circle 1 Square 2 Circle 1 1.5 输出示例 Circle Block Square Block Square Block Circle Block 2 简单工厂模式 一个工厂生产多个对象。 1抽象对象【通过接口进行抽象】2具体对象【通过类实现接口】3具体工厂 代码示例 public class Main {public static void main(String[] args) {ShapeFactorySystem shapeFactorySystem new ShapeFactorySystem(new SimpleShapeFactory());Scanner scanner new Scanner(System.in);int count Integer.parseInt(scanner.nextLine());for (int i 0; i count; i) {String line scanner.nextLine();String[] parts line.split( );String type parts[0];shapeFactorySystem.produce(type, Integer.parseInt(parts[1]));}} }interface Shape {void draw(int n); }class Circle implements Shape {public void draw(int n) {for (int i 0; i n; i) {System.out.println(Circle Block);}} }class Square implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(Square Block);}} }class SimpleShapeFactory {public Shape createShape(String type) {if (Circle.equals(type)) {return new Circle();} else if (Square.equals(type)) {return new Square();} else {throw new RuntimeException(Unknown type);}} }class ShapeFactorySystem {private SimpleShapeFactory simpleShapeFactory;public ShapeFactorySystem(SimpleShapeFactory simpleShapeFactory) {this.simpleShapeFactory simpleShapeFactory;}public void produce(String type, int n) {Shape shape simpleShapeFactory.createShape(type);shape.draw(n);} }3 工厂方法模式 和简单工厂不同的是不同对象的生产工厂也不同。代码示例 public class Main {public static void main(String[] args) {ShapeFactorySystem shapeFactorySystem new ShapeFactorySystem(new CircleFactory(), new SquareFactory());Scanner scanner new Scanner(System.in);int count Integer.parseInt(scanner.nextLine());for (int i 0; i count; i) {String line scanner.nextLine();String[] parts line.split( );String type parts[0];shapeFactorySystem.produce(type, Integer.parseInt(parts[1]));}} }interface Shape {void draw(int n); }class Circle implements Shape {public void draw(int n) {for (int i 0; i n; i) {System.out.println(Circle Block);}} }class Square implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(Square Block);}} }interface ShapeFactory {Shape createShape(String type); }class CircleFactory implements ShapeFactory {Overridepublic Shape createShape(String type) {return new Circle();} }class SquareFactory implements ShapeFactory {Overridepublic Shape createShape(String type) {return new Square();} }class ShapeFactorySystem {private ShapeFactory circleFactory;private ShapeFactory squareFactory;public ShapeFactorySystem(ShapeFactory circleFactory, ShapeFactory squareFactory) {this.circleFactory circleFactory;this.squareFactory squareFactory;}public void produce(String type, int n) {Shape shape;if (Circle.equals(type)) {shape circleFactory.createShape(type);} else if (Square.equals(type)) {shape squareFactory.createShape(type);} else {throw new RuntimeException(Unknown type);}shape.draw(n);} }4 思考 从这个例子中看不出工厂方法模式比简单工厂模式好在哪里。假设需求变化了需要增加一种类型那么对于简单工厂模式只要修改 // 新增类 class xxx implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(xxx Block);}} }// 修改方法 class SimpleShapeFactory {public Shape createShape(String type) {if (Circle.equals(type)) {return new Circle();} else if (Square.equals(type)) {return new Square();} else if (xxx.equals(type)) {...} else {throw new RuntimeException(Unknown type);}} }但是对应用层代码main方法不需要做任何改动。这反而更好。对于简单工厂模式要修改 // 修改应用层代码 public static void main(String[] args) {ShapeFactorySystem shapeFactorySystem new ShapeFactorySystem(new CircleFactory(), new SquareFactory(), xxx);Scanner scanner new Scanner(System.in);int count Integer.parseInt(scanner.nextLine());for (int i 0; i count; i) {String line scanner.nextLine();String[] parts line.split( );String type parts[0];shapeFactorySystem.produce(type, Integer.parseInt(parts[1]));} }// 新增类 class xxx implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(xxx Block);}} }// 新增类 class xxxFactory implements ShapeFactory {... }class ShapeFactorySystem {private ShapeFactory circleFactory;private ShapeFactory squareFactory;private xxxFactory ...;public ShapeFactorySystem(ShapeFactory circleFactory, ShapeFactory squareFactory, xxxFactory ...) {this.circleFactory circleFactory;this.squareFactory squareFactory;...}public void produce(String type, int n) {Shape shape;if (Circle.equals(type)) {shape circleFactory.createShape(type);} else if (Square.equals(type)) {shape squareFactory.createShape(type);} else if (xxx) {...} else {throw new RuntimeException(Unknown type);}shape.draw(n);} }真麻烦啊。 4.1 改进工厂方法模式 代码示例 public class Main {public static void main(String[] args) {ShapeFactorySystem shapeFactorySystem ShapeFactorySystem.getSingleton();Scanner scanner new Scanner(System.in);int count Integer.parseInt(scanner.nextLine());for (int i 0; i count; i) {String line scanner.nextLine();String[] parts line.split( );String type parts[0];shapeFactorySystem.produce(type, Integer.parseInt(parts[1]));}} }interface Shape {void draw(int n); }class Circle implements Shape {public void draw(int n) {for (int i 0; i n; i) {System.out.println(Circle Block);}} }class Square implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(Square Block);}} }interface ShapeFactory {Shape createShape(); }class CircleFactory implements ShapeFactory {Overridepublic Shape createShape() {return new Circle();} }class SquareFactory implements ShapeFactory {Overridepublic Shape createShape() {return new Square();} }class ShapeFactorySystem {private static final MapShapeType, ShapeFactory shapeFactoryMap new HashMap();private static ShapeFactorySystem shapeFactorySystem;private ShapeFactorySystem() {shapeFactoryMap.put(ShapeType.CIRCLE, new CircleFactory());shapeFactoryMap.put(ShapeType.SQUARE, new SquareFactory());}public static ShapeFactorySystem getSingleton() {if (shapeFactorySystem null) {synchronized (ShapeFactorySystem.class) {if (shapeFactorySystem null) {shapeFactorySystem new ShapeFactorySystem();}}}return shapeFactorySystem;}private ShapeFactory acquireShapeFactory(ShapeType type) {return shapeFactoryMap.get(type);}public void produce(String type, int n) {ShapeFactory shapeFactory acquireShapeFactory(ShapeType.of(type));Shape shape shapeFactory.createShape();shape.draw(n);} }enum ShapeType {CIRCLE(Circle), SQUARE(Square);private String value;private ShapeType(String value) {this.value value;}private String getValue() {return value;}public static ShapeType of(String value) {for (ShapeType shapeType : ShapeType.values()) {if (shapeType.getValue().equals(value)) {return shapeType;}}// 如果没有找到匹配的枚举对象可以抛出一个异常或返回nullthrow new IllegalArgumentException(Unknown ShapeType: value);} }多线程场景下不能用HashMap。 如果新增一种类型 // 新增类 class xxx implements Shape {Overridepublic void draw(int n) {for (int i 0; i n; i) {System.out.println(xxx Block);}} }// 新增类 class xxxFactory implements ShapeFactory {Overridepublic Shape createShape() {return new xxx();} }// 修改方法不修改之前代码新增语句 private ShapeFactorySystem() {shapeFactoryMap.put(ShapeType.CIRCLE, new CircleFactory());shapeFactoryMap.put(ShapeType.SQUARE, new SquareFactory()); }// 不修改之前的代码加一个枚举对象 enum ShapeType {CIRCLE(Circle), SQUARE(Square), xxx;... }当然了通过map enum这种改进也可以应用到简单工厂模式中。不过当创建对象变得复杂时简单工厂模式就难以应用对了 class SimpleShapeFactory {public Shape createShape(String type) {if (Circle.equals(type)) {return new Circle(); // 简单对象} else if (Square.equals(type)) {return new Square(); // 简单对象} else {throw new RuntimeException(Unknown type);}} }
http://www.pierceye.com/news/680562/

相关文章:

  • 淮南服装网站建设地址巴彦淖尔网站建设公司
  • 如何让自己的网站被百度收录wordpress 悬浮网易云
  • 天津展示型网站建设外包腾讯云wordpress镜像
  • python做网站点登入没反映wordpress母公司
  • 中国建设培训网站查询系统地产项目网站建设ppt
  • 温州高端网站建设网站开发实验心得
  • 设计参考网站有哪些陕西省西安市事业单位招聘网
  • 月编程做网站wordpress需要调用缩略图
  • 做一份网站动态图多少钱网站托管服务公司
  • 宣传式网站营销型网站套餐
  • 建设网站如何优化关键词江门排名优化公司
  • 做教学的视频网站wordpress小工具推荐
  • 唯品会专门做特卖的网站保险行业网站模板
  • 电商类公司网站应该怎么搭建广州做网站公司哪家比较好
  • 企业网站实名认证时间怎样开通微信公众号
  • 怎么才能在百度上搜到自己的网站如何优化网站
  • 酒泉网站建设价格福清市建设局网站
  • 网站建设有几种厦门网站的建设
  • 沈阳网站外包通过法人姓名查企业
  • 建设网站的多少钱红色风格网站
  • 公司网站运营维护单位温州h5建站
  • 做网站想注册商标是哪一类网站上线具体流程
  • 如何让网站自适应屏幕北京做网站哪个公司好
  • 个人网站建设简历网站路径怎么做
  • 学做面包到什么网站企业网站都没的百度快照咋办
  • 手机网站建设视频教程安徽建站之星
  • 做网站专题页的字大小是多少购物商城排名
  • 门窗东莞网站建设技术支持海口快速建站模板
  • 公司网站制作第三方网站浮窗制作
  • 网站需要服务器吗?万州网站建设