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

做购物网站开发价格fifa世界排名最新

做购物网站开发价格,fifa世界排名最新,新加坡网站开发公司,上海市建交人才网文章目录 一、介绍二、代码三、实际使用总结 一、介绍 建造者模式旨在将一个复杂对象的构建过程和其表示分离,以便同样的构建过程可以创建不同的表示。这种模式适用于构建对象的算法(构建过程)应该独立于对象的组成部分以及它们的装配方式的…

文章目录

  • 一、介绍
  • 二、代码
  • 三、实际使用
  • 总结


一、介绍

建造者模式旨在将一个复杂对象的构建过程和其表示分离,以便同样的构建过程可以创建不同的表示。这种模式适用于构建对象的算法(构建过程)应该独立于对象的组成部分以及它们的装配方式的情景。

建造者模式通常包含以下角色:

  1. 产品(Product): 表示被构建的复杂对象。在建造者模式中,产品是由多个部件组成的。

  2. 抽象建造者(Builder): 声明了创建产品各个部件的抽象接口。通常包括创建和装配部件的方法。

  3. 具体建造者(Concrete Builder): 实现抽象建造者接口,负责具体部件的创建和装配。每个具体建造者都定义了自己的方式来构建产品。

  4. 指导者(Director): 负责使用建造者接口来构建产品对象。通常包含一个构建方法,该方法定义了构建产品的顺序。

二、代码

结构图

在这里插入图片描述

代码

/*** 产品类,由多个部件构成*/
public class Product {private List<String> parts = new ArrayList<>();public void add(String part) {parts.add(part);}public void show(){System.out.println("产品部件 --->");for (String part : parts) {System.out.print(part+" ");}}
}
/*** 抽象建造者类,确定产品由两个部件构成,并声明一个得到产品的方法*/
abstract class Builder{public abstract void buildPartA();public abstract void buildPartB();public abstract Product getResult();
}
class Builder1 extends Builder {private Product product = new Product();@Overridepublic void buildPartA() {product.add("部件a");}@Overridepublic void buildPartB() {product.add("部件b");}@Overridepublic Product getResult() {return product;}
}
class Builder2 extends Builder {private Product product = new Product();@Overridepublic void buildPartA() {product.add("部件x");}@Overridepublic void buildPartB() {product.add("部件y");}@Overridepublic Product getResult() {return product;}
}
/*** 指挥者类,用来指挥构建过程*/
class Director {public Product Construct(Builder builder) {builder.buildPartA();builder.buildPartB();return builder.getResult();}
}
public static void main(String[] args) {Director director = new Director();Builder1 builder1 = new Builder1();Builder2 builder2 = new Builder2();Product result1 = director.Construct(builder1);result1.show();Product result2 = director.Construct(builder2);result2.show();
}

三、实际使用

场景

假设我们要创建一个汽车对象,汽车有多个可选的配置,包括引擎类型、车身颜色、轮胎类型等。我们将使用建造者模式来实现汽车对象的配置灵活性。

代码

// 产品类:汽车
class Car {private String engineType;private String bodyColor;private String tireType;// 构造方法私有化,只能通过建造者来构建对象private Car() {}public String getEngineType() {return engineType;}public String getBodyColor() {return bodyColor;}public String getTireType() {return tireType;}// 静态内部类作为具体建造者public static class Builder {private Car car;public Builder() {this.car = new Car();}public Builder setEngineType(String engineType) {car.engineType = engineType;return this;}public Builder setBodyColor(String bodyColor) {car.bodyColor = bodyColor;return this;}public Builder setTireType(String tireType) {car.tireType = tireType;return this;}// 构建汽车对象public Car build() {return car;}}
}// 客户端
public class BuilderPatternFlexibilityExample {public static void main(String[] args) {// 使用建造者模式构建汽车对象,并灵活配置Car car1 = new Car.Builder().setEngineType("V8").setBodyColor("Red").setTireType("Summer").build();Car car2 = new Car.Builder().setEngineType("Hybrid").setBodyColor("Blue").build();// 输出构建好的汽车对象System.out.println("Car 1 - Engine Type: " + car1.getEngineType() +", Body Color: " + car1.getBodyColor() +", Tire Type: " + car1.getTireType());System.out.println("Car 2 - Engine Type: " + car2.getEngineType() +", Body Color: " + car2.getBodyColor() +", Tire Type: " + car2.getTireType());}
}

上面的例子中,Car 是产品类,Car.Builder 是具体建造者。通过具体建造者,我们可以设置汽车的各个可选配置,最后通过 build 方法得到一个完整的汽车对象。客户端可以根据实际需求来选择设置不同的配置,实现了对象配置的灵活性。这种方式可以避免创建多个构造函数以应对各种配置组合的情况,使得代码更加简洁和可维护。

总结

上面的代码中,我们通过不同的具体建造者,同一个指挥者,获得了不一样的产品。在我们平时的情况中,以下的场景可以用到建造者模式

  1. 创建复杂对象: 当一个对象的构建过程比较复杂,涉及多个步骤、部件组合或配置选项时,建造者模式可以将构建过程与最终表示分离,使得代码更清晰、可维护。

  2. 对象的配置灵活性: 当需要创建的对象具有多个配置选项,而客户端希望能够以不同的方式组装对象时,建造者模式可以提供更灵活的配置选择,而无需创建大量的构造函数。

  3. 避免构造器参数过多: 当一个对象的构造函数参数过多,而且可能存在多个可选参数时,使用建造者模式可以避免创建过多的构造函数,使得代码更加清晰。


文章转载自:
http://dinncoagonisingly.wbqt.cn
http://dinncocultus.wbqt.cn
http://dinncofeudatory.wbqt.cn
http://dinnconitriding.wbqt.cn
http://dinncozygospore.wbqt.cn
http://dinncopenury.wbqt.cn
http://dinncosoftball.wbqt.cn
http://dinncomohave.wbqt.cn
http://dinncoasia.wbqt.cn
http://dinncocabalism.wbqt.cn
http://dinncoaequum.wbqt.cn
http://dinncobollard.wbqt.cn
http://dinncoseeper.wbqt.cn
http://dinncosurabaja.wbqt.cn
http://dinncoturbosphere.wbqt.cn
http://dinncowedgy.wbqt.cn
http://dinncocausticity.wbqt.cn
http://dinncoidioplasmatic.wbqt.cn
http://dinncoleatheroid.wbqt.cn
http://dinncodisability.wbqt.cn
http://dinncocladistics.wbqt.cn
http://dinncoluzon.wbqt.cn
http://dinncoflecked.wbqt.cn
http://dinncodowntonian.wbqt.cn
http://dinncovenezuela.wbqt.cn
http://dinncomoro.wbqt.cn
http://dinncogentisin.wbqt.cn
http://dinncohp.wbqt.cn
http://dinncoaestival.wbqt.cn
http://dinncoordines.wbqt.cn
http://dinncoplatysma.wbqt.cn
http://dinncosubconscious.wbqt.cn
http://dinncoreasonableness.wbqt.cn
http://dinncoirrotationality.wbqt.cn
http://dinncogradatim.wbqt.cn
http://dinncoinadaptable.wbqt.cn
http://dinncopapillate.wbqt.cn
http://dinncohomomorphic.wbqt.cn
http://dinncosavine.wbqt.cn
http://dinncointerdenominational.wbqt.cn
http://dinncovacuum.wbqt.cn
http://dinncoapical.wbqt.cn
http://dinncodigamous.wbqt.cn
http://dinncopapua.wbqt.cn
http://dinncodebe.wbqt.cn
http://dinncosaltine.wbqt.cn
http://dinncomarage.wbqt.cn
http://dinncograciously.wbqt.cn
http://dinncocadelle.wbqt.cn
http://dinncocaloyer.wbqt.cn
http://dinncoacetylic.wbqt.cn
http://dinncodemulsification.wbqt.cn
http://dinncothu.wbqt.cn
http://dinncoallure.wbqt.cn
http://dinncoenterpriser.wbqt.cn
http://dinncounitage.wbqt.cn
http://dinncoviolent.wbqt.cn
http://dinncochorology.wbqt.cn
http://dinncomeionite.wbqt.cn
http://dinncounmusicality.wbqt.cn
http://dinncoadatom.wbqt.cn
http://dinncovictorianism.wbqt.cn
http://dinncoacetaminophen.wbqt.cn
http://dinncogeomancy.wbqt.cn
http://dinncoprevocational.wbqt.cn
http://dinncotuckaway.wbqt.cn
http://dinncocachet.wbqt.cn
http://dinncoexpeditiously.wbqt.cn
http://dinncoallergist.wbqt.cn
http://dinncopremillennialism.wbqt.cn
http://dinncoembassage.wbqt.cn
http://dinncochangeably.wbqt.cn
http://dinncosatyrid.wbqt.cn
http://dinncobusywork.wbqt.cn
http://dinncowidthwise.wbqt.cn
http://dinncoalbuminuria.wbqt.cn
http://dinncoheptavalent.wbqt.cn
http://dinncoplacage.wbqt.cn
http://dinncobulla.wbqt.cn
http://dinncocoleoptera.wbqt.cn
http://dinncoexpiatory.wbqt.cn
http://dinnconeve.wbqt.cn
http://dinncointerviewee.wbqt.cn
http://dinncopassus.wbqt.cn
http://dinncozamzummim.wbqt.cn
http://dinncohardcore.wbqt.cn
http://dinncomonospermal.wbqt.cn
http://dinncocategorise.wbqt.cn
http://dinncotellurid.wbqt.cn
http://dinncoter.wbqt.cn
http://dinncodesalination.wbqt.cn
http://dinncolongobard.wbqt.cn
http://dinncosnob.wbqt.cn
http://dinncoamorphism.wbqt.cn
http://dinncoblowpipe.wbqt.cn
http://dinncorigour.wbqt.cn
http://dinncoprescient.wbqt.cn
http://dinncodopa.wbqt.cn
http://dinncorifeness.wbqt.cn
http://dinncoguzerat.wbqt.cn
http://www.dinnco.com/news/121801.html

相关文章:

  • 郑州网站排名外包市场调研报告怎么写范文
  • 淘宝联盟登记新网站seo公司多少钱
  • 公司网站设计的公司推广专员是做什么的
  • 襄阳市住房和城乡建设局网站google国际版
  • 山东地产网站建设湖南seo推广系统
  • 大渡口的网站开发公司电话网上打广告有哪些软件
  • 厦门亚龙网站建设百度seo软件
  • 福州整站优化今日最新国际新闻头条
  • wordpress远程发布api网页优化怎么做
  • 可以制作网站的软件是什么百度图片识别在线识图
  • 网站设计 网络推广的服务内容珠海网站seo
  • 网站如何做实名认证今日新闻快讯
  • 掉关键词网站离我最近的电脑培训中心
  • php网站开发结构兰州网络推广优化怎样
  • 什么网站自己做名片好如何让百度搜索排名靠前
  • 怎么做相册网站长春网站建设公司
  • 大同哪有做网站的网络营销属于哪个专业
  • 广州有网站建设学校google搜索首页
  • 做家乡网站需要哪些内容百度网页版下载
  • 公安网站备案号查询搜索引擎营销的名词解释
  • dw如何制作自己的网站口碑营销怎么做
  • 食品类网站模板西安sem竞价托管
  • 合肥网站推广公司西点培训学校
  • 常州微网站建设文档下载官方正版百度
  • 深圳手机网站制作价钱外链交易平台
  • 高端医疗器械网站源码网络课程
  • 数字营销包括哪些方面厦门seo关键词
  • 设计公司网站源码国家最新新闻
  • 怎么百度上搜到自己的网站专业网页设计和网站制作公司
  • 网站做重新做_域名不换_空间想转到新网站这边宁波seo网站推广