Kai Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 虎年行大運 - Design Pattern - Builder ## 前言 工程師或多或少都曾經遇過因應服務的擴展,必須要增加輸入參數的情況。 今天接到要做一杯紅茶的需求? 好的沒問題! 建一個方法,只需要輸入茶包、冰塊、甜度三種參數,這樣不僅紅茶,連其他茶飲都可以一併完成。 過幾天接到飲品要有額外添加料的需求? 也沒問題! 在上述方法中增加一個新的參數,這參數會帶入要增加那些添加料的資訊,需求完成。 又過了幾天,飲品要分成大、中、小杯裝...? 飲品要分成店家提供的杯子或顧客自行攜帶的杯子...? 飲品要做成熱飲...? 飲品要出特定季節版本或活動版本...? 飲品要根據店別出特定店版本...? 太多太多的需求慢慢加進來,才會發現...你的參數是不是越來越多了? 從一行變兩行、變三行。如果這個方法來自於介面的話,那代表在更動上會動到更多更多的實做類別。 累不累? 麻不麻煩? 之後老闆說,準備開一間咖啡店,飲品作法與原先的飲料店不同,但至些需求參數相差無幾,頭開始痛了嗎? 你需要的是可以幫你在一個實作類別中抽離建構子(Constructor)與參數的東西。 就是今天要介紹的,在解綁建構子與輸入參數的設計模式 -- **Builder** ## 主題 **Builder Pattern** 中文常見名稱 **配接器模式**、**轉接器模式** 屬於 Creational Pattern 的一種,目的是為了將建構子與過多的輸入參數解綁,讓建構子單純是建構子,參數的輸入可以根據流程進行動態的處理,而不是所有使用該方法的人都必須要求所有輸入參數的部分。 這樣就算今天需要建立許多的實作方法,每一個實作的方法與參數還是可以分開處理。你可以更彈性化的去加入其他設計模式來幫助完成。 **優點:** - 解偶建構子與參數 - 動態參數處理 **缺點:** - 只適用於相似性高的產出 - 若內部使用 Builder 過多則會造成模組數量容易肥大 ![](https://i.imgur.com/20W6cku.png) *[該圖引用來自 Wiki Builder Pattern - Class Diagram](https://en.wikipedia.org/wiki/Builder_pattern)* **架構圖物件說明** | 項目 | 說明 | | ---- | ---- | | Product | 產品 | | Builder | 介面 | | ConcreteBuilder | 實作 Builder 介面的類別 | | Director | 負責設計 ConcreteBuilder 該如何執行 | ## 實行方式 ```java= /** * @author kaichen * Builder 模式,把建構用的參數與建構子解耦,讓參數可以被特定的方法處理,並增加 Director 作為使用前的準備層,在這一層將參數依照特定邏輯放入 Builder 中 */ public class Builder { public static void main(String [] args){ InitBuilder initBuilder = new InitBuilderImpl(); Director cb1 = new Director_1(initBuilder); System.out.println(cb1.getResult()); Director cb2 = new Director_2(initBuilder); System.out.println(cb2.getResult()); } } interface InitBuilder{ public void setElement_1(String str); public void setElement_2(String str); public void setElement_3(String str); public void build(); public String getResult(); } class InitBuilderImpl implements InitBuilder{ private String element_1; private String element_2; private String element_3; private String result; public InitBuilderImpl(){} @Override public void build() { result = new StringBuilder().append("E1: ").append(element_1).append(", ") .append("E2: ").append(element_2).append(", ") .append("E3: ").append(element_3).toString(); } @Override public String getResult() { return result; } @Override public void setElement_1(String str) { this.element_1 = str; } @Override public void setElement_2(String str) { this.element_2 = str; } @Override public void setElement_3(String str) { this.element_3 = str; } } interface Director { public void setElements(); public String getResult(); } class Director_1 implements Director{ private final InitBuilder builder; private final String element_1 = "Builder 1 Element 1"; private final String element_2 = "Builder 1 Element 2"; private final String element_3 = "Builder 1 Element 3"; public Director_1(InitBuilder builder){ this.builder = builder; setElements(); this.builder.build(); } @Override public void setElements(){ this.builder.setElement_1(element_1); this.builder.setElement_2(element_2); this.builder.setElement_3(element_3); } @Override public String getResult(){ return this.builder.getResult(); } } class Director_2 implements Director{ private final InitBuilder builder; private final String element_1 = "Builder 2 Element 1"; private final String element_2 = "Builder 2 Element 2"; private final String element_3 = "Builder 2 Element 3"; public Director_2(InitBuilder builder){ this.builder = builder; setElements(); this.builder.build(); } @Override public void setElements(){ this.builder.setElement_1(element_1); this.builder.setElement_2(element_2); this.builder.setElement_3(element_3); } @Override public String getResult(){ return this.builder.getResult(); } } ``` 而有時候為求處理上的方便,會把輸入參數的方法改寫為回傳本身,這樣就可以在一行內處理完。如以下: ```java= interface InitBuilder{ public InitBuilder setElement_1(String str); public InitBuilder setElement_2(String str); public InitBuilder setElement_3(String str); public void build(); public String getResult(); } class InitBuilderImpl implements InitBuilder{ ... @Override public InitBuilder setElement_1(String str) { this.element_1 = str; return this; } @Override public InitBuilder setElement_2(String str) { this.element_2 = str; return this; } @Override public InitBuilder setElement_3(String str) { this.element_3 = str; return this; } ... } class Director_1 implements Director{ ... @Override public void setElements(){ this.builder.setElement_1(element_1) .setElement_2(element_2) .setElement_3(element_3); } ... } ``` 範例中建立介面 **InitBuilder**、實作類 **InitBuilderImpl**,藉由不同的 **Director** (**Director_1**、**Director_2**) 處理輸入參數、執行,取得不同的結果。 總結: - Builder: 介面 - BuilderImpl: 不同的流程、資料處理邏輯 (面向系統) - Director: 介面 - DirectorImpl: 不同的業務處理邏輯,內容會呼叫 BuilderImpl 協助完成全部或部分內容 (面向業務) :::danger 用一句話介紹 Builder Pattern: **解偶建構子或參數的設計模式** ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Design Pattern`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully