Tải bản đầy đủ (.pdf) (6 trang)

PatternMVCpdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (233.16 KB, 6 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

<b>Introduction to</b>



<b>MVC Design pattern</b>



陳偉凱



台北科技大學資工系



<b>2</b>


<b>MVC: Model View Controller</b>



z

<b>Splits user interface </b>



<b>interaction into</b>

<b>three </b>


<b>distinct roles</b>

<b>.</b>



„ <b>The </b><i><b>model</b></i><b>represents some </b>


<b>information about the </b>
<b>domain. </b>


„ <b>The</b><i><b>view</b></i><b>represents the </b>


<b>display of the model in the </b>
<b>UI. </b>


„ <b>The</b><i><b>controller</b></i><b>takes user </b>


<b>input, manipulates the </b>
<b>model, and causes the view </b>


<b>to update appropriately.</b>


a
b


c


a b c


a = 50%
b = 30%
c = 20%


<b>change notification</b>
<b>requests, modifications</b>
x


y
z


a b c


</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

<b>3</b>


<b>MVC: How it Work</b>



z

<b>Separating the </b>

<b>presentation</b>

<b>from the </b>

<b>model</b>



„<b>Fundamentally presentation and model are about </b>



<b>different concerns.</b>


„<b>Depending on context, users want toseethe same basic </b>


<b>model information in differentways.</b>


„<b>Non-visual objects are usually easier to testthan visual </b>


<b>ones.</b>


„<b>Thepresentation dependson themodel</b>
„<b>Observerpattern [GOF]</b>


Model
Presentation


<b>MVC: How it Work</b>



z

<b>Separating the </b>

<b>controller</b>

<b>from the </b>

<b>view</b>



„<b>Support editable and non-editable behavior: one view + </b>


<b>two controllers</b>


„<b>Controller = Strategiespattern [GOF]</b>
„<b>Useful for Web interface</b>


</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

<b>5</b>


<b>MVC: When to Use It</b>




z

<b>The value of MVC lies in its two </b>

<b>separations</b>



„<b>Separating the presentation from the model</b>


„<b>Separating the controller from the view (less important)</b>


z

<b>Don’t use it in very simple systems where the </b>



<b>model has no real behavior in it anyway.</b>



<b>6</b>

<b>Example code #1</b>



<b>namespace XXX</b>


<b>{</b>



<b>class Program</b>


<b>{</b>



<b>static model m = new model();</b>


<b>static Form1 f = new Form1(m);</b>


<b>static void Main(string[] args)</b>


<b>{</b>



<b>Application.Run(f);</b>


<b>}</b>



<b>}</b>




<b>}</b>

model


</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

<b>7</b>

<b>Example code #1</b>



<b>namespace XXX</b>
<b>{</b>


<b>class Form1 : Form</b>
<b>{</b>


<b>private model m;</b>


<b>public Form1(model m)</b>
<b>{</b>


<b>this.m = m;</b>
<b>…</b>


<b>}</b>


<b>private void B1_Click(object …)</b>
<b>{</b>


<b>m.press(1);</b>


<b>xxx.Text = m.GetResult().ToString();</b>
<b>}</b>


<b>}</b>


<b>}</b>


model
form


<b>Example code #2</b>


<b>namespace XXX</b>



<b>{</b>



<b>class Program</b>


<b>{</b>



<b>static Form1 f = new Form1();</b>


<b>static void Main(string[] args)</b>


<b>{</b>



<b>Application.Run(f);</b>


<b>}</b>



<b>}</b>



<b>}</b>

<sub>model</sub>


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

<b>9</b>

<b>Example code #2</b>



<b>namespace XXX</b>
<b>{</b>



<b>class Form1 : Form</b>
<b>{</b>


<b>private model m = new model;</b>
<b>public Form1()</b>


<b>{</b>
<b>}</b>


<b>private void B1_Click(object …)</b>
<b>{</b>


<b>m.press(1);</b>


<b>xxx.Text = m.GetResult().ToString();</b>
<b>}</b>


<b>…</b>
<b>}</b>
<b>}</b>


model
form


<b>10</b>

<b>Example Class Diagram #1</b>



List<Word>


<b>SpellerForm</b>



-words


<b>Dictionary</b>


-English
-Chinese


<b>Word</b>


View & Control Model


-Question
-Solution


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

<b>11</b>

<b>Example Class Diagram #2</b>



<b>SpellerForm</b>


-words


<b>Dictionary</b>


-English
-Chinese


<b>Word</b>


+Start()


+Next()
+GetQuestion()
+IsCorrect()
+GetScore()


<b>Speller</b>


Model Facade
View & Control


-Question
-Answer


</div>

<!--links-->

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×