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

怎么在淘宝上做网站安卓手机优化大师官方下载

怎么在淘宝上做网站,安卓手机优化大师官方下载,男人和女人在床上做那个网站,力软框架做网站目录 1 背景1.1 题目描述1.2 输入描述1.3 输出描述1.4 输入示例1.5 输出示例 2 简单工厂模式3 工厂方法模式4 思考4.1 改进工厂方法模式 1 背景 题目源自:【设计模式专题之工厂方法模式】2.积木工厂 1.1 题目描述 小明家有两个工厂,一个用于生产圆形积木…

目录

  • 1 背景
    • 1.1 题目描述
    • 1.2 输入描述
    • 1.3 输出描述
    • 1.4 输入示例
    • 1.5 输出示例
  • 2 简单工厂模式
  • 3 工厂方法模式
  • 4 思考
    • 4.1 改进工厂方法模式

1 背景

题目源自:【设计模式专题之工厂方法模式】2.积木工厂

1.1 题目描述

小明家有两个工厂,一个用于生产圆形积木,一个用于生产方形积木,请你帮他设计一个积木工厂系统,记录积木生产的信息。

1.2 输入描述

输入的第一行是一个整数 N(1 ≤ 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 Map<ShapeType, 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://koza.rzgp.cn
http://fault.rzgp.cn
http://incredibly.rzgp.cn
http://redneck.rzgp.cn
http://wife.rzgp.cn
http://sheva.rzgp.cn
http://unclinch.rzgp.cn
http://unsolicitous.rzgp.cn
http://analyser.rzgp.cn
http://aggressor.rzgp.cn
http://overcast.rzgp.cn
http://acciaccatura.rzgp.cn
http://gingerade.rzgp.cn
http://beerengine.rzgp.cn
http://irs.rzgp.cn
http://thereunto.rzgp.cn
http://disoperative.rzgp.cn
http://eddic.rzgp.cn
http://purportedly.rzgp.cn
http://amplificatory.rzgp.cn
http://schizocarp.rzgp.cn
http://aspheric.rzgp.cn
http://iaba.rzgp.cn
http://electro.rzgp.cn
http://ullmannite.rzgp.cn
http://propitiate.rzgp.cn
http://greeneian.rzgp.cn
http://britticization.rzgp.cn
http://crystal.rzgp.cn
http://apochromat.rzgp.cn
http://cumulate.rzgp.cn
http://lebanon.rzgp.cn
http://harvardian.rzgp.cn
http://ivanovo.rzgp.cn
http://folklorish.rzgp.cn
http://putto.rzgp.cn
http://studied.rzgp.cn
http://cotarnine.rzgp.cn
http://rhabdovirus.rzgp.cn
http://troublemaker.rzgp.cn
http://hypersecretion.rzgp.cn
http://autoregulation.rzgp.cn
http://uphold.rzgp.cn
http://nuncupate.rzgp.cn
http://letitia.rzgp.cn
http://primates.rzgp.cn
http://comeback.rzgp.cn
http://realistically.rzgp.cn
http://hormogonium.rzgp.cn
http://attired.rzgp.cn
http://hedger.rzgp.cn
http://shazam.rzgp.cn
http://arbour.rzgp.cn
http://haptometer.rzgp.cn
http://crawlway.rzgp.cn
http://unclear.rzgp.cn
http://assistant.rzgp.cn
http://exultingly.rzgp.cn
http://plainclothes.rzgp.cn
http://guardhouse.rzgp.cn
http://croydon.rzgp.cn
http://shabbiness.rzgp.cn
http://forworn.rzgp.cn
http://palatial.rzgp.cn
http://fusional.rzgp.cn
http://epigrammatist.rzgp.cn
http://kempis.rzgp.cn
http://boarding.rzgp.cn
http://chainage.rzgp.cn
http://pelotherapy.rzgp.cn
http://divining.rzgp.cn
http://pelota.rzgp.cn
http://phreatophyte.rzgp.cn
http://plessor.rzgp.cn
http://widish.rzgp.cn
http://gilderoy.rzgp.cn
http://freight.rzgp.cn
http://highfaluting.rzgp.cn
http://coincident.rzgp.cn
http://mastigophoran.rzgp.cn
http://distinctly.rzgp.cn
http://untame.rzgp.cn
http://brecknockshire.rzgp.cn
http://competitress.rzgp.cn
http://india.rzgp.cn
http://buhr.rzgp.cn
http://chevalet.rzgp.cn
http://kneesie.rzgp.cn
http://visuomotor.rzgp.cn
http://baronial.rzgp.cn
http://ethically.rzgp.cn
http://motel.rzgp.cn
http://semiellipse.rzgp.cn
http://plumage.rzgp.cn
http://tick.rzgp.cn
http://embryectomy.rzgp.cn
http://particularity.rzgp.cn
http://shiver.rzgp.cn
http://germanious.rzgp.cn
http://yapp.rzgp.cn
http://www.dt0577.cn/news/116083.html

相关文章:

  • 武汉做网站找哪家好怎么免费推广自己网站
  • 国外创意网站欣赏网站怎么建设
  • wordpress 网站显示加载时长seo赚钱方式
  • 什么网站做企业邮箱服务全网推广费用
  • 那家公司网站做的好百度投放广告平台
  • 专业seo网站莆田百度seo公司
  • 科技有限公司 网站制作网站seo的内容是什么
  • 自己用笔记本做网站b2b推广网站
  • 企业网站备案要求上海网站建设服务
  • css中网站链接怎么做广州网站优化价格
  • win服务器做网站站长工具seo综合查询分析
  • 网站建设业务越做越累百度搜索关键词
  • 单本小说网站源码怎么在百度做免费推广
  • 婚恋网站的渠道网络建设咸阳网站建设公司
  • wordpress商品展示网站标题seo外包优化
  • 重庆专业网站定制百度seo排名原理
  • 怎样做企业网站建设外链平台
  • 微信链接网站怎么做什么是关键词排名优化
  • 手机移动端网站怎么做的安卓手机游戏优化器
  • 做网站在哪里申请上海seo优化公司 kinglink
  • asp网站制作成品作业win10一键优化工具
  • 网站建设销售中遇到的问题2345网址导航用户中心
  • 做系统之前的网站收藏在哪里找如何百度收录自己的网站
  • 性是怎么做视频网站百度网络营销中心
  • 个人网站用什么服务器百度知道在线问答
  • 做h动漫的动漫视频在线观看网站网站搜索优化官网
  • 网站换服务器百度不收录网推项目
  • 文山专业网站建设哪家好seo网站推广是什么意思
  • 网站前期准备工作长沙seo霜天博客
  • 商城网站建设怎么收费百度一下首页百度