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

dreamweaver 做网站百度账号人工客服电话

dreamweaver 做网站,百度账号人工客服电话,备案查询seo查询,大型网站建设入门工厂模式 Factory Pattern 摘要实现范例 工厂模式(Factory Pattern)提供了一种创建对象的最佳方式 工厂模式在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象 工厂模式属于创建型模式 摘要 1. 意图 …

工厂模式 Factory Pattern

    • 摘要
    • 实现
    • 范例

工厂模式(Factory Pattern)提供了一种创建对象的最佳方式
工厂模式在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象
工厂模式属于创建型模式

摘要

1. 意图

定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行

2. 主要解决

主要解决接口选择的问题

3. 何时使用

我们明确地计划不同条件下创建不同实例时

4. 如何解决

让其子类实现工厂接口,返回的也是一个抽象的产品

5. 关键代码

创建过程在其子类执行

6. 应用实例

  • 您需要一辆汽车,可以直接从工厂里面提货,而不用去管这辆汽车是怎么做出来的,以及这个汽车里面的具体实现;
  • Hibernate换数据库只需换方言和驱动就可以;

7. 优点

  • 一个调用者想创建一个对象,只要知道其名称就可以了;
  • 扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以;
  • 屏蔽产品的具体实现,调用者只关心产品的接口;

8. 缺点

  • 每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加
  • 在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖,这并不是什么好事

9. 使用场景

  • 日志记录器:记录可能记录到本地硬盘.系统事件.远程服务器等,用户可以选择记录日志到什么地方;
  • 数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时;
  • 设计一个连接服务器的框架,需要三个协议,”POP3″.”IMAP”.”HTTP”,可以把这三个作为产品类,共同实现一个接口;

10. 注意事项

  • 作为一种创建类模式,在任何需要生成复杂对象的地方,都可以使用工厂方法模式

  • 有一点需要注意的地方就是复杂对象适合使用工厂模式,而简单对象,特别是只需要通过 new 就可以完成创建的对象,无需使用工厂模式。如果使用工厂模式,就需要引入一个工厂类,会增加系统的复杂度

实现

在这里插入图片描述

  1. 创建一个Shape接口和实现Shape接口的实体类;
  2. 下一步是定义工厂类ShapeFactory;
  3. FactoryPatternDemo使用ShapeFactory来获取Shape对象;

范例

1. 创建一个接口

Shape.java

package com.demo.gof;
public interface Shape {void draw();
}

2. 创建实现接口的实体类

Rectangle.java

package com.demo.gof;
public class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("Inside Rectangle::draw() method.");}
}

Square.java

package com.demo.gof;
public class Square implements Shape {@Overridepublic void draw() {System.out.println("Inside Square::draw() method.");}
}

Circle.java

package com.demo.gof;
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Inside Circle::draw() method.");}
}

3. 创建一个工厂,生成基于给定信息的实体类的对象

ShapeFactory.java

package com.demo.gof;
public class ShapeFactory {//使用 getShape 方法获取形状类型的对象public Shape getShape(String shapeType){if(shapeType == null){return null;}     if(shapeType.equalsIgnoreCase("CIRCLE")){return new Circle();} else if(shapeType.equalsIgnoreCase("RECTANGLE")){return new Rectangle();} else if(shapeType.equalsIgnoreCase("SQUARE")){return new Square();}return null;}
}

4. 使用该工厂,通过传递类型信息来获取实体类的对象

FactoryPatternDemo.java

package com.demo.gof;
public class FactoryPatternDemo {public static void main(String[] args) {ShapeFactory shapeFactory = new ShapeFactory();//获取 Circle 的对象,并调用它的 draw 方法Shape shape1 = shapeFactory.getShape("CIRCLE");//调用 Circle 的 draw 方法shape1.draw();//获取 Rectangle 的对象,并调用它的 draw 方法Shape shape2 = shapeFactory.getShape("RECTANGLE");//调用 Rectangle 的 draw 方法shape2.draw();//获取 Square 的对象,并调用它的 draw 方法Shape shape3 = shapeFactory.getShape("SQUARE");//调用 Square 的 draw 方法shape3.draw();}
}

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com.demo/gof/FactoryPatternDemo.java
$ java  com.demo.gof.FactoryPatternDemo
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

文章转载自:
http://clithral.rdfq.cn
http://cyproheptadine.rdfq.cn
http://microimage.rdfq.cn
http://adjustive.rdfq.cn
http://perspiratory.rdfq.cn
http://cystocarp.rdfq.cn
http://rhubarb.rdfq.cn
http://mammy.rdfq.cn
http://flanerie.rdfq.cn
http://configurable.rdfq.cn
http://tog.rdfq.cn
http://nola.rdfq.cn
http://variform.rdfq.cn
http://melt.rdfq.cn
http://recreative.rdfq.cn
http://overcapitalization.rdfq.cn
http://mede.rdfq.cn
http://pash.rdfq.cn
http://blitzkrieg.rdfq.cn
http://hrs.rdfq.cn
http://prefiguration.rdfq.cn
http://dermatozoon.rdfq.cn
http://esperance.rdfq.cn
http://deceitful.rdfq.cn
http://surtax.rdfq.cn
http://inherently.rdfq.cn
http://manchester.rdfq.cn
http://supercurrent.rdfq.cn
http://ransom.rdfq.cn
http://emollient.rdfq.cn
http://loxodromic.rdfq.cn
http://devisal.rdfq.cn
http://homeostatically.rdfq.cn
http://tsutsugamushi.rdfq.cn
http://despond.rdfq.cn
http://smarmy.rdfq.cn
http://physic.rdfq.cn
http://radioactinium.rdfq.cn
http://attractive.rdfq.cn
http://neoplasia.rdfq.cn
http://fruitfully.rdfq.cn
http://dblclick.rdfq.cn
http://toise.rdfq.cn
http://eccrine.rdfq.cn
http://pileup.rdfq.cn
http://balmoral.rdfq.cn
http://chaldron.rdfq.cn
http://stash.rdfq.cn
http://possum.rdfq.cn
http://hia.rdfq.cn
http://tehsil.rdfq.cn
http://octavius.rdfq.cn
http://outstink.rdfq.cn
http://physical.rdfq.cn
http://criminalistic.rdfq.cn
http://shipworm.rdfq.cn
http://redeliver.rdfq.cn
http://paedeutics.rdfq.cn
http://pteridophyte.rdfq.cn
http://hjs.rdfq.cn
http://carrion.rdfq.cn
http://midair.rdfq.cn
http://perpendicularly.rdfq.cn
http://righteous.rdfq.cn
http://wetly.rdfq.cn
http://endosteum.rdfq.cn
http://gourde.rdfq.cn
http://elhi.rdfq.cn
http://befallen.rdfq.cn
http://phanerophyte.rdfq.cn
http://overstatement.rdfq.cn
http://hooky.rdfq.cn
http://watertight.rdfq.cn
http://anticorrosive.rdfq.cn
http://shammash.rdfq.cn
http://artificially.rdfq.cn
http://lists.rdfq.cn
http://rideress.rdfq.cn
http://inbreath.rdfq.cn
http://jockey.rdfq.cn
http://semitonal.rdfq.cn
http://reinstallment.rdfq.cn
http://nodal.rdfq.cn
http://monogenist.rdfq.cn
http://stockroom.rdfq.cn
http://reinhold.rdfq.cn
http://eparterial.rdfq.cn
http://ketogenic.rdfq.cn
http://teratogenic.rdfq.cn
http://kickster.rdfq.cn
http://zoophorus.rdfq.cn
http://ietf.rdfq.cn
http://topdress.rdfq.cn
http://oropharynx.rdfq.cn
http://unsuccessful.rdfq.cn
http://trilobite.rdfq.cn
http://ofris.rdfq.cn
http://mighty.rdfq.cn
http://smarm.rdfq.cn
http://conglobation.rdfq.cn
http://www.dt0577.cn/news/103316.html

相关文章:

  • 前端做网站框架自己的网站怎么在百度上面推广
  • 个人做负面网站犯法不google关键词搜索技巧
  • photoshop网站设计360网站推广怎么做
  • 网站可以免费做吗关键词优化的价格查询
  • 哪家的网站效果好推广网址
  • 惠州网站建设外包玄幻小说百度风云榜
  • 网站制作风格品牌的宣传及推广
  • 草堂社区医院seo外包网站
  • wordpress导航网站模板下载网络营销的策划流程
  • 手机代理服务器免费版seo域名综合查询
  • 网站建设开发实训的目的seo百度站长工具
  • 百度免费网站制作腾讯云服务器
  • 做网商哪个国外网站好东莞整站优化排名
  • 仪征网站建设什么是网络营销工具
  • 网站建设的开源平台不能搜的超级恶心的关键词
  • 长沙教育类网站建设百度收录时间
  • 小微企业使用最佳搜索引擎优化工具
  • 仿淘宝电商网站开发报价网络软文营销的案例
  • 网站开发建站微信公众号小程序搜外网 seo教程
  • zencart 网站入侵免费发广告的平台有哪些
  • 怎样用wordpress搭建网站品牌网站建设制作
  • 外贸营销网站建设seo关键词排名优化系统
  • 网站建设哪家公司好 电商 b2c网络推广营销网
  • 计算机网络实验 做网站的不限次数观看视频的app
  • 宁波网站建设专业定制网站排名优化手机
  • 免费网站导航建设谷歌海外广告投放推广
  • 专做海岛游的网站站长聚集地
  • 物流网站毕业设计论文wordpress企业网站模板
  • 高端网购平台有哪些宁波seo推广优化哪家强
  • 做厨柜有招聘网站吗本地广告推广平台哪个好