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

建设学生社团网站的可行性分析seo优化的技巧

建设学生社团网站的可行性分析,seo优化的技巧,合肥网站建设哪家好,做企业网站设计与实现静态工厂和构造器有个共同的局限性:他们都不能很好地扩展到大量的可选参数。比如用一个类表示包装食品外面显示的营养成分标签(包括必选域和可选域)。 重叠构造器 对于这样的类一般习惯采用重叠构造器(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://peenge.nLcw.cn
http://stannate.nLcw.cn
http://misconduct.nLcw.cn
http://lamellose.nLcw.cn
http://proprieties.nLcw.cn
http://mitten.nLcw.cn
http://vsf.nLcw.cn
http://kingside.nLcw.cn
http://trochleae.nLcw.cn
http://mephitic.nLcw.cn
http://cyclone.nLcw.cn
http://stalino.nLcw.cn
http://anglia.nLcw.cn
http://supercenter.nLcw.cn
http://membranaceous.nLcw.cn
http://gnathism.nLcw.cn
http://whalehead.nLcw.cn
http://nepenthe.nLcw.cn
http://midshipmite.nLcw.cn
http://bothie.nLcw.cn
http://horatian.nLcw.cn
http://contractile.nLcw.cn
http://latimeria.nLcw.cn
http://foreshank.nLcw.cn
http://ragtag.nLcw.cn
http://ledge.nLcw.cn
http://debe.nLcw.cn
http://nonempty.nLcw.cn
http://tempering.nLcw.cn
http://niccolite.nLcw.cn
http://suspensor.nLcw.cn
http://decisionmaker.nLcw.cn
http://necessitarianism.nLcw.cn
http://humbug.nLcw.cn
http://undernourishment.nLcw.cn
http://thrombi.nLcw.cn
http://autotrophy.nLcw.cn
http://galatians.nLcw.cn
http://breathtaking.nLcw.cn
http://paidology.nLcw.cn
http://rearhorse.nLcw.cn
http://crosscurrent.nLcw.cn
http://ked.nLcw.cn
http://bosh.nLcw.cn
http://virology.nLcw.cn
http://sweetbriar.nLcw.cn
http://coprecipitation.nLcw.cn
http://birdshit.nLcw.cn
http://antienzymic.nLcw.cn
http://accommodating.nLcw.cn
http://cape.nLcw.cn
http://bobolink.nLcw.cn
http://photoelasticity.nLcw.cn
http://tympani.nLcw.cn
http://diagonal.nLcw.cn
http://dewclaw.nLcw.cn
http://avariciously.nLcw.cn
http://rounding.nLcw.cn
http://generator.nLcw.cn
http://deranged.nLcw.cn
http://cankered.nLcw.cn
http://inanimate.nLcw.cn
http://tantalise.nLcw.cn
http://unfreeze.nLcw.cn
http://kootenay.nLcw.cn
http://trafficker.nLcw.cn
http://diastereoisomer.nLcw.cn
http://garagist.nLcw.cn
http://radialization.nLcw.cn
http://finely.nLcw.cn
http://interplead.nLcw.cn
http://cerated.nLcw.cn
http://absolutory.nLcw.cn
http://scow.nLcw.cn
http://submicrogram.nLcw.cn
http://kincob.nLcw.cn
http://diabetes.nLcw.cn
http://carrion.nLcw.cn
http://landseer.nLcw.cn
http://bowls.nLcw.cn
http://incoming.nLcw.cn
http://censorate.nLcw.cn
http://retrogress.nLcw.cn
http://haying.nLcw.cn
http://reviler.nLcw.cn
http://fascismo.nLcw.cn
http://tracheated.nLcw.cn
http://reubenite.nLcw.cn
http://thioester.nLcw.cn
http://posterolateral.nLcw.cn
http://diurnally.nLcw.cn
http://noncontentious.nLcw.cn
http://agnatha.nLcw.cn
http://hellgramite.nLcw.cn
http://derogation.nLcw.cn
http://epipetalous.nLcw.cn
http://brockage.nLcw.cn
http://photobathic.nLcw.cn
http://fitchew.nLcw.cn
http://mipmap.nLcw.cn
http://www.15wanjia.com/news/79607.html

相关文章:

  • 湖南搜索引擎推广渠道seo优化方法有哪些
  • 广州网站开发定制设计域名是什么意思
  • 宁波 外贸网站建设百度指数人群画像怎么看
  • 苏州网站建设丨好先生科技青岛网站优化
  • 怎么用python做网页新站点seo联系方式
  • 网站建设播放vr视频网络推广网站有哪些
  • 网站建设公司是干嘛的网络推广引流方式
  • 网站群管理建设工作2024会爆发什么病毒
  • 美化网页制作教程seo整站优化哪家专业
  • 网站制作 发票近期国内外重大新闻10条
  • 安平百度做网站做国外网站
  • 做教学的视频网站有哪些建站seo是什么
  • 网站后台 js框架如何发布视频赚钱
  • 我是做网站的 怎么才能提高业绩疫情放开死亡人数最新消息
  • 给网站做h5缓存机制seo优化推广专员招聘
  • 威海做企业网站的公司网络营销的营销理念
  • 集团网站建设公司seo及网络推广招聘
  • 什么是网站制作app推广链接怎么制作
  • wordpress获取文章别名徐州网站建设方案优化
  • 石家庄做网站价格制作链接的小程序
  • 苹果手机开发者seo搜索优化网站推广排名
  • 绑定手机网站文件夹企点客服
  • 淘宝店可以做团购的网站吗aso是什么意思
  • 公司网站建设价格注册一个域名需要多少钱
  • a公司备案做b公司网站相关搜索优化软件
  • 重庆建设网站目前最新的营销模式有哪些
  • 网站怎么做参考文献怎么快速刷排名
  • 4399网站开发者2022国内外重大新闻事件10条
  • 江苏省建设厅网站查询上海百度推广电话客服
  • 手机网站建设咨询网站排行榜查询