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

芜湖网站建设百度推广有哪些推广方式

芜湖网站建设,百度推广有哪些推广方式,做移动网站优化首,安龙网站建设建造者模式 概述 将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。 分离了部件的构造(由Builder来负责)和装配(由Director负责)。 从而可以构造出复杂的对象。这个模式适用于:某个对象的构建过程复杂的情况。由于实现了构建和…

建造者模式

概述
将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。
在这里插入图片描述

  • 分离了部件的构造(由Builder来负责)和装配(由Director负责)。 从而可以构造出复杂的对象。这个模式适用于:某个对象的构建过程复杂的情况。
  • 由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。
  • 建造者模式可以将部件和其组装过程分开,一步一步创建一个复杂的对象。用户只需要指定复杂对象的类型就可以得到该对象,而无须知道其内部的具体构造细节。

结构

建造者(Builder)模式包含如下角色:

  • 抽象建造者类(Builder):这个接口规定要实现复杂对象的那些部分的创建,并不涉及具体的部件对象的创建。
  • 具体建造者类(ConcreteBuilder):实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。在构造过程完成后,提供产品的实例。
  • 产品类(Product):要创建的复杂对象。
  • 指挥者类(Director):调用具体建造者来创建复杂对象的各个部分,在指导者中不涉及具体产品的信息,只负责保证对象各部分完整创建或按某种顺序创建。

类图如下:
在这里插入图片描述
实例

创建共享单车

生产自行车是一个复杂的过程,它包含了车架,车座等组件的生产。而车架又有碳纤维,铝合金等材质的,车座有橡胶,真皮等材质。对于自行车的生产就可以使用建造者模式。

这里Bike是产品,包含车架,车座等组件;Builder是抽象建造者,MobikeBuilder和OfoBuilder是具体的建造者;Director是指挥者。类图如下:
在这里插入图片描述
具体的代码如下:

//自行车类
public class Bike {private String frame;private String seat;public String getFrame() {return frame;}public void setFrame(String frame) {this.frame = frame;}public String getSeat() {return seat;}public void setSeat(String seat) {this.seat = seat;}
}// 抽象 builder 类
public abstract class Builder {//子类也可以正常使用protected Bike mBike = new Bike();public abstract void buildFrame();public abstract void buildSeat();public abstract Bike createBike();
}//摩拜单车Builder类
public class MobikeBuilder extends Builder {@Overridepublic void buildFrame() {mBike.setFrame("铝合金车架");}@Overridepublic void buildSeat() {mBike.setSeat("真皮车座");}@Overridepublic Bike createBike() {return mBike;}
}//ofo单车Builder类
public class OfoBuilder extends Builder {@Overridepublic void buildFrame() {mBike.setFrame("碳纤维车架");}@Overridepublic void buildSeat() {mBike.setSeat("橡胶车座");}@Overridepublic Bike createBike() {return mBike;}
}//指挥者类
public class Director {private Builder mBuilder;public Director(Builder builder) {mBuilder = builder;}public Bike construct() {mBuilder.buildFrame();mBuilder.buildSeat();return mBuilder.createBike();}
}//测试类
public class Client {public static void main(String[] args) {showBike(new OfoBuilder());showBike(new MobikeBuilder());}private static void showBike(Builder builder) {Director director = new Director(builder);Bike bike = director.construct();System.out.println(bike.getFrame());System.out.println(bike.getSeat());}
}

在这里插入图片描述
注意:

上面示例是 Builder模式的常规用法,指挥者类 Director 在建造者模式中具有很重要的作用,它用于指导具体构建者如何构建产品,控制调用先后次序,并向调用者返回完整的产品类,但是有些情况下需要简化系统结构,可以把指挥者类和抽象建造者进行结合

// 抽象 builder 类
public abstract class Builder {protected Bike mBike = new Bike();public abstract void buildFrame();public abstract void buildSeat();public abstract Bike createBike();public Bike construct() {this.buildFrame();this.buildSeat();return this.createBike();}
}

说明:

这样做确实简化了系统结构,但同时也加重了抽象建造者类的职责,也不是太符合单一职责原则,如果construct() 过于复杂,建议还是封装到 Director 中。
优缺点

优点:

  • 建造者模式的封装性很好。使用建造者模式可以有效的封装变化,在使用建造者模式的场景中,一般产品类和建造者类是比较稳定的,因此,将主要的业务逻辑封装在指挥者类中对整体而言可以取得比较好的稳定性。
  • 在建造者模式中,客户端不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦,使得相同的创建过程可以创建不同的产品对象。
  • 可以更加精细地控制产品的创建过程 。将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰,也更方便使用程序来控制创建过程。
  • 建造者模式很容易进行扩展。如果有新的需求,通过实现一个新的建造者类就可以完成,基本上不用修改之前已经测试通过的代码,因此也就不会对原有功能引入风险。符合开闭原则。

缺点:

造者模式所创建的产品一般具有较多的共同点,其组成部分相似,如果产品之间的差异性很大,则不适合使用建造者模式,因此其使用范围受到一定的限制。

使用场景

建造者(Builder)模式创建的是复杂对象,其产品的各个部分经常面临着剧烈的变化,但将它们组合在一起的算法却相对稳定,所以它通常在以下场合使用。

  • 创建的对象较复杂,由多个部件构成,各部件面临着复杂的变化,但构件间的建造顺序是稳定的。
  • 创建复杂对象的算法独立于该对象的组成部分以及它们的装配方式,即产品的构建过程和最终的表示是独立的。

模式扩展

建造者模式除了上面的用途外,在开发中还有一个常用的使用方式,就是当一个类构造器需要传入很多参数时,如果创建这个类的实例,代码可读性会非常差,而且很容易引入错误,此时就可以利用建造者模式进行重构。

重构前代码如下:

public class Phone {private String cpu;private String screen;private String memory;private String mainboard;public Phone(String cpu, String screen, String memory, String mainboard) {this.cpu = cpu;this.screen = screen;this.memory = memory;this.mainboard = mainboard;}public String getCpu() {return cpu;}public void setCpu(String cpu) {this.cpu = cpu;}public String getScreen() {return screen;}public void setScreen(String screen) {this.screen = screen;}public String getMemory() {return memory;}public void setMemory(String memory) {this.memory = memory;}public String getMainboard() {return mainboard;}public void setMainboard(String mainboard) {this.mainboard = mainboard;}@Overridepublic String toString() {return "Phone{" +"cpu='" + cpu + '\'' +", screen='" + screen + '\'' +", memory='" + memory + '\'' +", mainboard='" + mainboard + '\'' +'}';}
}public class Client {public static void main(String[] args) {//构建Phone对象Phone phone = new Phone("intel","三星屏幕","金士顿","华硕");System.out.println(phone);}
}

上面在客户端代码中构建Phone对象,传递了四个参数,如果参数更多呢?代码的可读性及使用的成本就是比较高。

重构后代码:

public class Phone {private String cpu;private String screen;private String memory;private String mainboard;//私有构造方法private Phone(Builder builder) {cpu = builder.cpu;screen = builder.screen;memory = builder.memory;mainboard = builder.mainboard;}public static final class Builder {private String cpu;private String screen;private String memory;private String mainboard;public Builder() {}public Builder cpu(String val) {cpu = val;return this;}public Builder screen(String val) {screen = val;return this;}public Builder memory(String val) {memory = val;return this;}public Builder mainboard(String val) {mainboard = val;return this;}public Phone build() {return new Phone(this);}//内部类可以访问外部类私有方法}@Overridepublic String toString() {return "Phone{" +"cpu='" + cpu + '\'' +", screen='" + screen + '\'' +", memory='" + memory + '\'' +", mainboard='" + mainboard + '\'' +'}';}
}public class Client {public static void main(String[] args) {//通过构建中对象获取手机对象Phone phone = new Phone.Builder().cpu("intel").mainboard("华硕").memory("金士顿").screen("三星").build();System.out.println(phone);}
}

重构后的代码在使用起来更方便,某种程度上也可以提高开发效率。从软件设计上,对程序员的要求比较高。


文章转载自:
http://dinncocoup.stkw.cn
http://dinncolockmaker.stkw.cn
http://dinncoafterword.stkw.cn
http://dinncoinsectival.stkw.cn
http://dinncoddk.stkw.cn
http://dinncocelticize.stkw.cn
http://dinncoquadriphony.stkw.cn
http://dinncohebdomadary.stkw.cn
http://dinncolz.stkw.cn
http://dinncoprocessing.stkw.cn
http://dinncobrassie.stkw.cn
http://dinncohadrosaur.stkw.cn
http://dinncounlikelihood.stkw.cn
http://dinncoroscoe.stkw.cn
http://dinncobimetal.stkw.cn
http://dinncoisallobar.stkw.cn
http://dinncolibriform.stkw.cn
http://dinncobasalt.stkw.cn
http://dinncotrisomic.stkw.cn
http://dinncolowball.stkw.cn
http://dinncocursely.stkw.cn
http://dinncoanimalculum.stkw.cn
http://dinncotepefy.stkw.cn
http://dinncoflaxen.stkw.cn
http://dinncokrimmer.stkw.cn
http://dinncoeducate.stkw.cn
http://dinncobagdad.stkw.cn
http://dinncovinyon.stkw.cn
http://dinncogossip.stkw.cn
http://dinncohosier.stkw.cn
http://dinncointrospection.stkw.cn
http://dinncoacetylide.stkw.cn
http://dinncodelirifacient.stkw.cn
http://dinncoheterogynous.stkw.cn
http://dinncocaramelise.stkw.cn
http://dinncoirregularly.stkw.cn
http://dinncosocialize.stkw.cn
http://dinncomatrimony.stkw.cn
http://dinncorbs.stkw.cn
http://dinncogymnocarpous.stkw.cn
http://dinncoanthocyanin.stkw.cn
http://dinncobirdfarm.stkw.cn
http://dinncosussy.stkw.cn
http://dinncoformidable.stkw.cn
http://dinncoitn.stkw.cn
http://dinncoflagrantly.stkw.cn
http://dinncosockdolager.stkw.cn
http://dinncoplatypodia.stkw.cn
http://dinncowinery.stkw.cn
http://dinncoglottalic.stkw.cn
http://dinncobeijing.stkw.cn
http://dinncosulphide.stkw.cn
http://dinncopurgatory.stkw.cn
http://dinncotelerecording.stkw.cn
http://dinncopantologic.stkw.cn
http://dinncoprovident.stkw.cn
http://dinncoswordbearer.stkw.cn
http://dinncononreactive.stkw.cn
http://dinncorusticity.stkw.cn
http://dinncodamselfly.stkw.cn
http://dinncogumweed.stkw.cn
http://dinncoslaggy.stkw.cn
http://dinncogcf.stkw.cn
http://dinncoisodynamic.stkw.cn
http://dinncotheodicean.stkw.cn
http://dinncocataphracted.stkw.cn
http://dinncohowl.stkw.cn
http://dinncogemutlich.stkw.cn
http://dinncotrondhjem.stkw.cn
http://dinncocryoscopic.stkw.cn
http://dinncotephroite.stkw.cn
http://dinncopreappoint.stkw.cn
http://dinncounquarried.stkw.cn
http://dinncosericitization.stkw.cn
http://dinncoegregiously.stkw.cn
http://dinncomurices.stkw.cn
http://dinncorejuvenation.stkw.cn
http://dinncosantera.stkw.cn
http://dinncodesignator.stkw.cn
http://dinncoincandescent.stkw.cn
http://dinncolocational.stkw.cn
http://dinncooscillogram.stkw.cn
http://dinncoproteinous.stkw.cn
http://dinncoclyster.stkw.cn
http://dinnconudzh.stkw.cn
http://dinncosantour.stkw.cn
http://dinncotelephone.stkw.cn
http://dinncomutarotase.stkw.cn
http://dinncogyges.stkw.cn
http://dinncomaturate.stkw.cn
http://dinncoacharnement.stkw.cn
http://dinncopianoforte.stkw.cn
http://dinncooffscouring.stkw.cn
http://dinncoamniocentesis.stkw.cn
http://dinncoinseam.stkw.cn
http://dinncoringtaw.stkw.cn
http://dinncosulfazin.stkw.cn
http://dinncogave.stkw.cn
http://dinncocruise.stkw.cn
http://dinncotypy.stkw.cn
http://www.dinnco.com/news/114697.html

相关文章:

  • 做自动采集电影网站有什么处罚seo结算系统
  • 网站营销是什么意思怎么交换友情链接
  • 免费的成品网站百度关键词排名代做
  • 云服务器是否可以做多个网站域名注册好了怎么弄网站
  • wordpress改成织梦seo入门培训教程
  • 网站建设网站目的模板岳阳网站建设推广
  • 网站 系统 区别免费的黄冈网站代码
  • wordpress 钩子的好处windows优化大师是什么软件
  • wordpress免插件图床360网站seo手机优化软件
  • 机械营销网站建设案例怎么制作一个网站5个网页
  • 广东响应式网站全专业优化公司
  • 企业网站建设太原网站建设营销型网站内容
  • 河南省网站建设免费网址注册
  • code编程网站百度指数官网入口
  • 中企动力近期做的网站seo关键词优化怎么做
  • 南山网站建设公司seo成都培训
  • php淘客网站开发制作网站的步骤是什么
  • 做食品网站网店代运营正规公司
  • 小猫济南网站建设公司中国企业网络营销现状
  • 中卫平面设计师招聘宁波seo链接优化
  • 湖北建设厅造价网站百度推广运营怎么做
  • 官方网站开发哪家便宜在线网站建设平台
  • 网站做百度地图定位seo技巧分享
  • 网站301跳转效果市场营销推广方案模板
  • 59做网站广告词
  • 地址 上海石门二路 网站建设今日头条新闻最全新消息
  • 景宁建设局网站官网临沂seo公司稳健火星
  • 上海网站建设q479185700棒产品推广运营的公司
  • 济南大型网站设计公司seo优化网站教程百度
  • 学校网站备案怎么做培训心得体会200字