日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

构建器设计模式_创新设计模式:构建器模式

發(fā)布時(shí)間:2023/12/3 asp.net 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 构建器设计模式_创新设计模式:构建器模式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

構(gòu)建器設(shè)計(jì)模式

以前我們看過工廠和抽象工廠模式。 這些模式可以達(dá)到目的,并且確實(shí)有用,但是在某些用例中,我們必須創(chuàng)建一個(gè)非常復(fù)雜的對(duì)象,并且創(chuàng)建它需要不同的步驟,每個(gè)步驟都需要不同的操作。 在這種情況下,構(gòu)建器模式可能非常有用。
構(gòu)建器設(shè)計(jì)模式是一種創(chuàng)新設(shè)計(jì)模式,可用于逐步創(chuàng)建復(fù)雜對(duì)象。

假設(shè)我們有一個(gè)具有許多依賴關(guān)系的對(duì)象,并且為了獲取每個(gè)依賴關(guān)系,必須執(zhí)行某些特定的操作。
在這種情況下,我們可以使用構(gòu)建器模式

  • 將復(fù)雜對(duì)象的各個(gè)部分的創(chuàng)建和組裝封裝在一個(gè)單獨(dú)的Builder對(duì)象中。
  • 將對(duì)象創(chuàng)建委托給Builder對(duì)象,而不是直接創(chuàng)建對(duì)象。

想象一下后端系統(tǒng)必須撰寫和發(fā)送電子郵件的情況。
創(chuàng)建電子郵件可能是一個(gè)復(fù)雜的過程。 您必須指定標(biāo)題,設(shè)置收件人,添加問候語和結(jié)束語。 您可能還想使用小胡子 。 有多種選擇。

創(chuàng)建電子郵件所需的所有操作都只有一個(gè)班級(jí),這可能會(huì)使我們的班級(jí)腫并失去其初衷。

因此,我們將從負(fù)責(zé)發(fā)送電子郵件的類開始。

package com.gkatzioura.design.creational.builder;public class Email {private final String title;private final String recipients;private final String message;public Email(String title, String recipients, String message) {this.title = title;this.recipients = recipients;this.message = message;}public String getTitle() {return title;}public String getRecipients() {return recipients;}public String getMessage() {return message;}public void send() {} }

如您所見,該類僅包含三個(gè)字符串字段,并且對(duì)它們沒有任何額外的處理。 因此,我們將創(chuàng)建一個(gè)構(gòu)建器類,該類將處理消息格式,收件人表示形式和Email類的創(chuàng)建。

package com.gkatzioura.design.creational.builder;import java.util.HashSet; import java.util.Set;public class EmailBuilder {private Set recipients = new HashSet();private String title;private String greeting;private String mainText;private String closing;public EmailBuilder addRecipient(String recipient) {this.recipients.add(recipient);return this;}public EmailBuilder removeRecipient(String recipient) {this.recipients.remove(recipient);return this;}public EmailBuilder setTitle(String title) {this.title = title;return this;}public EmailBuilder setGreeting(String greeting) {this.greeting = greeting;return this;}public EmailBuilder setMainText(String mainText) {this.mainText = mainText;return this;}public EmailBuilder setClosing(String closing) {this.closing = closing;return this;}public Email create() {String message = greeting+"\n"+mainText+"\n"+closing;String recipientSection = commaSeparatedRecipients();return new Email(title,recipientSection,message);}private String commaSeparatedRecipients() {StringBuilder sb = new StringBuilder();for(String recipient:recipients) {sb.append(",").append(recipient);}return sb.toString().replaceFirst(",","");}}

下一步是使電子郵件創(chuàng)建更加嚴(yán)格,以便只能通過EmailBuilder創(chuàng)建電子郵件。

package com.gkatzioura.design.creational.builder;import java.util.HashSet; import java.util.Set;public class Email {private final String title;private final String recipients;private final String message;private Email(String title, String recipients, String message) {this.title = title;this.recipients = recipients;this.message = message;}public String getTitle() {return title;}public String getRecipients() {return recipients;}public String getMessage() {return message;}public void send() {}public static class EmailBuilder {private Set recipients = new HashSet();private String title;private String greeting;private String mainText;private String closing;public EmailBuilder addRecipient(String recipient) {this.recipients.add(recipient);return this;}public EmailBuilder removeRecipient(String recipient) {this.recipients.remove(recipient);return this;}public EmailBuilder setTitle(String title) {this.title = title;return this;}public EmailBuilder setGreeting(String greeting) {this.greeting = greeting;return this;}public EmailBuilder setMainText(String mainText) {this.mainText = mainText;return this;}public EmailBuilder setClosing(String closing) {this.closing = closing;return this;}public Email build() {String message = greeting+"\n"+mainText+"\n"+closing;String recipientSection = commaSeparatedRecipients();return new Email(title,recipientSection,message);}private String commaSeparatedRecipients() {StringBuilder sb = new StringBuilder();for(String recipient:recipients) {sb.append(",").append(recipient);}return sb.toString().replaceFirst(",","");}} }

使用構(gòu)建器模式創(chuàng)建電子郵件的最終結(jié)果將是這樣。

Email email = new Email.EmailBuilder().addRecipient("john@Doe.com").setMainText("Check the builder pattern").setGreeting("Hi John!").setClosing("Regards").setTitle("Builder pattern resources").build();

總而言之,通過使用構(gòu)建器模式,我們能夠創(chuàng)建一個(gè)復(fù)雜的對(duì)象及其復(fù)雜的部分。

您可以在github上找到源代碼。

在下一個(gè)博客中,我們將討論單例模式。

另外,我還編寫了備忘單,其中包含“創(chuàng)作設(shè)計(jì)模式”的摘要。
在鏈接中注冊(cè)以接收它。

翻譯自: https://www.javacodegeeks.com/2018/03/creational-design-patterns-builder-pattern.html

構(gòu)建器設(shè)計(jì)模式

總結(jié)

以上是生活随笔為你收集整理的构建器设计模式_创新设计模式:构建器模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。