设计模式-建造者模式(一)

来源: 腾讯云 时间:2023-05-04 09:38:30


(相关资料图)

建造者模式(Builder Pattern)是一种创建型设计模式,它可以将对象的构建过程分离出来,使得同样的构建过程可以创建不同的表示。在该模式中,一个类负责对象的创建,并将对象的创建过程分解为多个步骤,每个步骤都由一个独立的建造者类来实现。最终通过指导这些步骤来组装出一个完整的对象。

在实际开发中,建造者模式常被用于构建复杂的对象,这些对象可能有多个属性需要初始化,而且这些属性之间可能有复杂的依赖关系。使用建造者模式可以避免在代码中直接实例化对象时需要传入大量的参数,并可以保证对象构建的一致性。

下面我们通过一个简单的例子来说明建造者模式的使用。

假设我们需要构建一个汽车对象,这个汽车对象包含以下属性:车牌号、车型、颜色、引擎、轮胎、座椅等。我们可以按照以下步骤来构建汽车对象:

创建汽车对象的抽象类Car,定义Car对象的基本属性和方法,如下所示:

javaCopy codepublic abstract class Car {    protected String licensePlateNumber;    protected String model;    protected String color;    protected String engine;    protected String tires;    protected String seats;    public abstract void setLicensePlateNumber(String licensePlateNumber);    public abstract void setModel(String model);    public abstract void setColor(String color);    public abstract void setEngine(String engine);    public abstract void setTires(String tires);    public abstract void setSeats(String seats);    public abstract String getDescription();}

创建汽车对象的具体实现类,如SedanCar、SUVCar、SportsCar等,实现Car对象的抽象方法,如下所示:

public class SedanCar extends Car {    public SedanCar() {        this.licensePlateNumber = "";        this.model = "";        this.color = "";        this.engine = "";        this.tires = "";        this.seats = "";    }    public void setLicensePlateNumber(String licensePlateNumber) {        this.licensePlateNumber = licensePlateNumber;    }    public void setModel(String model) {        this.model = model;    }    public void setColor(String color) {        this.color = color;    }    public void setEngine(String engine) {        this.engine = engine;    }    public void setTires(String tires) {        this.tires = tires;    }    public void setSeats(String seats) {        this.seats = seats;    }    public String getDescription() {        return "This is a sedan car with license plate number " + licensePlateNumber + ", model " + model + ", color " + color + ", engine " + engine + ", tires " + tires + ", seats " + seats + ".";    }}
public class SUVCar extends Car {    public SUVCar() {        this.licensePlateNumber = "";        this.model = "";        this.color = "";        this.engine = "";        this.tires = "";        this.seats = "";    }    public void setLicensePlateNumber(String licensePlateNumber) {        this.licensePlateNumber = licensePlateNumber;    }    public void setModel(String model) {        this.model = model;    }    public void setColor(String color) {        this.color = color;    }    public void setEngine(String engine) {        this.engine = engine;    }    public void setTires(String tires) {        this.tires = tires;    }    public void setSeats(String seats) {        this.seats = seats;    }    public String getDescription() {        return "This is an SUV car with license plate number " + licensePlateNumber + ", model " + model + ", color " + color + ", engine " + engine + ", tires " + tires + ", seats " + seats + ".";    }}

X 关闭

热门推荐

InitContainer|天天热讯

2023-05-04   腾讯云

中核集团发布2022年企业社会责任报告

2023-05-04   中国产业经济信息网

补可以组什么词_补的组词

2023-05-04   万能网

kubernetes-AntiAffinity_世界动态

2023-05-04   腾讯云

为你点赞!北京青年的闪亮青春

2023-05-04   北京日报客户端

kubernetes-AntiAffinity

2023-05-04   腾讯云

Taint和Toleration

2023-05-04   腾讯云

kubernetes-AntiAffinity_天天讯息

2023-05-04   腾讯云

kubernetes-Affinity示例

2023-05-04   腾讯云

Taint和Toleration-焦点热讯

2023-05-03   腾讯云

栀子花_柢

2023-05-03   互联网

世界新动态:Taint和Toleration

2023-05-03   腾讯云

Taint命令入门

2023-05-03   腾讯云

热的组词_热什么意思

2023-05-03   万能网

出实样啦!昌平这处安置房明年交房

2023-05-03   咱昌平的事儿

杀虫剂使用不当,易爆燃伤人

2023-05-03   北京青年报官网

kubernetes-AntiAffinity 热点评

2023-05-03   腾讯云

Taint和Toleration

2023-05-03   腾讯云

Taint命令入门|世界微动态

2023-05-03   腾讯云

Taint命令入门 动态焦点

2023-05-03   腾讯云

(图片故事)匠心如火炼珍馐

2023-05-03   新华社新闻

ThinkPHP-Session 的使用和配置

2023-05-03   腾讯云

使用SubPath

2023-05-03   腾讯云

Secret热更新-新要闻

2023-05-03   腾讯云

DaemonSet的更新和回滚(二)

2023-05-03   腾讯云

kubernetes-Affinity示例

2023-05-03   腾讯云

Taint命令入门

2023-05-03   腾讯云

Taint和Toleration 世界通讯

2023-05-03   腾讯云

资讯推荐:kubernetes-AntiAffinity

2023-05-03   腾讯云

微视频|你是星火

2023-05-03   新华网

ConfigMap&Secret使用限制

2023-05-03   腾讯云

全球热文:Secret热更新

2023-05-03   腾讯云

Volumes示例

2023-05-03   腾讯云

全球观察:Secret热更新

2023-05-03   腾讯云

ConfigMap&Secret使用限制

2023-05-03   腾讯云

Volumes示例

2023-05-03   腾讯云

ThinkPHP-Session 的使用和配置

2023-05-03   腾讯云

什么是干咳_干咳介绍

2023-05-03   万能网

安心!返程路上有他们来守护

2023-05-03   北京日报客户端

ThinkPHP-缓存的使用和配置

2023-05-03   腾讯云

ThinkPHP-Session 的使用和配置

2023-05-03   腾讯云

ThinkPHP-缓存的使用和配置

2023-05-03   腾讯云

et katy perry_et katy perry_速递

2023-05-03   互联网

每日视讯:全球最大规格剪切型密炼机成功下线

2023-05-03   中国产业经济信息网

kubernetes存活性探针使用

2023-05-03   腾讯云

Copyright   2015-2022 中国行业信息网版权所有  备案号:   联系邮箱:29 59 11 57 8@qq.com