Kimn
    • 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
    • Make a copy
    • 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 Make a copy 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
    --- ###### tags: `Design Pattern` --- # 10/30 ~ 10/31 ## 結構型模式 - 結構型模式描述 : ==如何將類或物件按某種布局組成更大的結構。== - 它分為類結構模式和物件結構型模式,前者採用繼承機制來組織接口和類,後者採用組合或聚合來組成物件。 - 由於組合關係或聚合關係比繼承關係耦合度低,滿足==合成復用原則==,所以物件結構型模式比類型結構型模式具有更大的靈活性。 - 分為 7 種 : 代理、適配器、裝飾者、橋接、外觀、組合、享元。 ### 代理模式 Proxy #### 概述: - 由於某些原因需要==給某物件提供一個代理以"控制該物件的訪問"==。 - 這時,訪問物件不適合或者不能直接引用目標物件,代理物件作為訪問物件和目標物件的中介。 - 【例】 : 買房屋要透過仲介找到屋主,買電腦要透過代理商,而不是廠商。 - JAVA 中的代理按照代理類生成時間不同分為靜態代理、動態代理。靜態代理代理類在編譯期就生成,而動態代理代理類則是在 JAVA 運行時動態生成。動態代理又有 JDK 代理和 CGLib 代理兩種。 #### 結構: - 代理 (Proxy) 模式分為三種角色: - 抽象主體(Subject)類 : 通過接口或抽象類聲明真實主題和代理物件實現的業務方法。 ( 代理規範 ) - 真實主題(Real Subject)類 : 實現了抽象主題中具體業務,是代理物件所代表的真實物件,是最終要引用的物件。(屋主) - 代理(Proxy)類 : 提供了與真實主題相同的接口,其內部含有對真實主題的引用,它可以訪問、控制或擴展真實主題的功能。(仲介) :::info 【例】火車站買票 如果要買火車票,需要到火車站買票,坐車到火車站,排隊等一系列操作,顯然比較麻煩。而火車站在多個地方都有代售點,我們去代售點買票就方便很多。 這個例子就是典型的代理模式,火車站是目標物件,代售點是代理物件。類圖如下: ![](https://i.imgur.com/kK2TMg9.png) ::: ```typescript= // 賣火車票 - 抽象主體(Subject)類 interface SellTickets { sell(): void; } // 火車站 - 真實主題(Real Subject)類 class TrainStation implements SellTickets { sell(): void { console.log("火車站賣票"); } } // 代售點 - 代理(Proxy)類 class ProxyPoint implements SellTickets { // 聲明火車站類物件 private trainstation: TrainStation = new TrainStation(); sell(): void { console.log("代售點收取一些服務費用"); this.trainstation.sell(); } } class Client { static main(): void { // 創建代售點類物件 const proxtpoint: ProxyPoint = new ProxyPoint(); // 調用方法進行買票 proxtpoint.sell(); } } Client.main(); ``` - 從上面代碼中可以看出測試類直接訪問的是 ProxyPoint 類物件,也就是說 ProxyPoint 作為訪問物件和目標物件的中介。 - 同時也對 sell 方法進行了增強(代理點收取了一些費用) - #### 動態代理 ```typescript= // 獲取代理物件工廠類 // 代理類也實現了對應的接口 class ProxyFactory { // 聲明目標物件 - station 要是 any 編譯才會過 private station: any = new TrainStation(); constructor(target: TrainStation) { this.station = target; } getProxyObject(): TrainStation { return new Proxy(this.station, { get: (target, propKey) => { console.log("動態代理的保存之前處理..."); return target[propKey]; }, }); } } class Client { static main(): void { const instance = new ProxyFactory(new TrainStation()).getProxyObject(); instance.sell(); } } Client.main(); ``` - 動態代理 vs 靜態代理 - 動態代理最大的好處是,在接口中聲明的所有方法,都被轉移到調用處理器一個集中的方法中處理(InvocationHandler.invoke) (Handler?)。 - 這樣在接口方法數量比較多的時候,我們可以靈活處理,而不需要像靜態代理那樣每一個方法進行中轉。 - 如果接口增加一個方法,靜態代理模式除了所有實現類需要實現這個方法外,所有代理類也需要實現此方法。增加了代碼維護的複雜度。而動態代理不會出現該問題。 - 優點: - 代理模式在客戶端與目標物件之間起到一個中介作用和保護目標物件的作用; - 代理物件可以擴展目標物件的功能; - 代理模式能將客戶端與目標物件分離,在一定程度上降低了系統的耦合度; - 缺點: - 增加了系統的複雜度 #### 使用場景 - 遠程(Remote)代理 本地服務通過網路請求遠程服務。為了實現本地到遠程的通信,我們需要實現網絡通信,處理其中可能的異常。 為了良好的代碼設計和可維護性,我們將網絡通信部分隱藏起來,只暴露給本地服務一個接口,通過該接口即可訪問遠程服務提供的功能,而不必過多關心通信部分的細節。 - 防火牆(Firewall)代理 當你將瀏覽器配置成使用代理功能時,防火墻就將你的瀏覽器的請求轉給互聯網;當互聯網返回響應時,代理服務器再把它轉給你的瀏覽器 - 保護(Protect or Access)代理 控制對一個物件的訪問,如果需要,可以給==不同的用戶提供不同級別的使用權限==。

    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