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

扬州网站建设制作典型十大优秀网络营销案例

扬州网站建设制作,典型十大优秀网络营销案例,搭建网站架构怎么做,潍坊营销型网站制作本文使用了王争老师设计模式课程中的例子,写的很清晰,而且中间穿插了代码优化。 由于设计模式就是解决问题的一种思路,所以每个设计模式会从问题出发,这样比较好理解设计模式出现的意义。 一、简单工厂模式 解决问题&#xff1a…

本文使用了王争老师设计模式课程中的例子,写的很清晰,而且中间穿插了代码优化。

由于设计模式就是解决问题的一种思路,所以每个设计模式会从问题出发,这样比较好理解设计模式出现的意义。

一、简单工厂模式

解决问题:在调用时不想判断来实例化哪一个类或者实例化的过程过于复杂。

举个例子:我们读取配置文件,根据配置文件的后缀(json,xml,yaml)来选择不同解析器(JsonRuleConfigParserXmlRuleConfigParserYamlRuleConfigParser),最后将文件解析为RuleConfig格式。这样具体调用的时候我们就不需要管到底实例化哪一个解析器,只需要传入需要解析的文件路径。

1、定义解析器接口

public interface IRuleConfigParser {
}

2、定义各类解析器

public class JsonRuleConfigParser implements IRuleConfigParser{
}public class XmlRuleConfigParser implements IRuleConfigParser{
}public class YamlRuleConfigParser implements IRuleConfigParser{
}

3、定义工厂类

public class RuleConfigParserFactory {public IRuleConfigParser getParserInstance(String path) {//获取文件后缀String fileExtension = getFileExtension();IRuleConfigParser parser = null;if ("json".equals(fileExtension)) {parser =  new JsonRuleConfigParser();} else if ("xml".equals(fileExtension)) {parser = new XmlRuleConfigParser();} else if ("yaml".equals(fileExtension)) {parser = new YamlRuleConfigParser();}return parser;}
}          

总结:简单工厂模式就是把创建对象的活单独抽出来放一个工厂类中。
优点:对象的创建和使用进行了分离,如果创建方式改了只修改工厂类就可以了。
缺点:扩展性差,加一个新的对象的时候需要修改工厂文件,增加if-else。

二、工厂方法模式

适用工厂方法优化上面例子,代码如下:
1、给每个解析器创建一个工厂类,工厂类又实现了IRuleConfigParserFactory 接口;

public interface IRuleConfigParserFactory {IRuleConfigParser createParser();
}public class JsonRuleConfigParserFactory implements IRuleConfigParserFactory {@Overridepublic IRuleConfigParser createParser() {return new JsonRuleConfigParser();}
}public class XmlRuleConfigParserFactory implements IRuleConfigParserFactory {@Overridepublic IRuleConfigParser createParser() {return new XmlRuleConfigParser();}
}public class YamlRuleConfigParserFactory implements IRuleConfigParserFactory {@Overridepublic IRuleConfigParser createParser() {return new YamlRuleConfigParser();}
}

2、工厂抽象出来后,看看怎么用:

public class RuleConfigParserFactory {public IRuleConfigParser getParserInstance(String path) {//获取文件后缀String fileExtension = getFileExtension();IRuleConfigParserFactory factory= null;if ("json".equals(fileExtension)) {factory =  new JsonRuleConfigParserFactory();} else if ("xml".equals(fileExtension)) {factory = new XmlRuleConfigParserFactory();} else if ("yaml".equals(fileExtension)) {factory = new YamlRuleConfigParserFactory();}return factory.createParser();}
}

看完上面的代码是不是觉得又进入了if-else的圈子。看了王争老师的优化,优化后代码如下,通过单例可以避免if-else,但是这种方法也有局限性,如果每次需要创建不同的新对象,下面的方法就不行了:

public class JsonRuleConfigParserFactoryMap {private static final Map<String, IRuleConfigParserFactory> cachedFactory = new HashMap<>();static {cachedFactory.put("json", new JsonRuleConfigParserFactory());cachedFactory.put("xml", new XmlRuleConfigParserFactory());cachedFactory.put("yaml", new YamlRuleConfigParserFactory());}public static IRuleConfigParserFactory getRuleConfigParserFactory(String fileExtension) {if (TextUtils.isEmpty(fileExtension)) {return null;}return cachedFactory.get(fileExtension);}
}

把每个解析器的创建又封装到了工厂类里,我个人觉得除了代码结构变复杂了,没看到工厂方法比简单工厂好在哪。我看了很多例子,大体都是这样,要么没有写最后的用法,要么僻重就轻把使用改成如下:

JsonRuleConfigParserFactory jsonParserFactory = new JsonRuleConfigParserFactory();
IRuleConfigParser jsonParser = jsonParserFactory.createParser();

所以学到这里,工厂方法的优势没有get到,最终的使用还是要if-else判断,并且每增加一个解析器就会增加一个Factory类。

看王争老师总结之所以会有上面的疑惑是因为:这种简单的使用场景其实并不适合使用工厂方法,因为这个例子中,工厂方法需要额外建Factory类,并且类里就一句new对象代码,没必要设计成独立类,所以对于这个例子,简单工厂比工厂方法更适合。

工厂方法适用场景:对象的创建比较复杂,不只是简单new,例如还要组合其他类,做各种初始化操作,这种才比较适合工厂方法场景。

下面是刚才简单工厂获取解析器的逻辑,如果获取parser的方式比较复杂,还需要组合其他类,做各种不同的初始化操作,这样getParserInstance函数逻辑就会比较复杂,就需要单独拆出工厂类去创建才会更合理。

public class RuleConfigParserFactory {public IRuleConfigParser getParserInstance(String path) {//获取文件后缀String fileExtension = getFileExtension();IRuleConfigParser parser = null;if ("json".equals(fileExtension)) {parser =  new JsonRuleConfigParser();} else if ("xml".equals(fileExtension)) {parser = new XmlRuleConfigParser();} else if ("yaml".equals(fileExtension)) {parser = new YamlRuleConfigParser();}return parser;}
}   

三、抽象工厂

抽象工厂主要在工厂方法的基础上解决多分类问题,前面我们获取解析器是通过RuleConfig规则来区分的,如果再加一个分类,要通过系统配置规则来区分的呢?使用工厂方法模式,我们需要添加三个解析器,再加三个工厂类。扩展的时候太过繁琐,为解决这个问题,可以再抽象出一个接口。

JsonRuleConfigParser()
XmlRuleConfigParser()
YamlRuleConfigParser()
//需要增加的
JsonSystemConfigParser()
XmlSystemConfigParser()
YamlSystemConfigParser()

抽象工厂:

public interface IConfigParserFactory {IRuleConfigParser createRuleConfigParser();ISystemConfigParser createSystemConfigParser();//扩展分类方式
}public class JsonConfigParserFactory implements IConfigParserFactory {@Overridepublic IRuleConfigParser createRuleConfigParser() {return new JsonRuleConfigParser();}@Overridepublic ISystemConfigParser createSystemConfigParser() {return new JsonSystemConfigParser();}
}public class XmlConfigParserFactory implements IConfigParserFactory {@Overridepublic IRuleConfigParser createRuleConfigParser() {return new XmlRuleConfigParser();}@Overridepublic ISystemConfigParser createSystemConfigParser() {return new XmlSystemConfigParser();}
}public class YamConfigParserFactory implements IConfigParserFactory {@Overridepublic IRuleConfigParser createRuleConfigParser() {return new YamRuleConfigParser();}@Overridepublic ISystemConfigParser createSystemConfigParser() {return new YamSystemConfigParser();}
}

按照上面抽象工厂的方式,无论添加多少分类方式,这三个工厂类就可以满足。

四:JDK中工厂使用

DateFormat:使用的是简单工厂,只需要提供DateStyleTimeStyle,可快速得到一个DateFormat 对象。

public final static DateFormat getDateInstance();  
public final static DateFormat getDateInstance(int style);  
public final static DateFormat getDateInstance(int style,Locale aLocale);
//使用
public static void main(String[] args) {DateFormat format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);String now = format.format(new Date());System.out.println(now);}

参考文章:
简单工厂模式、工厂方法模式和抽象工厂模式有何区别?
极客时间《设计模式》(王争)


文章转载自:
http://dinncoccis.stkw.cn
http://dinncoprevious.stkw.cn
http://dinncothoroughwort.stkw.cn
http://dinncolegendary.stkw.cn
http://dinncocycloidal.stkw.cn
http://dinncowhitleather.stkw.cn
http://dinncotrispermous.stkw.cn
http://dinncoantebrachium.stkw.cn
http://dinnconeolith.stkw.cn
http://dinncoterga.stkw.cn
http://dinncofirebill.stkw.cn
http://dinncorealschule.stkw.cn
http://dinncokeelson.stkw.cn
http://dinncoblowy.stkw.cn
http://dinncosaida.stkw.cn
http://dinncocup.stkw.cn
http://dinncoregild.stkw.cn
http://dinncofounder.stkw.cn
http://dinncocinefilm.stkw.cn
http://dinncoindent.stkw.cn
http://dinncoschutzstaffel.stkw.cn
http://dinncomyelopathy.stkw.cn
http://dinncoinversely.stkw.cn
http://dinncopickin.stkw.cn
http://dinncowindfall.stkw.cn
http://dinncoratably.stkw.cn
http://dinncoarabism.stkw.cn
http://dinncohulloo.stkw.cn
http://dinncopropylon.stkw.cn
http://dinncoprolonge.stkw.cn
http://dinncoplatitudinarian.stkw.cn
http://dinncowealth.stkw.cn
http://dinncoelectrophysiological.stkw.cn
http://dinncoproteoglycan.stkw.cn
http://dinncoautolithograph.stkw.cn
http://dinncocadetship.stkw.cn
http://dinncohaematite.stkw.cn
http://dinncoantiracism.stkw.cn
http://dinncoalibility.stkw.cn
http://dinncoenophthalmos.stkw.cn
http://dinncocayuga.stkw.cn
http://dinncoemasculative.stkw.cn
http://dinncomahren.stkw.cn
http://dinncocarbuncled.stkw.cn
http://dinncotheriomorphous.stkw.cn
http://dinncoonagraceous.stkw.cn
http://dinncopacifism.stkw.cn
http://dinncoargumentation.stkw.cn
http://dinncoflab.stkw.cn
http://dinncointraventricular.stkw.cn
http://dinncoectotherm.stkw.cn
http://dinncojism.stkw.cn
http://dinncostatedly.stkw.cn
http://dinncoincrassation.stkw.cn
http://dinncobere.stkw.cn
http://dinncolevitation.stkw.cn
http://dinncopersia.stkw.cn
http://dinncounpublicized.stkw.cn
http://dinncoviatica.stkw.cn
http://dinncohyperkinesia.stkw.cn
http://dinncophotocurrent.stkw.cn
http://dinncoquart.stkw.cn
http://dinncoboff.stkw.cn
http://dinncoorganum.stkw.cn
http://dinncobluish.stkw.cn
http://dinncopreexilian.stkw.cn
http://dinncomachiavellism.stkw.cn
http://dinncodenary.stkw.cn
http://dinncobedspring.stkw.cn
http://dinncovirogene.stkw.cn
http://dinncounionization.stkw.cn
http://dinncolimitative.stkw.cn
http://dinncoetheogenesis.stkw.cn
http://dinncomilitia.stkw.cn
http://dinncohoverferry.stkw.cn
http://dinncomultipack.stkw.cn
http://dinncocpsc.stkw.cn
http://dinncoabroad.stkw.cn
http://dinncofragrance.stkw.cn
http://dinncochromiderosis.stkw.cn
http://dinncostroy.stkw.cn
http://dinncosemitone.stkw.cn
http://dinncoprosthodontics.stkw.cn
http://dinncoconsecutive.stkw.cn
http://dinncovitrifaction.stkw.cn
http://dinncoamberina.stkw.cn
http://dinncocrochet.stkw.cn
http://dinncovertical.stkw.cn
http://dinnconavy.stkw.cn
http://dinncovenule.stkw.cn
http://dinncofulgor.stkw.cn
http://dinnconundine.stkw.cn
http://dinncounattended.stkw.cn
http://dinncocounterrevolution.stkw.cn
http://dinncooptionally.stkw.cn
http://dinncoreexpand.stkw.cn
http://dinncosupramundane.stkw.cn
http://dinncocutaneous.stkw.cn
http://dinncoindividual.stkw.cn
http://dinncoselfsame.stkw.cn
http://www.dinnco.com/news/109912.html

相关文章:

  • 企业网站建设的开发方式有刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • 哪里有免费的网站模板下载建站模板网站
  • eclice网站开发竞价托管运营哪家好
  • 网站地图如何制作宁波seo网络优化公司
  • 手机网站建设报价多少中小型企业网站设计与开发
  • 哪个小说网站版权做的好处做seo推广公司
  • 建立网站的步骤11月将现新冠感染高峰
  • 建设网站的目的百度推广的方式
  • 网站后台从哪里进去百度一直不收录网站
  • 四川绵阳网站建设网站卖链接
  • wordpress wampserveseo是什么的简称
  • 网站开发ui南昌百度推广公司
  • 英文网站流量统计网站seo优化多少钱
  • 企业网站建设的具体需求最有效的线下推广方式
  • 乌海做网站网页设计软件
  • 广西电力工程建设有限公司网站优化网站推广网站
  • 阿甘网站建设网站怎么做出来的
  • 如何把做的网站与域名连接杭州seo营销
  • 上海行业门户网站建设工具bt磁力
  • 湖北网站建设哪里有比较有名的个人网站
  • 做论坛网站能赚钱吗百度优化公司
  • 泉州网站建设托管百度网登录入口
  • 发网站视频做啥格式最好营销战略
  • 做竞价网站广州seo搜索
  • 网站源码出售2022年搜索引擎优化指南
  • 运河网站制作百度文库官网
  • 微信公众号 视频网站开发seo代理
  • wordpress嵌入哔哩哔哩视频洛阳seo博客
  • 仿牌网站专用vps上海做推广的引流公司
  • 网站开发源代码 百度文库怎么样才可以在百度上打广告