<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 29.模板模式 在模板模式中,一個類提供了一個基礎模板,其他類出于其預期目的使用了該基礎模板。 讓我們來看一個例子,看看下面顯示的代碼。 我們有一個名為`News`的類,可以通過各種機制(例如文本(例如 SMS),通過 HTML 格式的網絡,使用 json 或使用 XML)來傳遞此新聞。 因此,`News`類實現了一個基本模板。 它具有稱為`print`的方法,該方法可打印`header`,`body`和`footer`三種方法的輸出。 也就是說,它定義了一個模板,該模板表示要傳遞新聞時,它必須具有標題,然后是新聞正文,然后是新聞頁腳。 現在,鍵入并運行下面的程序 [template.rb](code/design_patterns/template.rb) 。 ```rb ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ```rb | ``` # template.rb class News attr_accessor :title, :content def initialize title, content @title = title @content = content end def header raise "Not Implemented" end def body raise "Not Implemented" end def footer raise "Not Implemented" end def print puts header puts body puts footer end end class PlainText &lt; News def header """ ************************* * TODAYS NEWS * ************************* """ end def footer """ ************************* * GOODBYE! * ************************* """ end def body """ #{title} ========================= #{content} """ end end PlainText.new( "Good Morning!", "Nice weather today" ).print ```rb | ``` 輸出量 ```rb ************************* * TODAYS NEWS * ************************* Good Morning! ========================= Nice weather today ************************* * GOODBYE! * ************************* ``` 現在來看`PlainText`類,它繼承自`News`,因此它必須實現`News`定義的模式。 因此(HTG3)要做的就是定義三種方法,即`header`,`body`和`footer`。 確實如此。 現在以純文本格式打印新聞,我們需要做的就是初始化`PlainText`類的實例,并在其上調用`print`。 通過以下代碼完成 ```rb PlainText.new( "Good Morning!", "Nice weather today" ).print ``` 因此,如果你看到的話,模板模式定義了基本模板或結構,從而為想要擴展它的類帶來了清晰度和結構。 這也可以減少在派生類中需要完成的編碼。 ### 29.1。 行使 為什么不修改模板模式代碼,以便我們得到這樣的 HTML 格式的文本 ```rb <html> <head> <title>Today's News</title> </head> <body> <h1>Good Morning!</h1> <p>Nice weather today</p> </body> </html> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看