<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_小AI</title><subtitle type="text">Be the change you want to see in the world.Things are always as hard as you think but always as easy as you do. </subtitle><id>http://feed.cnblogs.com/blog/u/54380/rss</id><updated>2012-05-29T12:08:37Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/54380/rss"/><entry><id>http://www.cnblogs.com/heaiping/archive/2012/05/05/2484748.html</id><title type="text">[立此存照]Push-up</title><summary type="text">RT</summary><published>2012-05-05T06:56:00Z</published><updated>2012-05-05T06:56:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2012/05/05/2484748.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2012/05/05/2484748.html"/><content type="html">&lt;p&gt;RT&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2484748.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2012/05/05/2484748.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2012/04/10/Definition_of_Refactoring.html</id><title type="text">Definition of Refactoring</title><summary type="text">Long time since my last post, busy with the work these months.Update here with the definition of the refactoring.From the classic book: Refactoring by MF:Refactoring(noum): a change made to the internal structure of the software to make it easier to understand and cheaper to modify without changing </summary><published>2012-04-10T14:22:00Z</published><updated>2012-04-10T14:22:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2012/04/10/Definition_of_Refactoring.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2012/04/10/Definition_of_Refactoring.html"/><content type="html">&lt;p&gt;Long time since my last post, busy with the work these months.&lt;/p&gt;&lt;p&gt;Update here with the definition of the refactoring.&lt;/p&gt;&lt;p&gt;From the classic book: Refactoring by MF:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Refactoring(noum): a change made to the internal structure of the software to make it easier to understand and cheaper to modify without changing its observable behavior.&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Refactoring(verb): to restructure software by applying a series of refactorings without changing its observable behavior.&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;Hopefully, will get back here soon.&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2441417.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2012/04/10/Definition_of_Refactoring.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2012/02/10/2345537.html</id><title type="text">TypeInitializationExeption</title><summary type="text">mark here, finish soon.</summary><published>2012-02-10T07:45:00Z</published><updated>2012-02-10T07:45:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2012/02/10/2345537.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2012/02/10/2345537.html"/><content type="html">&lt;p&gt;mark here, finish soon.&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2345537.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2012/02/10/2345537.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/20/2295157.html</id><title type="text">a link for book</title><summary type="text">http://news.cnblogs.com/n/125826/</summary><published>2011-12-20T13:25:00Z</published><updated>2011-12-20T13:25:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/20/2295157.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/20/2295157.html"/><content type="html">&lt;p&gt;&lt;a href="http://news.cnblogs.com/n/125826/"&gt;http://news.cnblogs.com/n/125826/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2295157.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/20/2295157.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/12/2285390.html</id><title type="text">[review]Design Pattern:State</title><summary type="text">StateAlter the object's behavior by the changes of its state. And the states are all encapsulated into single object state by state. decouple the state(especially the object's behavior under different states) from the the object itselfWhen we use this patternThey are all about the state of t</summary><published>2011-12-12T14:48:00Z</published><updated>2011-12-12T14:48:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/12/2285390.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/12/2285390.html"/><content type="html">&lt;p&gt;&lt;strong&gt;State&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Alter the object's behavior by the changes of its state. And the states are all encapsulated into single object state by state. decouple the state(especially the object's behavior under different states) from the the object itself&lt;/p&gt;&lt;p&gt;&lt;strong&gt;When we use this pattern&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;They are all about the state of the object and dependency on the state, we are highly recommended to use this pattern if below:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The behavior of the object depends on its states so much, the behavior will be changed while the changes of the states&lt;/li&gt;&lt;li&gt;If we got lots of if/else statements to describe different behaviors under different conditions. By the help of this pattern, we can design the each statement as a very concrete state object. If we got lots of if/else statements, our method may be very large, which is not what we expected and is ugly. we should avoid this situationa and try our best to reduce the using so many if/else statements.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;Roles in this pattern&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Content: This is the very object that depends so much on its states. this object defines the interface that the clients want. and also maintains an instance of the ConcreteState which represents the current state of the Content&lt;/li&gt;&lt;li&gt;State: this is interface designed for the&amp;nbsp;particular behavior depending on the&amp;nbsp;particular state&lt;/li&gt;&lt;li&gt;ConcreteState: a object which represents an&amp;nbsp;particular state and also implements the State interface for the&amp;nbsp;particular bahavior, the amount of the ConcreteState depends on the amount of Content's state.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;One more thing for the ConcreteState: which not only implements the particular behavior of the content but also is responsible fot setting the content's state from the current(itself) to the next.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Easy to say but hard to do, a small example below to demonstrate the above:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" onclick="cnblogs_code_show('b920aa91-65aa-4256-882e-4f9c6b2913c6')"&gt;&lt;div id="cnblogs_code_open_b920aa91-65aa-4256-882e-4f9c6b2913c6" class="cnblogs_code_hide"&gt;&lt;span style="color: #0000ff;"&gt;namespace&lt;/span&gt; State&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; State&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Handle(Content content);&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;implement the behavior but also change the state to next&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteStateA : State&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Handle(Content content)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;ConcreteStateA.Handle&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;implement the behavior&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;            content.State = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteStateB();&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;change the state, the next state of this state is state B&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteStateB : State&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Handle(Content content)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;ConcreteStateB.Handle&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;implement the behavior&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;            content.State = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteStateA();&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt;/change the state, the next state of this state is state A&lt;/span&gt;&lt;span style="color: #808080;"&gt;&lt;br /&gt;&lt;/span&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Content&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; State state;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Content(State state)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = state;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #808080;"&gt;///&lt;/span&gt; &lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; default constructor is here to set the default state if no special state is asked when initialization&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt; &lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;&lt;br /&gt;&lt;/span&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Content()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteStateA();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; State State&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state; }&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;set&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = value; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Request()&lt;br /&gt;        {&lt;br /&gt;            state.Handle(&lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Content content = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Content();&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;use the default constructor&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;            content.Request();&lt;br /&gt;            content.Request();&lt;br /&gt;            content.Request();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Different hehaviors under different states, the behavior is implemented by the corresponding state which also changes the state from the current to the next.&lt;/p&gt;&lt;p&gt;The content maintains the instance of an state and its behaviors are changed by the changes of its states. But they are decoupled from each other .&lt;/p&gt;&lt;p&gt;The state is self-described.&lt;/p&gt;&lt;p&gt;The one who always maintains the instance of State is the state.&lt;/p&gt;&lt;p&gt;The state is the state which belongs to the Content but not others.&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2285390.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/12/2285390.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/08/2281338.html</id><title type="text">[review]Design Pattern:Bridge</title><summary type="text">BridgeThe book told me that this pattern is dedicated to decouple the abstraction from its implemetation so they can vary by themself without affecting each others.To be honest, I don't know which one I should call as abstraction and which one as implemetation. I call them two structures designe</summary><published>2011-12-08T14:29:00Z</published><updated>2011-12-08T14:29:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/08/2281338.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/08/2281338.html"/><content type="html">&lt;p&gt;Bridge&lt;/p&gt;&lt;p&gt;The book told me that this pattern is dedicated to decouple the abstraction from its implemetation so they can vary by themself without affecting each others.&lt;/p&gt;&lt;p&gt;To be honest, I don't know which one I should call as abstraction and which one as implemetation. I call them two structures designed for different functions. They focus on different situations, but they should work together to get the things done.&lt;/p&gt;&lt;p&gt;There are so many ways for us available to&amp;nbsp;composite two objects and to let them work together. A very frequent way is to use the inheritance mechanism.&lt;/p&gt;&lt;p&gt;But the&amp;nbsp;inheritance &amp;nbsp;has drawbacks with less flexible in some situations. The inherited class depends on its father class so much. It is hard to modify, to extend.&lt;/p&gt;&lt;p&gt;We can use another way to improve this situation with the new machenism called as Composition and &amp;nbsp;Aggregation. Which makes a "Has" structure compared with the inheritance mechanism with the "is-a" structure.&lt;/p&gt;&lt;p&gt;With the new one, the different parts can work independently but also can work together.&lt;/p&gt;&lt;p&gt;Software design is so flexible that there is no a hard way to let us know to choose which one. it is full of trade-offs based on our experiences and the special situations.&lt;/p&gt;&lt;p&gt;Sometimes the inheritance is good but sometimes the composition or the aggregation is better.&lt;/p&gt;&lt;p&gt;The Bridge Pattern is the very structure which uses the Composition structure to design the system. If you draw the UML of this pattern, you can find dramatically that the structure is like a very bridge, which bidges the two different parts(the two ones the book called as Abstraction and Implementation) and helps them communicate with each other and cooperates with each other.&lt;/p&gt;&lt;p&gt;A small example here to show this pattern:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" onclick="cnblogs_code_show('21f3b080-130d-4691-9f18-f50bea8fad73')"&gt;&lt;div id="cnblogs_code_open_21f3b080-130d-4691-9f18-f50bea8fad73" class="cnblogs_code_hide"&gt;&lt;span style="color: #0000ff;"&gt;namespace&lt;/span&gt; Bridge&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Implementor&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Operation();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteImplementorA : Implementor&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Operation()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Operation A&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteImplementorB : Implementor&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Operation()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Operation B&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Abstraction&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;protected&lt;/span&gt; Implementor implementor;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; SetImplementor(Implementor implementor)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.implementor = implementor;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Operation()&lt;br /&gt;        {&lt;br /&gt;            implementor.Operation();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; RefinedAbstraction : Abstraction&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Operation()&lt;br /&gt;        {&lt;br /&gt;            implementor.Operation();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Client&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Abstraction ab = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; RefinedAbstraction();&lt;br /&gt;&lt;br /&gt;            ab.SetImplementor(&lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteImplementorA());&lt;br /&gt;            ab.Operation();&lt;br /&gt;&lt;br /&gt;            ab.SetImplementor(&lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteImplementorB());&lt;br /&gt;            ab.Operation();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2281338.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/08/2281338.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/07/2280022.html</id><title type="text">[review]Design Pattern:Iterator</title><summary type="text">IteratorA way provided by this pattern lets the client access the aggregate object in turns(one by one) but without exposing the details of the aggregate object.An iterator are designed in this pattern toenumerate the object.Auctually. ASP.NET has implemented this pattern with providing two interfac</summary><published>2011-12-07T15:04:00Z</published><updated>2011-12-07T15:04:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/07/2280022.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/07/2280022.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Iterator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;A way provided by this pattern lets the client access the aggregate object in turns(one by one) but without exposing the details of the aggregate object.&lt;/p&gt;&lt;p&gt;An iterator are designed in this pattern to&amp;nbsp;enumerate the object.&lt;/p&gt;&lt;p&gt;Auctually. ASP.NET has implemented this pattern with providing two interfaces:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;IEnumerator: does not support the generics&lt;/li&gt;&lt;li&gt;IEnumerable: support the generics&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Most of time for most of situations they are enough for our program. there is no need for us to do it ourself. But it is ok for us to dig, which can give us more.&lt;/p&gt;&lt;p&gt;The iterator is responsible for enumerating the aggregate object and accesing the inner types of the object. The client just uses the Iterator to MoveNext. to access the First.etc.&lt;/p&gt;&lt;p&gt;If you want a object to be accessed by the index, implement the indexer provied by the C#, and also implement the Iterator(customed or inherent ones), the interator will hold the object and make the enumeration if needed.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Investigating the history is for further understanding of the future.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;I made a demo implementing the Iterator pattern, and also supporting the generics, see below:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" onclick="cnblogs_code_show('ece486cc-52ee-497d-807b-c658a6ebe984')"&gt;&lt;/div&gt;&lt;p&gt;One more thing, I strongly recommend every one should use the generics supported from the C#2 for security issues for performance issue.&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2280022.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/07/2280022.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/06/2278613.html</id><title type="text">[review]Design Pattern:Command</title><summary type="text">CommandEncapsulate an request into the object. The command holds the receiver, the invoker holds the command.encapsulate the details, show the interface to the client. the client does not know the exact implemetation .The client only cares about his request and tells the invoker what his request is,</summary><published>2011-12-06T15:11:00Z</published><updated>2011-12-06T15:11:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/06/2278613.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/06/2278613.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Command&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Encapsulate an request into the object. The command holds the receiver, the invoker holds the command.&lt;/p&gt;&lt;p&gt;encapsulate the details, show the interface to the client. the client does not know the exact implemetation .&lt;/p&gt;&lt;p&gt;The client only cares about his request and tells the invoker what his request is, the invoker creates an&amp;nbsp;appropriate command according to the client's request and executes the command.&lt;/p&gt;&lt;p&gt;The command holds the approriate receiver. the receiver's action is executed, the things are done.&lt;/p&gt;&lt;p&gt;Actually this pattern also decouples the appearance from the implemetation. So called DP is nothing but OOD, exactly the experience about how the BIG GUYS use the OOD.&lt;/p&gt;&lt;p&gt;IMO, every software designer and programmer should know all the DP even you don't use some of them. after that you got more than the DP itself. One who learns the PD should compare them together.&lt;/p&gt;&lt;p&gt;I do like this pattern very much. I got more than the pattern itself.&lt;/p&gt;&lt;p&gt;A small demo here to show the above:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;namespace&lt;/span&gt; Command&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Receiver&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Action()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Execute Action&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Command&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;protected&lt;/span&gt; Receiver receiver;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Command(Receiver receiver)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.receiver = receiver;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Execute();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteCommand : Command&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; ConcreteCommand(Receiver receiver) : &lt;span style="color: #0000ff;"&gt;base&lt;/span&gt;(receiver) { }&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Execute()&lt;br /&gt;        {&lt;br /&gt;            receiver.Action();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Invoker&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; Command command;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; SetCommand(Command command)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.command = command;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ExecuteCommand()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.command.Execute();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Client&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Receiver r = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Receiver();&lt;br /&gt;            Command c = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteCommand(r);&lt;br /&gt;&lt;br /&gt;            Invoker i = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Invoker();&lt;br /&gt;            i.SetCommand(c);&lt;br /&gt;            i.ExecuteCommand();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2278613.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/06/2278613.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/05/2277333.html</id><title type="text">[review]Design Pattern:Builder</title><summary type="text">BuilderDecouple theappearance from the construct process(algorithm) for a complicated object, the algorithm can be used for so much appearance. for reusing the algorithm, for hiding the details from the client. The appearance can be showed without leaving any parts needed with the help of the unifie</summary><published>2011-12-05T14:49:00Z</published><updated>2011-12-05T14:49:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/05/2277333.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/05/2277333.html"/><content type="html">&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Builder&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Decouple the&amp;nbsp;appearance from the construct process(algorithm) for a complicated object, the algorithm can be used for so much appearance. for reusing the algorithm, for hiding the details from the client. The appearance can be showed without leaving any parts needed with the help of the unified interface provided by the Director.&lt;/p&gt;&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;When we use this pattern&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;as i told above.&lt;/p&gt;&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Roles in this pattern&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Builder: define the interface for creating the part of the product&lt;/li&gt;&lt;li&gt;ConcreteBuilder: implement the Builder interface for the very certain product, for the very given appearance. and also provide an interface for retrieve the current product that the current ConcreteBuilder is trying to build&lt;/li&gt;&lt;li&gt;Director:define the interface to create the product by the interface provided by the Builder, the interface &amp;nbsp;design the algorithm about how the product is created &amp;nbsp;and how to use the part of the interface implemented by the ConcreteBuilder, the order how the parts show themself&lt;/li&gt;&lt;li&gt;Product: the one we want to create.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Demo&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;namespace&lt;/span&gt; Builder&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        List&amp;lt;&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;&amp;gt; parts = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Add(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; part)&lt;br /&gt;        {&lt;br /&gt;            parts.Add(part);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ShowProduct()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; part &lt;span style="color: #0000ff;"&gt;in&lt;/span&gt; parts)&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine(part);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Builder&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderA();&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderB();&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;abstract&lt;/span&gt; Product GetResult();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteBuilder1 : Builder&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; Product product = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Product();&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderA()&lt;br /&gt;        {&lt;br /&gt;            product.Add(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;1A&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderB()&lt;br /&gt;        {&lt;br /&gt;            product.Add(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;1B&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; Product GetResult()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; ConcreteBuilder2 : Builder&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; Product product = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Product();&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderA()&lt;br /&gt;        {&lt;br /&gt;            product.Add(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;2A&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; BuilderB()&lt;br /&gt;        {&lt;br /&gt;            product.Add(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;2B&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;override&lt;/span&gt; Product GetResult()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Director&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Construct(Builder builder)&lt;br /&gt;        {&lt;br /&gt;            builder.BuilderA();&lt;br /&gt;            builder.BuilderB();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Client&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Director director = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Director();&lt;br /&gt;            Builder b1 = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteBuilder1();&lt;br /&gt;            Builder b2 = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; ConcreteBuilder2();&lt;br /&gt;&lt;br /&gt;            director.Construct(b1);&lt;br /&gt;            Product p1 = b1.GetResult();&lt;br /&gt;            p1.ShowProduct();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            director.Construct(b2);&lt;br /&gt;            Product p2 = b2.GetResult();&lt;br /&gt;            p2.ShowProduct();&lt;br /&gt;&lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/div&gt;&lt;p&gt;IMO, this pattern can be learnt with the Factory, the Template and the Strategy... They are some kind similar to each other. BUT they are different, it is interesting to&amp;nbsp;differentiate them. and it is also useful to have a better comprehension and understanding for all the pattern and OOP if you dig them together.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2277333.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/05/2277333.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/heaiping/archive/2011/12/01/2271283.html</id><title type="text">[review]Design Pattern:Memento</title><summary type="text">MementoMemento as its name describes, which is for storing something. for recorving something from it.We want to capture the type's inner state at a given time and also want to recove it to where it was captured but without jeopardizing the encapsulation withoutexposing the type's details to</summary><published>2011-12-01T15:01:00Z</published><updated>2011-12-01T15:01:00Z</updated><author><name>小AI</name><uri>http://www.cnblogs.com/heaiping/</uri></author><link rel="alternate" href="http://www.cnblogs.com/heaiping/archive/2011/12/01/2271283.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/heaiping/archive/2011/12/01/2271283.html"/><content type="html">&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Memento&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Memento as its name describes, which is for storing something. for recorving something from it.&lt;/p&gt;&lt;p&gt;We want to capture the type's inner state at a given time and also want to recove it to where it was captured but without jeopardizing the encapsulation without&amp;nbsp;exposing the type's details to the client. The memento pattern makes it happen.&lt;/p&gt;&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;When we use this pattern&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;for storing the state first and recover the state from where it stored. We can do it with the normal method with a temp&amp;nbsp;variable(instance) of the type and use the getter and setter to finish it. like below:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt; &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; MyClient&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; StoreWithTheNormalWay()&lt;br /&gt;        {&lt;br /&gt;            MyType myType = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; MyType();&lt;br /&gt;            myType.MyState = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;First&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;            myType.Show();&lt;br /&gt;&lt;br /&gt;            MyType tempType = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; MyType();&lt;br /&gt;&lt;br /&gt;            tempType.MyState = myType.MyState;&lt;br /&gt;&lt;br /&gt;            myType.MyState = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Changed&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;            myType.Show();&lt;br /&gt;&lt;br /&gt;            myType.MyState = tempType.MyState;&lt;br /&gt;            myType.Show();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; MyType&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; myState;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; MyState&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; myState; }&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;set&lt;/span&gt; { myState = value; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Show()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(myState);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/div&gt;&lt;p&gt;The above code does make what we want with a temporary instance of MyType. and just reassign the state from the temporary to the myType. But you can find it was ugly with exposing the details of the MyType to the Client. what's worse? if the MyType changes it structure( and a field or remove a field. etc.) we got to update the client code. it is verbose and hard. How we resolve this. It is time for the memento pattern.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Actually the memento does use the temporary mechanism, but the memento encapsulate the details of the type to itself and let the Caretaker hold it. It is amazing.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="text-decoration: underline;"&gt;Roles in this pattern&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Originator:responsible for creating the memento and recovering from the it&lt;/li&gt;&lt;li&gt;Memento: the very memento to restore the state of the originator at a given time&lt;/li&gt;&lt;li&gt;Caretaker:Responsible for hold the mementor but have no access to update the memento&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;A small demo to show what i have praised above:&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;namespace&lt;/span&gt; Memento&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Memento&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Memento(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; state)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = state;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;get&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Originator&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state; }&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;set&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = value; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Memento CreateMemento()&lt;br /&gt;        {&lt;br /&gt;            Memento memento = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Memento(state);&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; memento;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; SetMemneto(Memento memento)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state = memento.State;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ShowState()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.state);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Caretaker&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; Memento memento;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Memento Memento&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.memento; }&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;set&lt;/span&gt; { &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;.memento = value; }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Originator originator = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Originator();&lt;br /&gt;            originator.State = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;First&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;            originator.ShowState();&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;show "First"&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;            Caretaker caretaker = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Caretaker();&lt;br /&gt;            caretaker.Memento = originator.CreateMemento();&lt;br /&gt;&lt;br /&gt;            originator.State = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Changed&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;            originator.ShowState();&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;show "Changed"&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            originator.SetMemneto(caretaker.Memento);&lt;br /&gt;            originator.ShowState();&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;show "First" AGAIN&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; MyClient&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; StoreWithTheNormalWay()&lt;br /&gt;    {&lt;br /&gt;        MyType myType = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; MyType();&lt;br /&gt;        myType.MyState = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;First&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;        myType.Show();&lt;br /&gt;&lt;br /&gt;        MyType tempType = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; MyType();&lt;br /&gt;&lt;br /&gt;        tempType.MyState = myType.MyState;&lt;br /&gt;&lt;br /&gt;        myType.MyState = &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Changed&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;;&lt;br /&gt;        myType.Show();&lt;br /&gt;&lt;br /&gt;        myType.MyState = tempType.MyState;&lt;br /&gt;        myType.Show();&lt;br /&gt;    }&lt;br /&gt;}&lt;/div&gt;&lt;p&gt;The newer version with the memento is better and more clarified. Let the originator store and recover by itself.and client even does not know how it works and also does not know the details of the originator which is one of the principles of the OOP.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/heaiping/aggbug/2271283.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/heaiping/archive/2011/12/01/2271283.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
