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

建设学生社团网站的可行性分析seo搜索优化费用

建设学生社团网站的可行性分析,seo搜索优化费用,本地wordpress平台,在微信上怎么做网站静态工厂和构造器有个共同的局限性:他们都不能很好地扩展到大量的可选参数。比如用一个类表示包装食品外面显示的营养成分标签(包括必选域和可选域)。 重叠构造器 对于这样的类一般习惯采用重叠构造器(telescoping constructor&…

静态工厂和构造器有个共同的局限性:他们都不能很好地扩展到大量的可选参数。比如用一个类表示包装食品外面显示的营养成分标签(包括必选域和可选域)。

重叠构造器

对于这样的类一般习惯采用重叠构造器(telescoping constructor)模式,在这种模式下,提供的第一个构造器只有必要的参数,第二个构造器有一个可选的参数,第三个构造器有两个可选参数,依次类推,最后一个构造器包含所有的可选参数。

参考以下代码实现:

public class NutritionFacts{private final int servingSize;private final int servings;private final int calories;private final int fat;private final int sodium;private final int carbohydrate;public NutritionFacts(int servingSize,int servings){this(servingSize,servings,,0,0,0,0);}public NutritionFacts(int servingSize,int servings,int calories){this(servingSize,servings,,calories,0,0,0);}public NutritionFacts(int servingSize,int servings,int calories,int fat){this(servingSize,servings,,calories,fat,0,0);}public NutritionFacts(int servingSize,int servings,int calories,int fat,int sodium){this(servingSize,servings,calories,fat,sodium,0);}public NutritionFacts(int servingSize,int servings,int calories,int fat,int sodium,int carbohydrate){this.servingSize = servingSize;this.servings = servings;this.calories = calories;this.fat = fat;this.sodium = sodium;this.carbohydrate = carbohydrate;}
}

当想要创建实例的时候,利用参数中的构造器,但该列表中包含了要设置的所有参数:

NutritionFacts cocaCola = new NutritionFacts(240,8,100,0,35,27);

随着参数数目的增加,它很快就失去了控制

简而言之,重叠构造器模式可行,但是当有许多参数的时候,客户端代码会很难编写,并且仍然难以阅读

  • 如果想知道那些值是什么意思,必须仔细数着这些参数来探个究竟
  • 如果不小心颠倒了其中两个参数的顺序,编译器也不会出错,但是程序在运行时会出现错误的行为

JavaBean模式

该模式下,先调用一个无参构造器来创建对象,然后再调用setter方法来设置每个必要的参数,以及每个相关的可选参数:

public class NutritionFacts{private int servingSize;private int servings;private int calories;private int fat;private int sodium;private int carbohydrate;public NutritionFacts(){}public void setServingSize(int val){this.servingSize = val;}......
}

该模式弥补了重叠构造器模式的不足,创建实例很容易,产生的代码读起来也很容易:

NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
......

JavaBeans模式自身有着很严重的缺点。因为构造过程被分到几个调用中,在构造过程中JavaBean可能处于不一致的状态。类无法通过检验构造器参数的有效性来保证一致性。试图使用处于不一致状态的对象将会导致失败。

JavaBeans模式使得把类做成不可变的可能性不复存在。这就需要程序员付出额外的努力来确保它的线程安全。

建造者模式

它不直接生成想要的对象,而是让客户端利用所有必要的参数调用构造器(或者静态工厂)得到一个builder对象。然后客户端在builder对象上调用类似于setter的方法,来设置每个相关的可选参数,最后,客户端调用无参的build方法来生成通常不可变的对象。这个builder通常是他构建的类的静态成员类。

public class NutritionFacts{private final int servingSize;private final int servings;private final int calories;private final int fat;private final int sodium;private final int carbohydrate;public static class Builder{//必选参数private final int servingSize;private final int servings;//可选参数,初始化为0private int calories = 0;private int fat = 0;private int sodium = 0;private int carbohydrate = 0;public Builder(int servingSize,int servings){this.servingSize = servingSize;this.servings = servings;}public Builder calories(int val){this.calories = val;return this;}public Builder fat(int val){this.fat = val;return this;}public Builder sodium(int val){this.sodium = val;return this;}public Builder carbohydrate(int val){this.carbohydrate = val;return this;}public NutritionFacts build(){return new NutritionFacts(this);}}public NutritionFacts(Builder builder){servingSize = builder.servingSize;servings = builder;calories = builder.calories;fat = builder.fat;sodium = builder.sodium;carbohydrate = builder.carbohydrate;}}

NutritionFacts是不可变的,所有的默认参数值都单独放在一个地方。客户端代码:

NutritionFacts cocaCola = new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();

类层次结构Builder模式

使用平行层次结构的builder时,各自嵌套在相应的类中。抽象类有抽象的builder,具体类有具体的builder。假设用类层次根部的一个抽象类表示各式各样的比萨:

/*** @ClassName Pizza* @Author jiaxinxiao* @Date 2023/2/26 22:34*/
public abstract class Pizza {public enum Topping{HAM,MUSHROOM,ONION,PEPPER,SAUSAGE}final Set<Topping> toppings;abstract static class Builder<T extends Builder<T>>{EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);public T addTopping(Topping topping){toppings.add(Objects.requireNonNull(topping));return self();}abstract Pizza build();//子类必须实现这个方法并return thisprotected abstract T self();}Pizza(Builder<?> builder){toppings = builder.toppings.clone();}
}

这里有两个具体的Pizza子类,其中一个表示经典纽约风味,另一个表示馅料内置半月形比萨,前者需要一个尺寸参数,后者则要你指定酱汁应该内置还是外置:

/*** @ClassName NyPizza* @Author jiaxinxiao* @Date 2023/2/26 22:46*/
public class NyPizza extends Pizza{public enum Size{SMALL,MEDIUM,LARGE}private final Size size;public static class Builder extends Pizza.Builder<Builder>{private final Size size;public Builder(Size size){this.size = Objects.requireNonNull(size);}@OverrideNyPizza build() {return new NyPizza(this);}@Overrideprotected Builder self() {return this;}}private NyPizza(Builder builder) {super(builder);size = builder.size;}
}
/*** @ClassName Calzone* @Author jiaxinxiao* @Date 2023/3/4 7:14*/
public class Calzone extends Pizza{private final boolean sauceInside;public static class Builder extends Pizza.Builder<Builder>{//defaultprivate boolean sauceInside = false;public Builder sauceInside(){sauceInside = true;return this;}@OverrideCalzone build() {return new Calzone(this);}@Overrideprotected Builder self() {return this;}}private Calzone(Builder builder) {super(builder);sauceInside = builder.sauceInside;}
}

每个子类的构建器中的build方法,都声明返回正确的子类:NyPizza.Builder的build方法返回NyPizza,而Calzon.Builder中的则返回Calzone。在该方法中,子类方法声明返回超级类中声明的返回类型的子类型,这被称作协变返回类型。它允许客户端无需转换类型就能使用这些构建器。

客户端使用:

NyPizza pizza = new NyPizza.Builder(Size.SMALL).addTopping(Topping.SAUSAGE).addTopping(Topping.ONION).build();
Calzone calzone = new Calzone.Builder().addTopping(Topping.HAM).sauceInside().build();

与构造器相比,builder的微弱优势在于,它可以有多个可变(varargs)参数。因为builder是利用单独的方法来设置每一个参数。此外,构建器还可以将多次调用某一个方法而传入的参数集中到一个域中,如调用两次的addTopping方法。

Builder模式的不足

为了创建对象,必须先创建它的构建器。虽然创建这个构建器的开销在实践中可能不那么明显,但是在某些十分注重性能的情况下,可能就成问题了。

Builder模式还比重叠构造器模式更加冗长,因此它只在有很多参数的时候才使用,比如4个或者更多的参数。

总结

如果类的构造器或者静态工厂中具有多个参数,设计这种类时,Builder模式就是一种不错的选择,特别是当大多数参数都是可选或者类型相同的时候。与使用重叠构造器模式相比,使用Builder模式的客户端代码将更易于阅读和编写,构建器也比JavaBean更加安全。


文章转载自:
http://higgler.wwxg.cn
http://swatter.wwxg.cn
http://mortling.wwxg.cn
http://razorback.wwxg.cn
http://quenton.wwxg.cn
http://acousticon.wwxg.cn
http://octose.wwxg.cn
http://distemperedness.wwxg.cn
http://unforested.wwxg.cn
http://distraite.wwxg.cn
http://researchful.wwxg.cn
http://hypnotic.wwxg.cn
http://intermediator.wwxg.cn
http://strasbourg.wwxg.cn
http://bung.wwxg.cn
http://leonid.wwxg.cn
http://quavering.wwxg.cn
http://twig.wwxg.cn
http://bauxitic.wwxg.cn
http://hellbender.wwxg.cn
http://lav.wwxg.cn
http://phagophobia.wwxg.cn
http://vellicate.wwxg.cn
http://coziness.wwxg.cn
http://immunochemist.wwxg.cn
http://rimbaldian.wwxg.cn
http://stalagmometer.wwxg.cn
http://backwards.wwxg.cn
http://betcha.wwxg.cn
http://sannup.wwxg.cn
http://methadon.wwxg.cn
http://socially.wwxg.cn
http://trawl.wwxg.cn
http://odiously.wwxg.cn
http://limberneck.wwxg.cn
http://fumigate.wwxg.cn
http://bitstock.wwxg.cn
http://dowser.wwxg.cn
http://shortstop.wwxg.cn
http://scrag.wwxg.cn
http://cecity.wwxg.cn
http://leucocythemia.wwxg.cn
http://laborism.wwxg.cn
http://haptoglobin.wwxg.cn
http://liber.wwxg.cn
http://renascence.wwxg.cn
http://stolon.wwxg.cn
http://hunnish.wwxg.cn
http://iatrical.wwxg.cn
http://paletot.wwxg.cn
http://bacteriform.wwxg.cn
http://egotize.wwxg.cn
http://gregarious.wwxg.cn
http://largest.wwxg.cn
http://cestode.wwxg.cn
http://catecheticel.wwxg.cn
http://foredeck.wwxg.cn
http://bergamasque.wwxg.cn
http://efflorescent.wwxg.cn
http://weariness.wwxg.cn
http://gynaecic.wwxg.cn
http://lockmaking.wwxg.cn
http://politer.wwxg.cn
http://jellaba.wwxg.cn
http://supposing.wwxg.cn
http://augmented.wwxg.cn
http://dionysus.wwxg.cn
http://price.wwxg.cn
http://schnitzel.wwxg.cn
http://actualization.wwxg.cn
http://ho.wwxg.cn
http://classbook.wwxg.cn
http://ssg.wwxg.cn
http://onychophagia.wwxg.cn
http://baotou.wwxg.cn
http://incorrectness.wwxg.cn
http://cytrel.wwxg.cn
http://sergeanty.wwxg.cn
http://pentastylos.wwxg.cn
http://flavorous.wwxg.cn
http://vmi.wwxg.cn
http://pretubercular.wwxg.cn
http://lincolnesque.wwxg.cn
http://vinylon.wwxg.cn
http://exempligratia.wwxg.cn
http://qktp.wwxg.cn
http://hydrosulfide.wwxg.cn
http://nebelwerfer.wwxg.cn
http://cattlelifter.wwxg.cn
http://preventer.wwxg.cn
http://sank.wwxg.cn
http://sublunary.wwxg.cn
http://blowtorch.wwxg.cn
http://heimisch.wwxg.cn
http://significative.wwxg.cn
http://fascicled.wwxg.cn
http://citronella.wwxg.cn
http://bellman.wwxg.cn
http://catabolic.wwxg.cn
http://hottish.wwxg.cn
http://www.hrbkazy.com/news/63023.html

相关文章:

  • 网站如何做微信支付链接兰州网络推广电话
  • 郑州做网站公司电话苏州网站建设公司排名
  • 江门做公司网站制作网站的公司有哪些
  • 网站建设组织机构百度一下官方下载安装
  • 厦门网站建设公司推荐东莞seo广告宣传
  • 口碑好的秦皇岛网站建设哪家好深圳网页设计
  • 360免费建站模板接app推广接单平台
  • 网站进入沙盒的表现打广告推广怎么做
  • 做网站怎样让字体滚动浙江百度查关键词排名
  • 星子网微庐山windows优化软件排行
  • 银川网站建设有哪些seo的作用是什么
  • 网站分屏布局设计今日国际新闻最新消息
  • 做网站 需要什么样的服务器seo新手快速入门
  • 诸城网站开发今日足球赛事分析推荐
  • 专门做搞笑游戏视频网站免费seo在线工具
  • 成都高级网站建设项目推广平台排行榜
  • 网站一元空间有哪些呀枣庄网络推广seo
  • 虚拟主机与网站建设广东seo点击排名软件哪家好
  • 一个服务器可以建几个网站软文营销广告
  • 属于b2b的网站免费推广网站
  • 下载网站系统源码今日重大新闻头条财经
  • 计算机机应用网站建设与维护seo快速排名点击
  • 房产网站关键词优化做app软件大概多少钱
  • 步骤的英文南宁seo优化
  • ftp怎么上传文件到网站seo咨询常德
  • 网站建设结课java培训机构
  • 抚州网站推广苹果被曝开发搜索引擎对标谷歌
  • it运维搜索引擎优化好做吗
  • 做一个网站要怎么做百度点击率排名有效果吗
  • 微信公众号链接网站怎么做百度首页网站推广多少钱一年