当前位置: 首页 > news >正文 厦门学校网站建设连锁销售平台 news 2025/10/29 7:30:24 厦门学校网站建设,连锁销售平台,品牌网站运营,logo设计在线生成免费免费设计logo在开发组件的时候比如button、text等#xff0c;需要对这些组件做比较多的初始化工作#xff0c;比如初始化长度等。传统的开发方案如下#xff1a; 图 传统开发方案UML 上面的方案组件创建及组件的其他业务操作耦合在一起#xff0c;违背了单一职责原则#xff1b;在客户… 在开发组件的时候比如button、text等需要对这些组件做比较多的初始化工作比如初始化长度等。传统的开发方案如下 图 传统开发方案UML 上面的方案组件创建及组件的其他业务操作耦合在一起违背了单一职责原则在客户端Client需要知道具体的组件类名才能创建对应的组件。 1 工厂方法模式概述 定义一个用于创建对象的接口让子类决定将哪一个类实例化。针对不同的产品提供不同的工厂系统提供一个与产品等级结构对应的工厂等级。 图 工厂方法模式UML Product: 抽象产品类是工厂模式所创建对象的超类型也是产品对象的公共父类。 ConcreteProduct: 具体的产品类由专门的具体工厂创建具体工厂和具体产品之间一一对应。 Factory: 抽象工厂类声名了创建产品的方法是工厂方法模式的核心所有创建对象的工厂类都必须实现该接口。 ConcreteFactory: 具体的工厂类实现了抽象工厂类中定义的工厂方法可有客户端调用返回一个具体产品类的实例。 1.1 工厂方法模式实现上述需求 public abstract class Module {private double width;private double height;private String name;public double getWidth() {return width;}public void setWidth(double width) {this.width width;}public double getHeight() {return height;}public void setHeight(double height) {this.height height;}public String getName() {return name;}public void setName(String name) {this.name name;}Overridepublic String toString() {return name { width width , height height };} }public class ButtonModule extends Module{}public class TextModule extends Module{ }/*** 在开发组件的时候比如button、text等需要对这些组件做比较多的初始化工作比如初始化长度等。*/ public interface Factory {Module create();}public class ButtonFactory implements Factory{Overridepublic Module create() {ButtonModule module new ButtonModule();module.setHeight(100.0);module.setName(button);// 其他初始化工作return module;} }public class TextFactory implements Factory{Overridepublic Module create() {TextModule textModule new TextModule();textModule.setName(text);textModule.setHeight(123);//其他初始化操作return textModule;} } 为了让系统具有更好的灵活性和可扩展性客户端创建工厂对象时不再通过new关键字而是通过存储在配置文件里的具体工厂类的类名使用反射机制来创建具体的工厂。优化如下 ?xml version1.0 encodingUTF-8? configclassNamecom.huangmingfu.factory_method.ButtonFactory/className /configpublic class FactoryMethodXmlUtil {public static Object getBean() {try {DocumentBuilderFactory dFactory DocumentBuilderFactory.newInstance();DocumentBuilder documentBuilder dFactory.newDocumentBuilder();Document document documentBuilder.parse(FactoryMethodXmlUtil.class.getClassLoader().getResourceAsStream(factory_method/concrete-factory.xml));NodeList nodeList document.getElementsByTagName(className);Node node nodeList.item(0).getFirstChild();String className node.getNodeValue();Class? aClass Class.forName(className);return aClass.newInstance();} catch (Exception e) {e.printStackTrace();return null;}}}public class Client {public static void main(String[] args) {Factory factory;factory (Factory) FactoryMethodXmlUtil.getBean();assert factory ! null;System.out.println(factory.getClass());Module module factory.create();System.out.println(module);}}2 优缺点 2.1 优点 1向客户隐藏了产品类实例化细节用户只需关心所需产品对应的工厂无须关心创建细节甚至无需知道具体产品类的类名 2扩展性好符合开闭原则。 2.2 缺点 1添加新的产品时需要编写新的具体产品类及对应的具体工厂类使得系统中类的个数成对增加增加了系统复杂度及开销。 3 适用场景 客户不知道所需要的对象的类只知道所对应的工厂。对象创建时需初始化操作多。 文章转载自: http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.jmbgl.cn.gov.cn.jmbgl.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn 查看全文 http://www.tj-hxxt.cn/news/258900.html 相关文章: 长春网络传媒做网站骗钱长沙手机网站制作 电子商务网站设计书大学生ppt自我介绍幻灯片 网站背景颜色代码怎么查看服务器上的网站 做网站 传视频 用什么笔记本好云主机怎么安装网站 不建网站如何做淘宝客这2个代码 找做网站的 安装一下 网页版视频网站建设需要多少钱厦门 网站建设 公司 免费自助建站来宾网站优化 淘宝网站制作大型免费网页游戏排行榜 金融公司网站规划方案百度aipage智能建站系统 pr效果做的好的网站有哪些动漫网页设计作业 福建金融公司网站建设百度提升排名 网站建设公司账户搭建重庆官方推广网站 专业网站推广引流厦门工程建设招聘信息网站 高要区住房和城乡建设局网站网站建设外包兼职 商业空间设计案例网站网站关键词排名检测工具 域名备案网站公司的管理方式与管理方法 备份核验单时网站域名tk网站免费 小视频解析网站怎么做房地产网站模版 网站设计确认wordpress 酷炫分页 网站二次开发合同2024新冠又来了吗 做音乐网站怎么放音乐怎么设计自己logo 网站服务器租用时间证件查询网入口 微信公众平台开发网站免费建站论坛 专业的医疗网站建设海外推广方法有哪些 discuz建站教程杭州网站设计公司哪个好 网站 先建设还是先等级保护备案关键词优化seo公司 dedecms_v5.6室内装饰设计公司企业网站模板.rar.net网站 作品 天水做网站电话做网站用什么空间 迁安三屏网站建设微信小程序开发需要什么技能 中铁建设集团华北分公司网站搜什么关键词能搜到好片