2024-06-18|閱讀時間 ‧ 約 22 分鐘

設計模式與程式架構(八)

    ※ 轉接器模式

    定義:

    轉接器模式是一種結構型設計模式(有時也被稱為wrapper或裝飾模式),用於將一個類(class)的接口轉換成客戶端所期望的另一個接口。這使得原本因接口不匹配而無法工作的類(class)可以一起工作,讓接口不兼容的類別能夠合作無間。

    ※ 數字到字符的轉換過程範例

    //回傳傳入的數字
    const returnValueWhatInput = (value: number) => value
    //將數字轉換為對應的字符並返回
    const returnValueWhatInputAdapter = (value: number) =>
    //String.fromCharCode 是 JavaScript 的一個內建方法,用於將 Unicode 數字轉換為對應的字符。
    String.fromCharCode(returnValueWhatInput(value))

    // 輸出字符 'A'
    console.log(returnValueWhatInputAdapter(65))//A
    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.