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 - Prototype ## 前言 身為工程師的你,是不是偶爾會面對到以下幾種狀況? 1. 需要一個相同的物件來執行事務上的處理,且不能透過 new,因為須要當前物件狀態。 2. 正在跑 Loop 建立大量、相同的物件,差異僅在物件中的屬性值,但建立物件的過程涉及了十幾道工。 曾經的作法就是暴力重建、暴力複製、富含耐心的等待... 結果好死不死發現物件中有些屬性是 Private 無法進行暴力複製;或者僅知道介面,無法找到想複製的實體類別物件...欲哭無淚。 不需要這麼累,也不需要這麼耗時間與資源,有了今天要介紹的方法,省下的時間可以早點下班跟家人或愛人團圓~ ## 主題 **Prototype Pattern** 中文常見名稱 **原型模式** 屬於 Creational Pattern 的一種,意在透過 Clone 物件的方式,保留當前物件狀態與屬性、減少建立物件的過程與時間。 Java 中的複製還包含兩種: **淺複製** 與 **深複製** 在談及兩種複製之前,還要先認識到 Java 的物件中通常包含兩種類型的屬性: **原始型別屬性**、**類別屬性(引用類型屬性)** - **淺複製** - 若成員屬性為**原始型別**,如: int, double, String 等,**會複製**實體給新物件 - 若成員屬性為**類別**,如: Collection 類,**不會複製**實體,會引用同一來源 - **深複製** - 若成員屬性為**原始型別**,如: int, double, String 等,**會複製**實體給新物件 - 若成員屬性為**類別**,如: Collection 類,**會複製**實體給新物件 **優點:** - 執行效能佳 (new 的建立物件方式很耗費資源) - 節省建立時間 - 可取得無法透過 new 方式建立的物件實體 **缺點:** - 每一個類別屬性都需要實作 Cloneable 介面 (意即每個類別都要有 clone()) - 若需要更動 clone() 時,會直接對類別物件進行修改,違背 OCP 原則 ![](https://i.imgur.com/rNFqb1Y.png) *[該圖引用來自 Wiki Prototype Pattern - Class Diagram](https://en.wikipedia.org/wiki/Prototype_pattern)* **架構圖物件說明** | 項目 | 說明 | | ---- | ---- | | Prototype | 介面或抽象,須實作 Cloneable 介面 | | ConcretePrototype1 | 介面實體類,若屬性有類別,則需要覆寫 clone() 並在其中將類別成員屬性一併 clone 後再引用 | | Client | 負責使用 Prototype 的物件 | ## 實行方式 ```java= /** * @author kaichen * 透過 Clone 方式得到物件,可以避免無法透過 new 建立物件或是建立物件的過程非常複雜等情境。 * 注意,在物件中的類別成員屬性也需要實作 Cloneable 介面,並在物件的 Clone() 中添加該類別成員屬性的 clone 和更新,否則當類別成員屬性會引用同一來源 * 注意,在物件中的原始型別成員屬性因為是 Immutable Object 的關係,因此就算改變賦予的值,也會直接生成新物件,而不影響原先物件引用的來源 */ public class Prototype { public static void main(String [] args) throws CloneNotSupportedException{ /* 設定放入參數 */ int testI = 10; String testStr = "10"; TestClass testClass = new TestClass("test10", "test10"); /* 建立物件和 Clone物件 */ PrototypeAbs prototypeAbs = new ConcretePrototype1(testI, testStr, testClass); PrototypeAbs clonePrototypeAbs = prototypeAbs.clone(); PrototypeAbs prototypeAbs2 = new ConcretePrototype2(testI, testStr, testClass); PrototypeAbs clonePrototypeAbs2 = prototypeAbs2.clone(); /* 設定新值給參數 */ /* 原始型別屬性會直接替新值建立新物件,不影響物件原先引用來源 */ /* 類別型別屬性因沒有在 Clone() 中一併複製關係,造成兩物件引用同一來源,因此改變類別型別屬性的值,會同時影響兩物件 */ testI = 20; testStr = "20"; testClass.setTestStr1("test20"); testClass.setTestStr2("test20"); System.out.println("PrototypeAbs 1"); System.out.println(prototypeAbs.hashCode() + " Get Int: " + prototypeAbs.getI()); System.out.println(prototypeAbs.hashCode() + " Get String: " + prototypeAbs.getStr()); System.out.println(prototypeAbs.hashCode() + " Get TestClass String 1: " + prototypeAbs.getTestClass().getTestStr1()); System.out.println(prototypeAbs.hashCode() + " Get TestClass String 2: " + prototypeAbs.getTestClass().getTestStr2()); System.out.println(); System.out.println("PrototypeAbs 1 Clone"); System.out.println(clonePrototypeAbs.hashCode() + " Get Int: " + clonePrototypeAbs.getI()); System.out.println(clonePrototypeAbs.hashCode() + " Get String: " + clonePrototypeAbs.getStr()); System.out.println(clonePrototypeAbs.hashCode() + " Get TestClass String 1: " + clonePrototypeAbs.getTestClass().getTestStr1()); System.out.println(clonePrototypeAbs.hashCode() + " Get TestClass String 2: " + clonePrototypeAbs.getTestClass().getTestStr2()); System.out.println(); System.out.println("PrototypeAbs 2"); System.out.println(prototypeAbs2.hashCode() + " Get Int: " + prototypeAbs2.getI()); System.out.println(prototypeAbs2.hashCode() + " Get String: " + prototypeAbs2.getStr()); System.out.println(prototypeAbs2.hashCode() + " Get TestClass String 1: " + prototypeAbs2.getTestClass().getTestStr1()); System.out.println(prototypeAbs2.hashCode() + " Get TestClass String 2: " + prototypeAbs2.getTestClass().getTestStr2()); System.out.println(); System.out.println("PrototypeAbs 2 Clone"); System.out.println(clonePrototypeAbs2.hashCode() + " Get Int: " + clonePrototypeAbs2.getI()); System.out.println(clonePrototypeAbs2.hashCode() + " Get String: " + clonePrototypeAbs2.getStr()); System.out.println(clonePrototypeAbs2.hashCode() + " Get TestClass String 1: " + clonePrototypeAbs2.getTestClass().getTestStr1()); System.out.println(clonePrototypeAbs2.hashCode() + " Get TestClass String 2: " + clonePrototypeAbs2.getTestClass().getTestStr2()); } } abstract class PrototypeAbs implements Cloneable{ protected int i = 0; protected String str = "0"; protected TestClass testClass = null; public PrototypeAbs (){} public PrototypeAbs(int newI, String newStr, TestClass newTestClass){ this.i = newI; this.str = newStr; this.testClass = newTestClass; } public int getI() { return i; } public void setI(int i) { this.i = i; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } public TestClass getTestClass() { return testClass; } public void setTestClass(TestClass testClass) { this.testClass = testClass; } @Override public PrototypeAbs clone() throws CloneNotSupportedException { return (PrototypeAbs) super.clone(); } } class ConcretePrototype1 extends PrototypeAbs { public ConcretePrototype1 (){} public ConcretePrototype1(int newI, String newStr, TestClass newTestClass){ super(newI, newStr, newTestClass); } @Override public PrototypeAbs clone() throws CloneNotSupportedException { ConcretePrototype1 c = (ConcretePrototype1) super.clone(); TestClass t = (TestClass) super.testClass.clone(); c.setTestClass(t); return c; } } class ConcretePrototype2 extends PrototypeAbs { public ConcretePrototype2 (){} public ConcretePrototype2(int newI, String newStr, TestClass newTestClass){ super(newI, newStr, newTestClass); } } class TestClass implements Cloneable { private String testStr1 = "testStr"; private String testStr2 = "testStr"; public TestClass(){} public TestClass(String testStr1, String testStr2){ this.testStr1 = testStr1; this.testStr2 = testStr2; } public void setTestStr1(String newStr){ this.testStr1 = newStr; } public void setTestStr2(String newStr){ this.testStr2 = newStr; } public String getTestStr1(){ return this.testStr1; } public String getTestStr2(){ return this.testStr2; } @Override public TestClass clone() throws CloneNotSupportedException { return (TestClass) super.clone(); } } ``` **實行結果** ``` 有實作類別 clone 的物件,其類別屬性的引用來源已不同 - 深複製 PrototypeAbs 1 1882008996 Get Int: 10 1882008996 Get String: 10 1882008996 Get TestClass String 1: test20 1882008996 Get TestClass String 2: test20 PrototypeAbs 1 Clone 638559109 Get Int: 10 638559109 Get String: 10 638559109 Get TestClass String 1: test10 638559109 Get TestClass String 2: test10 ------------------------------------------------ 無實作類別 clone 的物件,其類別屬性的引用來源相同 - 淺複製 PrototypeAbs 2 1287160904 Get Int: 10 1287160904 Get String: 10 1287160904 Get TestClass String 1: test20 1287160904 Get TestClass String 2: test20 PrototypeAbs 2 Clone 1710537297 Get Int: 10 1710537297 Get String: 10 1710537297 Get TestClass String 1: test20 1710537297 Get TestClass String 2: test20 ``` 上述例子中共有兩個實體類繼承了 PrototypeAbs 抽象。分成有覆寫 clone 的**深複製**與沒覆寫 clone 的**淺複製**,在最後的結果輸出很清楚的顯示: 有覆寫的,其類別成員屬性的值已引用不同的來源;沒覆寫的,其類別成員屬性的值依舊是相同來源。 首頁 [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