杜嘉豪
    • 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
    • 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
    • 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 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
  • 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
    DP4 建造者模式、觀察者模式 === ###### tags: `DesignPatterns` http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf ## 建造者模式 定義: 將一個複雜的物件的建構與他的表示分離,使的同樣的建構過程可以建立不同的表示。 建築師依照藍圖(Director)去蓋房子,一棟房子有哪些部位具體都寫在藍圖上。 蓋一棟房子可能需要 1.建造廚房的團隊 2.建造廁所的團隊 3. 建造電梯的團隊 p.176 主要用於建立一些複雜的物件,這些物件內部建構間的建造順序通常是穩定的,但物件內部的構造通常面臨著複雜的變化。 part A + part B p.176 建造者模式的好處就是讓建造程式碼與表示程式碼分離,由於建造者隱藏了產品是如何組裝的,所以若需要改變各產品的內部表示,只需要在定義一個具體的建造者就可以了。 Q: 建造者模式中的 Builder 實體,是否都只能是類似性質的 Builder, 否則 Builder 介面會多很多不共通的方法。 https://sourcemaking.com/design_patterns/builder/php/1 ```php= <?php class HTMLPage { private $page = NULL; private $page_title = NULL; private $page_heading = NULL; private $page_text = NULL; function __construct() { } function showPage() { return $this->page; } function setTitle($title_in) { $this->page_title = $title_in; } function setHeading($heading_in) { $this->page_heading = $heading_in; } function setText($text_in) { $this->page_text .= $text_in; } function formatPage() { $this->page = '<html>'; $this->page .= '<head><title>'.$this->page_title.'</title></head>'; $this->page .= '<body>'; $this->page .= '<h1>'.$this->page_heading.'</h1>'; $this->page .= $this->page_text; $this->page .= '</body>'; $this->page .= '</html>'; } } ``` ```php= <?php class XMLPage { private $page = NULL; // private $page_title = NULL; // private $page_heading = NULL; // private $page_text = NULL; function __construct() { } function showPage() { return $this->page; } function setTitle($title_in) { // $this->page_title = $title_in; } function setHeading($heading_in) { // $this->page_heading = $heading_in; } function setText($text_in) { // $this->page_text .= $text_in; } function formatPage() { $this->page = '<?xml version="1.0"?>'; $this->page .= '<entry>'; $this->page .= '<name>' . ''</name>''; $this->page .= '<tel>' . '</tel>'; $this->page .= '<email>' . '</email>'; $this->page .= '<comments>' . '</comments>'; $this->page .= '<entry>'; } } ``` --- ```php= <?php abstract class AbstractPageBuilder { abstract function getPage(); } abstract class AbstractPageDirector { abstract function __construct(AbstractPageBuilder $builder_in); abstract function buildPage(); abstract function getPage(); } class HTMLPage { private $page = NULL; private $page_title = NULL; private $page_heading = NULL; private $page_text = NULL; function __construct() { } function showPage() { return $this->page; } function setTitle($title_in) { $this->page_title = $title_in; } function setHeading($heading_in) { $this->page_heading = $heading_in; } function setText($text_in) { $this->page_text .= $text_in; } function formatPage() { $this->page = '<html>'; $this->page .= '<head><title>'.$this->page_title.'</title></head>'; $this->page .= '<body>'; $this->page .= '<h1>'.$this->page_heading.'</h1>'; $this->page .= $this->page_text; $this->page .= '</body>'; $this->page .= '</html>'; } } class HTMLPageBuilder extends AbstractPageBuilder { private $page = NULL; function __construct() { $this->page = new HTMLPage(); } function setTitle($title_in) { $this->page->setTitle($title_in); } function setHeading($heading_in) { $this->page->setHeading($heading_in); } function setText($text_in) { $this->page->setText($text_in); } function formatPage() { $this->page->formatPage(); } function getPage() { return $this->page; } } class HTMLPageDirector extends AbstractPageDirector { private $builder = NULL; public function __construct(AbstractPageBuilder $builder_in) { $this->builder = $builder_in; } public function buildPage() { $this->builder->setTitle('Testing the HTMLPage'); $this->builder->setHeading('Testing the HTMLPage'); $this->builder->setText('Testing, testing, testing!'); $this->builder->setText('Testing, testing, testing, or!'); $this->builder->setText('Testing, testing, testing, more!'); $this->builder->formatPage(); } public function getPage() { return $this->builder->getPage(); } } writeln('BEGIN TESTING BUILDER PATTERN'); writeln(''); $pageBuilder = new HTMLPageBuilder(); $pageDirector = new HTMLPageDirector($pageBuilder); $pageDirector->buildPage(); $page = $pageDirector->getPage(); writeln($page->showPage()); writeln(''); writeln('END TESTING BUILDER PATTERN'); function writeln($line_in) { echo $line_in."<br/>"; } ?> ``` Output ``` BEGIN TESTING BUILDER PATTERN <html> <head><title>Testing the HTMLPage</title></head> <body> <h1>Testing the HTMLPage</h1> Testing, testing, testing! Testing, testing, testing, or! Testing, testing, testing, more! </body> </html> END TESTING BUILDER PATTERN ``` ## 觀察者模式定義: (又叫做 發佈/訂閱 Publish/Subscribe 模式) 定義: 觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個處提物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們能夠自己更新自己。 p.196 動機? 將一個系統分割成一系列互相協作的類別有一個很不好的副作用,那就是需要維護相關物件間的醫治姓。我們不希望為了維持一致性而使各類別緊密耦合,這樣會給維護、擴展和重用都帶來不便。(例如 p.185) 當一個物件的改變需要同時蓋便其他物件時,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式。 一個抽象模型有了個方面,其中一方面依賴於另一方面,這時候使用觀察者模式可以將這兩者封裝在獨立的物件中,使他們各自獨立地改變和複用。 觀察者模式所做的工作其實就是在解除耦合。讓耦合的雙方都依賴於抽象,而不是依賴於具體。從而使得各自的變化都不會影響另一邊的變化。 p.203 委託就是一種參考方法的類型。一旦為委託分配了方法,委託將與該方法具有完全相同的行為。委託方法的使用可以向其他任何方法依樣, 具有參數和返回值。委託可以看做是對函數的抽象,是函數的"類別",委託的實體將代表一個具體的函數。 p.203 一個委託可以搭仔多個方法,所有方法被依序喚起。更重要的是,它可以使的委託物件所搭仔的方法並不需要同一個類別。 (Dispatch 可以 搭仔許多不同的 Job) ----------------------- P.203 委託物件所搭載的所有方法必須具有相同的圓形和型式,也就是擁有相同的參數列表和返回值類型。 ## 觀察者 - 觀察者模式定義了物件之間一對多的關係 - 主題(也就是可被觀察者)更新觀察者的方式是透過一個共同介面 - 觀察者和可觀察者之間採用鬆綁的方式結合(loose-coupling),因為可觀察者不知道觀察者的細節,只知道觀察者有實踐特定的觀察者介面 - 使用此模式時,你可以從被觀察者處推送貨拉取資料。(然而,推送的方式被認為是比較正確的做法。) - 有多個觀察者時,不可以依賴特定的通知次序。 觀察者模式 MVC 將變動部分封裝起來 多用合成,少用繼承 針對介面寫程式,步是針對實踐寫程式 努力鬆綁物件之間的緊密度 ----------------- 畫胖火柴人、瘦火柴人都有共通的步驟,就是都需要筆,然後畫頭、手、腳、身體。 於是我們宣告一個火柴人類別 胖火柴人、瘦火柴人分別 原本直接的做法是宣告胖火柴人、瘦火柴人兩個類別客戶端再去實例化。 這邊 但這樣違反了依賴倒轉原則,因為客戶端(高階模組)不應該依賴火柴人CLASS 透過廚師的烹調手法(細節)煮了某道菜(抽象)名為義大利麵 這時候抽象就依賴了細節,也就是菜直接透過(依賴)了廚師的烹調手法實現,但根據依賴倒轉原則,抽象不應該依賴於細節。 假設今天廚師得了武漢肺炎味覺有問題了,也就代表細節會被影響到, 這時候因為菜直接依賴廚師烹調手法的關係,我們的菜味道無法正常呈現。 所以這邊我們不應該依賴於廚師的烹調手法(細節),而是應該依賴餐點固定的食譜(抽象),具體規定每道菜有哪些手續, 而義大利麵具體做法則是實踐這個食譜中的烹調手續,並添加細節(煮多久之類的)。

    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