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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Design Pattern - Adapter ## 前言 你是否曾經遇過辛辛苦苦開發了一個不錯的程式包,卻因為一點點的差異導致無法被重複應用。 這個差異可能是 5%、10%、20% 左右,但卻造成了整包東西無法被再次使用、整合的情境。 今天在專案中引用一個 A 服務,隔天被告知要更換成 B 服務,幾周後又要改成 C 服務,而來自 A B C 的服務本身或多或少都有差異,造成每一次的改變都必須更動到原本的程式邏輯才能完美契合。 但對於程式碼的維護來說卻是糟糕的處理方式,每一次的更正都必須要跑一次測試。 這就像過去有許許多多不同規格的電線插頭一樣,為了不同的插頭而去更換機械部件的插座,非常不符合效益。 今天要說到的 Adapter 正是後來出現的 "轉接頭",他實現了不同規格仍能對接的期望,避免需要更換插座或是更換插頭的狀況。 ## 主題 **Adapter Pattern** 中文常見名稱 **建造者模式** 屬於 **Structural Design Pattern** 一種常見的設計理念。 目的是幫助工程師在對接服務的時候,避免需要動到對街服務本身的邏輯,從而避免了可能因改動造成的問題。確保了程式本身的獨立與完整。 **優點:** - 底層邏輯與實務邏輯(Adapter層)職責分離,增加底層邏輯的重複使用性 - 符合 S.O.L.I.D 原則 - 高擴展性 **缺點:** - 過度的使用 Adapter Pattern 會增加整體架構的複雜性與可維護性 - 較直接使用底層邏輯來的低效 - 測試的難度較高,錯誤的來源有更多的可能性 Adapter Design Pattern 又分為兩種: - Object Adapter - Class Adapter ## Object Adapter 透過實作指定 Interface 的 Adapter 類,並放入想要轉換的類方式實現調配 ![](https://hackmd.io/_uploads/HJKLOSmI3.png) [圖片來源於 Adapter Design Pattern Wiki](https://en.wikipedia.org/wiki/Adapter_pattern) ## Class Adapter 透過多重繼承的方式進行調配,適用於 C++、Python,Java 因為不支援多重繼承,故需要改寫成繼承與實作並行的方式實現調配 ![](https://hackmd.io/_uploads/ByE5OBQ83.png) [圖片來源於 Adapter Design Pattern Wiki](https://en.wikipedia.org/wiki/Adapter_pattern) ## 實行方式 ```java= public class Adapter { public static void main(String [] args){ InitService1 service1 = new Service1Impl(); InitService2 service2 = new Service2Impl(); service1.service_1(); service1.service_2(); service2.service_3(); service2.service_4(); AdapterService1ToService2 adapterService = new AdapterService1ToService2(service1); adapterService.service_3(); adapterService.service_4(); } } interface InitService1{ void service_1(); void service_2(); } class Service1Impl implements InitService1{ @Override public void service_1() { System.out.println("This is Service 1 Service_1()."); } @Override public void service_2() { System.out.println("This is Service 1 Service_2()."); } } interface InitService2{ void service_3(); void service_4(); } class Service2Impl implements InitService2 { @Override public void service_3() { System.out.println("This is Service 2 Service_3()."); } @Override public void service_4() { System.out.println("This is Service 2 Service_4()."); } } class AdapterService1ToService2 implements InitService2 { private InitService1 service1Impl; public AdapterService1ToService2(InitService1 initService1){ this.service1Impl = initService1; } @Override public void service_3(){ /* code ... */ System.out.println("This is Adapter."); service1Impl.service_1(); /* code ... */ } @Override public void service_4(){ /* code ... */ System.out.println("This is Adapter."); service1Impl.service_2(); /* code ... */ } } ``` ``` Execute Results: This is Service 1 Service_1(). This is Service 1 Service_2(). This is Service 2 Service_3(). This is Service 2 Service_4(). This is Adapter. This is Service 1 Service_1(). This is Adapter. This is Service 1 Service_2(). ``` 範例中有 **InitService1**、**InitService2** 兩介面,與實作介面的實體 **Service1Impl**、**Service2Impl** Kai 這邊建立一個將 Service1 轉為 Service2 的 Adapter 類別 **AdapterService1ToService2**,由於目標是轉成 Service2,故需實作 InitService2 除了實作介面的方法外,還包含將 Service1 放入的方法,在使用上就可以透過放入 Service1 的方式對接 Service2 的服務了 :::danger 用一句話介紹 Adapter 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