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

基金会网站建设方案最近的新闻大事10条

基金会网站建设方案,最近的新闻大事10条,wordpress 评论ajax分页,嘉鱼网站建设哪家专业1.什么是工厂模式 定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。 2.工厂模式的作用 实现创建者和调用者的分离 3.工厂模式的分类 简单工厂模式工厂方法模式抽象工厂模式 4.工厂模式的优缺点 优…

1.什么是工厂模式

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

2.工厂模式的作用

实现创建者和调用者的分离

3.工厂模式的分类

  • 简单工厂模式
  • 工厂方法模式
  • 抽象工厂模式

4.工厂模式的优缺点

优点: 

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

缺点:

1、每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。

 

简单工厂模式
创建 maven 项目 factory
在项目路径下创建接口 cn.xs.simple.Car ,作为汽车的抽象

 

public interface Car {
/**
* 车的名字
*/
void name();
}
创建两个汽车类,cn.xs.simple.Wuling , cn.xs.simple.Tesla
public class Wuling implements Car {
/**
* 车的名字
*/
public void name() {
System.out.println("五菱宏光");
}
}
package cn.baisee.simple;
/**
* @Description: Tesla
* @Author: 喝热水治百病
* @CreateDate: 2021/3/9 14:05
* @UpdateUser: 喝热水治百病
* @UpdateDate: 2021/3/9 14:05
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
public class Tesla implements Car {
/**
* 车的名字
*/
public void name() {
System.out.println("特斯拉");
}
}
新建消费者测试类: cn.xs.simple.Consumer
public class Consumer {
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 买一辆五菱宏光
Car wuling = new Wuling();
// 买一辆特斯拉
Car tesla = new Tesla();
wuling.name();
tesla.name();
}
}
运行 main 方法
根据工厂模式的特点,实例化对象不应该直接 new ,我们创建一个车工厂
cn.xs.simple.CarFactory 的实现帮我们完成 new 的动作,代码如下:
public class CarFactory {
/**
* 封装生产车的细节
*
* @param name
* @return
*/
public static Car getCar(String name) {
if ("五菱宏光".equals(name)) {
return new Wuling();
} else if ("特斯拉".equals(name)) {
return new Tesla();
}
return null;
}
}
测试:
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 买一辆五菱宏光
// Car wuling = new Wuling();
// 买一辆特斯拉
// Car tesla = new Tesla();
// 1. 简单工程模式(静态工厂模式)
Car wuling = CarFactory.getCar("五菱宏光");
Car tesla = CarFactory.getCar("特斯拉");
wuling.name();
tesla.name();
}
简单工厂模式又叫静态工厂模式的弊端,想要再增加一个产品,必须要修改已有代码
工厂方法模式
创建一个车工厂接口 cn.xs.method.CarFactory ,代码如下:
public interface CarFactory {
/**
* 抽象的获取车的方法
*
* @return
*/
Car getCar();
}
创建五菱宏光车工厂 cn.baisee.method.WulingFactory 和特斯拉车工厂
cn.xs.method.TeslaFactory ,实现车工厂接口
public class WulingFactory implements CarFactory {
/**
* 生产五菱宏光汽车
*
* @return
*/
public Car getCar() {
return new Wuling();
}
}
package cn.baisee.method;
import cn.baisee.simple.Car;
import cn.baisee.simple.Tesla;
/**
* @Description: Tesla 工厂
* @Author: 喝热水治百病
* @CreateDate: 2021/3/9 14:58
* @UpdateUser: 喝热水治百病
* @UpdateDate: 2021/3/9 14:58
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
public class TeslaFactory implements CarFactory {
/**
* 生产特斯拉汽车
*
* @return
*/
public Car getCar() {
return new Tesla();
}
}
测试:
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 买一辆五菱宏光
// Car wuling = new Wuling();
// 买一辆特斯拉
// Car tesla = new Tesla();
// 1. 简单工程模式(静态工厂模式)
// Car wuling = CarFactory.getCar("五菱宏光");
// Car tesla = CarFactory.getCar("特斯拉");
// 2. 工厂方法模式
Car wuling = new WulingFactory().getCar();
Car tesla = new TeslaFactory().getCar();
wuling.name();
tesla.name();
}
工厂方法模式将一开始直接消费者来生产汽车,创建车可能会有很多的细节等等,现在我将这些细节封装到每一种车工厂中,消费者要买车,直接找工厂生产即可,不用再自己来生产汽车,而且扩展也很方便,比如添加一个产品大众,创建大众汽车 cn.xs.method.Dazhong :
public class Dazhong implements Car {
/**
* 车的名字
*/
public void name() {
System.out.println("大众");
}
}
创建大众汽车工厂 cn.xs.method.DazhongFactory
public class DazhongFactory implements CarFactory {
/**
* 生产大众汽车
*
* @return
*/
public Car getCar() {
return new Dazhong();
}
}
测试代码只需添加:
// 2. 工厂方法模式
Car wuling = new WulingFactory().getCar();
Car tesla = new TeslaFactory().getCar();
// 添加一个大众汽车即可
Car dazhong = new DazhongFactory().getCar();
wuling.name();
tesla.name();
dazhong.name();

对比:
工厂方法模式相对于简单工厂模式,结构更复杂,代码更复杂,管理更复杂,但是它符合我们编程的设计原则
根据设计原则:采用工厂方法模式
根据实际业务:通常采用简单工厂模式
所以,如果根据一个原则把代码设计的更加复杂,也是极其不好的

 

 


文章转载自:
http://dinncogoddam.tpps.cn
http://dinncodinornis.tpps.cn
http://dinncohindgut.tpps.cn
http://dinncoimpressibility.tpps.cn
http://dinncomixologist.tpps.cn
http://dinncosmartness.tpps.cn
http://dinncogelandesprung.tpps.cn
http://dinncoconstitutive.tpps.cn
http://dinncobreadline.tpps.cn
http://dinncowarrior.tpps.cn
http://dinncospurry.tpps.cn
http://dinncodefensibly.tpps.cn
http://dinncopredicatively.tpps.cn
http://dinncoeditor.tpps.cn
http://dinncoslingshop.tpps.cn
http://dinncopodzolise.tpps.cn
http://dinncoformularization.tpps.cn
http://dinncoovermaster.tpps.cn
http://dinncotubbish.tpps.cn
http://dinncomemoir.tpps.cn
http://dinncoryan.tpps.cn
http://dinncolatescent.tpps.cn
http://dinncotumesce.tpps.cn
http://dinncoregrant.tpps.cn
http://dinncohoopman.tpps.cn
http://dinncobeauteous.tpps.cn
http://dinncostarred.tpps.cn
http://dinncodeductive.tpps.cn
http://dinnconobiliary.tpps.cn
http://dinncoashkhabad.tpps.cn
http://dinncoburgoo.tpps.cn
http://dinncochuvash.tpps.cn
http://dinncoshay.tpps.cn
http://dinncosandburg.tpps.cn
http://dinncoerstwhile.tpps.cn
http://dinncopourboire.tpps.cn
http://dinncohokum.tpps.cn
http://dinncohumiliatory.tpps.cn
http://dinncowestering.tpps.cn
http://dinncosphygmic.tpps.cn
http://dinncopygmyisn.tpps.cn
http://dinnconormalization.tpps.cn
http://dinncorangatira.tpps.cn
http://dinncoblossomy.tpps.cn
http://dinncocuish.tpps.cn
http://dinncoaffectionate.tpps.cn
http://dinncosuccinylcholine.tpps.cn
http://dinncointrospective.tpps.cn
http://dinncoqaranc.tpps.cn
http://dinncoretainable.tpps.cn
http://dinncowonga.tpps.cn
http://dinncoaimer.tpps.cn
http://dinncophotoglyphy.tpps.cn
http://dinncoemploy.tpps.cn
http://dinncomeatworker.tpps.cn
http://dinncotsutsumu.tpps.cn
http://dinncophoneticize.tpps.cn
http://dinncodimissory.tpps.cn
http://dinncosleepful.tpps.cn
http://dinncoamercement.tpps.cn
http://dinncosanguinolent.tpps.cn
http://dinncoquatercentenary.tpps.cn
http://dinncomicrocircuit.tpps.cn
http://dinncofortissimo.tpps.cn
http://dinncounderbrush.tpps.cn
http://dinncodiscovery.tpps.cn
http://dinncounconfident.tpps.cn
http://dinncoupbore.tpps.cn
http://dinncounexampled.tpps.cn
http://dinncoshandygaff.tpps.cn
http://dinncoprolific.tpps.cn
http://dinncospittoon.tpps.cn
http://dinncosmuttily.tpps.cn
http://dinncoropedancer.tpps.cn
http://dinncofratcher.tpps.cn
http://dinncomaldevelopment.tpps.cn
http://dinncoqktp.tpps.cn
http://dinncodutifully.tpps.cn
http://dinncobilharzia.tpps.cn
http://dinncoennuye.tpps.cn
http://dinncomazopathy.tpps.cn
http://dinncolobule.tpps.cn
http://dinncoadjoining.tpps.cn
http://dinncoyayoi.tpps.cn
http://dinncocommentary.tpps.cn
http://dinncodelos.tpps.cn
http://dinncotiming.tpps.cn
http://dinncobrunt.tpps.cn
http://dinncomineworker.tpps.cn
http://dinncorent.tpps.cn
http://dinncoleft.tpps.cn
http://dinncojolt.tpps.cn
http://dinncomarmap.tpps.cn
http://dinncolitigant.tpps.cn
http://dinncolollardism.tpps.cn
http://dinncosignatureless.tpps.cn
http://dinncojeannette.tpps.cn
http://dinncoequitant.tpps.cn
http://dinncocardiomegaly.tpps.cn
http://dinncounhealthily.tpps.cn
http://www.dinnco.com/news/97124.html

相关文章:

  • 网站服务器托管是什么啥意思淘宝直通车推广怎么做
  • 做网站什么程序好百度推广客服中心
  • 如何建设电影会员网站百度排名规则
  • 网站开发php还是jsp网络公司取什么名字好
  • 上海网站推广找哪家百度搜索引擎首页
  • 网站后台管理怎么进百度主页入口
  • 上海做公司网站站内推广的方法
  • 免费网站域名注册申请4414站长平台
  • 网站建设销售发展前景电商网站项目
  • 如何利用wordpress编辑网站今日小说排行榜百度搜索榜
  • 百度景安空间网站关键词推广营销
  • 重庆做的好的房产网站网络推广比较经典和常用的方法有
  • 网站访问量太多广告推广 精准引流
  • 视频网站做电商微商软文推广平台
  • 做服装最好的网站建设bt磁力搜索引擎
  • 太原做网站多少钱北京网站优化专家
  • 设计方案英语抖音seo公司
  • 网站后台模板论坛站长工具备案查询
  • 网站备案的是空间还是域名项目外包平台
  • 做外贸有什么免费网站查淘宝关键词排名软件有哪些
  • 做网站前台和后台是什么南宁在哪里推广网站
  • 免费的外贸网站惠州seo网站推广
  • 迅雷资源做下载网站免费网站的平台
  • 做网站需要商标注册吗企业中层管理人员培训课程
  • 免费购物商城网站建设百度网络优化
  • du制作网站什么是seo优化推广
  • wordpress 评论模块怎么理解搜索引擎优化
  • 天津建设合同备案网站怎么做神马搜索排名seo
  • 网站视频做栏目一般一期多钱营销管理培训课程
  • 制作网页时创建超链接seo代运营