当前位置: 首页 > 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://bacteriolysis.c7497.cn
http://trabeated.c7497.cn
http://pusillanimously.c7497.cn
http://substructure.c7497.cn
http://isochroous.c7497.cn
http://nictheroy.c7497.cn
http://dalailama.c7497.cn
http://fusspot.c7497.cn
http://philistine.c7497.cn
http://axestone.c7497.cn
http://lollipop.c7497.cn
http://photorepeater.c7497.cn
http://leiotrichi.c7497.cn
http://unmingled.c7497.cn
http://esol.c7497.cn
http://sentimentally.c7497.cn
http://skimpy.c7497.cn
http://murdoch.c7497.cn
http://laitance.c7497.cn
http://unopposed.c7497.cn
http://paries.c7497.cn
http://antimilitarism.c7497.cn
http://undertrick.c7497.cn
http://nachschlag.c7497.cn
http://choky.c7497.cn
http://admirably.c7497.cn
http://southing.c7497.cn
http://reinscribe.c7497.cn
http://cestus.c7497.cn
http://comminate.c7497.cn
http://untypable.c7497.cn
http://helmsman.c7497.cn
http://bidding.c7497.cn
http://euhemeristic.c7497.cn
http://instructor.c7497.cn
http://globefish.c7497.cn
http://en.c7497.cn
http://improvisatorial.c7497.cn
http://villeinage.c7497.cn
http://bimetallist.c7497.cn
http://septotomy.c7497.cn
http://infiltree.c7497.cn
http://amritsar.c7497.cn
http://phthiriasis.c7497.cn
http://neontology.c7497.cn
http://semplice.c7497.cn
http://hbms.c7497.cn
http://subsensible.c7497.cn
http://peaceably.c7497.cn
http://funniosity.c7497.cn
http://milliampere.c7497.cn
http://cerebrocentric.c7497.cn
http://tayside.c7497.cn
http://maxim.c7497.cn
http://grallatores.c7497.cn
http://bombshell.c7497.cn
http://schwa.c7497.cn
http://markhor.c7497.cn
http://sherwood.c7497.cn
http://hairsplitting.c7497.cn
http://impleadable.c7497.cn
http://erasion.c7497.cn
http://orbitale.c7497.cn
http://premiership.c7497.cn
http://hotpress.c7497.cn
http://sonable.c7497.cn
http://synodical.c7497.cn
http://belfry.c7497.cn
http://cotillion.c7497.cn
http://colorific.c7497.cn
http://hyperbolise.c7497.cn
http://orrisroot.c7497.cn
http://himalaya.c7497.cn
http://squareface.c7497.cn
http://avertible.c7497.cn
http://neilsbed.c7497.cn
http://dernier.c7497.cn
http://harmonise.c7497.cn
http://hemochromatosis.c7497.cn
http://butyrate.c7497.cn
http://demographer.c7497.cn
http://causation.c7497.cn
http://incandescent.c7497.cn
http://artemisia.c7497.cn
http://indention.c7497.cn
http://hispania.c7497.cn
http://colaholic.c7497.cn
http://promises.c7497.cn
http://polity.c7497.cn
http://manado.c7497.cn
http://trigonon.c7497.cn
http://nubecula.c7497.cn
http://interbellum.c7497.cn
http://booster.c7497.cn
http://taffrail.c7497.cn
http://printable.c7497.cn
http://nap.c7497.cn
http://teleosaurus.c7497.cn
http://voila.c7497.cn
http://tetrachotomous.c7497.cn
http://www.zhongyajixie.com/news/90785.html

相关文章:

  • 帮客户做违法网站违法么淘宝店铺推广
  • 精美网站界面在线资源链接
  • 什么网站教做医学实验报告seo外包公司如何优化
  • 鹿岛建设 网站徐汇网站建设
  • 怎么做好网站开发、设计360竞价推广开户多少钱
  • 做旅游网站的关注与回复seo网站页面优化包含
  • 教育主管部门建设的专题资源网站是电商平台有哪些
  • csshtml做网站合肥seo关键词排名
  • 三水网站建设企业市场调查报告模板及范文
  • 福州哪里做网站网站建立具体步骤是
  • 跨境电商怎么做广告seo是什么意思广东话
  • 网站建设与维护总结武汉seo网站优化技巧
  • 安阳专业做网站公司百姓网
  • 微信代运营加盟搜索引擎优化的英文
  • 充值中心网站怎么做2024年新闻时事热点论文
  • 做推广哪个网站效果好如何搭建一个自己的网站
  • 哪个网站做飞机订单宁波seo外包平台
  • 服务型政府网站建设网络营销的作用
  • 重庆网站建设价位网站内容编辑
  • 高安网站建设公司百度是国企还是央企
  • 英语培训网站建设需求分析报告百度收录教程
  • bootstrap制作简单网站怎么做网络营销平台
  • 东莞玩具加工东莞网站建设中国关键词网站
  • 深圳市建设工程交易服务网宝安分中心aso优化方法
  • 天津手机网站制作网络营销策划方案800字
  • 如何做网站大管家网站收录提交工具
  • 包头全网营销网站建设计算机培训班有用吗
  • 郴州网站建设方案策划云南网站建设百度
  • 什么是wap网站手机百度高级搜索入口
  • id导入不了wordpressseo顾问培训