﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-beautykingdom-随笔分类-Designed Patterns</title><link>http://www.cppblog.com/beautykingdom/category/12496.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 09 Jun 2011 17:47:22 GMT</lastBuildDate><pubDate>Thu, 09 Jun 2011 17:47:22 GMT</pubDate><ttl>60</ttl><item><title>How I explained Design Patterns to my wife《reprint》</title><link>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148264.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Wed, 08 Jun 2011 06:14:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148264.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/148264.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148264.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/148264.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/148264.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; text-indent: 0px; letter-spacing: normal; border-collapse: separate; font: 16px 'Times New Roman'; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="line-height: 16px; font-family: Verdana, Arial, sans-serif; font-size: 13px" class="Apple-style-span"> 
<h2 style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; color: rgb(255,153,0); font-size: 13pt; font-weight: bold">Introduction</h2>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Me and my wife had some interesting conversations on<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(0,76,213); text-decoration: none" href="http://www.codeproject.com/KB/architecture/SOLIDPrinciplesInOOD.aspx">Object Oriented Design principles</a>. After publishing the conversation on CodeProject, I got some good responses from the community and that really inspired me. So, I am happy to share our next conversation that took place on Object Oriented Design Patterns. Here it is.</p>
<h2 style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; color: rgb(255,153,0); font-size: 13pt; font-weight: bold">What is a Design Pattern?</h2>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: I guess you already have some basic idea about Object Oriented Design principles. We had a nice talk on the OOD principles (SOLID principles), and I hope you didn't mind that I published our conversation in a CodeProject article. You can find it here:<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(0,76,213); text-decoration: none" href="http://www.codeproject.com/KB/architecture/SOLIDPrinciplesInOOD.aspx">How I explained OOD to my wife</a>.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Design Patterns are nothing but applications of those principles in some specific and common situations, and standardizing some of those. Let's try to understand what Design Patterns are by using some examples.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Sure, I love examples.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Let's talk about our car. It's an object, though a complex one, which consists of thousands of other objects such as the engine, wheels, steering, seats, body, and thousands of different parts and machinery.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/Engine.jpg" width="150" height="150" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/Wheel.jpg" width="150" height="99" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/Parts.jpg" width="150" height="112" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/lights.jpg" width="98" height="150" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/steering.jpg" width="150" height="93" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Different parts of a car.</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">While building this car, the manufacturer gathered and assembled all the different smaller parts that are subsystems of the car. These different smaller parts are also some complex objects, and some other manufacturers had to build and assemble those too. But, while building the car, the car company doesn't really bother too much about how those objects were built and assembled (well, as long as they are sure about the quality of these smaller objects/equipments). Rather, the car manufacturer cares about how to assemble those different objects into different combinations and produce different models of cars.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/car1.jpg" width="150" height="74" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/car2.jpg" width="150" height="78" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/car3.jpg" width="150" height="98" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Different models of cars, produced by assembling different parts and following different designs.</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: The car manufacturer company must have some designs or blue prints for each different model of car which they follow, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Definitely, and, these designs are well-thought designs, and they've put a good amount of time and effort to sketch those designs. Once the designs are finalized, producing a car is just a matter of following the designs.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Hm.. it's good to have some good designs upfront and following those allows to produce different products in a quick amount of time, and each time the manufacturer has to build a product for a specific model, they don't have to develop a design from scratch or re-invent the wheel, they just follow the designs.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/plan1.jpg" width="249" height="149" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/plan2.jpg" width="286" height="176" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Different design plans for producing different models of products (cars).</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: You got the point. Now, we are software manufacturers and we build different kinds of software programs with different components or functionality based upon the requirements. While building such different software systems, we often have to develop code for some situations that are common in many different software systems, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Yes. And often, we face common design problems while developing different software applications.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: We try to develop our software applications in an Object Oriented manner and try to apply OOD principles for achieving code that is manageable, reusable, and expandable. Wouldn't it be nice whenever we see such design problems, we have a pool of some carefully made and well tested designs of objects for solving those?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Yes, that would save us time and would also allow us to build better software systems and manage them later.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Perfect. The good news is, you don't have to really develop that pool of object designs from scratch. People already have gone through similar design problems for years, and they already have identified some good design solutions which have been standardized already. We call these Design Patterns.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">We must thank the Gang of Four (GoF) for identifying the 23 basic Design Patterns in their book<span class="Apple-converted-space">&nbsp;</span><strong>Design Patterns: Elements of Reusable Object-Oriented Software</strong>. In case you are wondering who formed this famous gang, they are Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. There are many Object Oriented Design Patterns, but these 23 patterns are generally considered the foundation for all other Design Patterns.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Can I create a new pattern? Is that possible?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Yes darling, why not? Design Patterns are not something invented or newly created by scientists. They are just discovered. That means, for each kind of common problem scenario, there must be some good design solutions there. If we are able to identify an object oriented design that could solve a new design related problem, that would be a new Design Pattern defined by us. Who knows? If we discover some a Design Pattern, someday people may call us Gang of Two.. Ha ha.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Fahana</strong>: :)</p>
<h2 style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; color: rgb(255,153,0); font-size: 13pt; font-weight: bold">How will we learn Design Patterns?</h2>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: As I have always believed, examples are the greatest way of learning. In our learning approach, we won't discuss the theories first and implement later. I think this is a BAD approach. Design Patterns were not invented based on theories. Rather, the problem situations occurred first and based upon the requirement and context, some design solutions were evolved, and later some of them were standardized as patterns. So, for each design pattern we discuss, we will try to understand and analyze some real life example problems, and then we will try to formulate a design in a step by step process and end up with a design that will match with some patterns; Design Patterns were discovered in this same process. What do you think?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: I think this approach makes more sense to me. If I can end up with Design Patterns by analyzing problems and formulating solutions, I won't have to memorize design diagrams and definitions. Please proceed using your approach.</p>
<h2 style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; color: rgb(255,153,0); font-size: 13pt; font-weight: bold">A basic design problem and its solution</h2>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Let's consider the following scenario:</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Our room has some electric equipments (lights, fans etc). The equipments are arranged in a way where they could be controlled by switches. At any time, you can replace or troubleshoot an electrical equipment without touching the other things. For example, you can replace a light with another without replacing or changing the switch. Also, you can replace a switch or troubleshoot it without touching or changing the corresponding light or fan; you can even connect the light with the fan's switch and connect the fan with the light's switch, without touching the switches.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/fan.jpg" width="258" height="196" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/light.jpg" width="129" height="215" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Electrical equipments: A fan and a light.</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/normalswitch.jpg" width="186" height="191" complete="true"  alt="" /><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/fancyswitch.jpg" width="155" height="147" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Two different switches for fan and light, one is normal and the other is fancy.</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Yes, but that's natural, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Yes, that's very natural, and that's how the arrangement should be. When different things are connected together, they should be connected in a way where change or replacement of one system doesn't affect another, or even if there is any effect, it stays minimal. This allows you to manage your system easily and at low cost. Just imagine if changing the light in your room requires you to change the switch also. Would you care to purchase and set up such a system in your house?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Definitely no.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Now, let's think how the lights or fans are connected with the switches so that changing one doesn't have any impact on the other. What do you think?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: The wire, of course!</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Perfect. It's the wire and the electrical arrangement that connect the lights/fans with the switches. We can generalize it as a bridge between the different systems that can get connected through it. The basic idea is, things shouldn't be directly connected with one another. Rather, they should be connected though some bridges or interfaces. That's what we call "loose coupling" in software world.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: I see. I got the idea.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Now, let's try to understand some key issues in the light/fan and switch analogy, and try to understand how they are designed and connected.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: OK, let me try.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">We have switches in our example. There may be some specific kinds of switches like normal switches, fancy ones, but, in general, they are switches. And, each switch can be turned on and off.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">So, we will have a base<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span class="Apple-converted-space">&nbsp;</span>class as follows:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain0" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg0" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="0"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse0" preid="0"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre0" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> Switch
{
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> On()
  { 
    <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Switch has an on button
</span>  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> Off()
  {
    <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Switch has an off button
</span>  }
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">And, as we may have some specific kinds of switches, for example a fancy switch, a normal switch etc., we will also have<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span class="Apple-converted-space">&nbsp;</span>classes extending the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span class="Apple-converted-space">&nbsp;</span>class:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain1" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg1" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="1"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse1" preid="1"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre1" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> NormalSwitch : Switch
{
}

<span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> FancySwitch : Switch
{
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">These two specific switch classes may have their own specific features and behaviours, but for now, let's keep them simple.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Cool. Now, what about fan and light?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Let me try. I learned from the Open Closed principles from Object Oriented Design principles that we should try to do abstractions whenever possible, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Right.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Unlike switches, fan and light are two different things. For switches, we were able to use a base<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span class="Apple-converted-space">&nbsp;</span>class, but as fan and light are two different things, instead of defining a base class, an interface might be more appropriate. In general, they are all electrical equipments. So, we can define an interface, say,<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code>, for abstracting fans and lights, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Right.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: OK, each electrical equipment has some common functionality. They could all be turned on or off. So the interface may be as follows:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain2" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg2" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="2"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse2" preid="2"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre2" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">interface</span> IElectricalEquipment
{
    <span style="color: blue" class="code-keyword">void</span> PowerOn(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Each electrical equipment can be turned on
</span>    <span style="color: blue" class="code-keyword">void</span> PowerOff(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Each electrical equipment can be turned off
</span>}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Great. You are getting good at abstracting things. Now, we need a bridge. In real world, the wires are the bridges. But, in our object design, a switch knows how to turn on or off an electrical equipment, and the electrical equipment somehow needs to be connected with the switches, As we don't have any wire here, the only way to let the electrical equipment be connected with the switch is encapsulation.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Yes, but switches don't know the fans or lights directly. A switch actually knows about an electrical equipment<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span class="Apple-converted-space">&nbsp;</span>that it can turn on or off. So, that means, an<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">ISwitch</code><span class="Apple-converted-space">&nbsp;</span>should have an<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span class="Apple-converted-space">&nbsp;</span>instance, right?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Right. Here, the encapsulated instance, which is an abstraction of fan or light (<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code>) is the bridge. So, let's modify the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span class="Apple-converted-space">&nbsp;</span>class to encapsulate an electrical equipment:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain3" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg3" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="3"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse3" preid="3"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre3" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> Switch
{
  <span style="color: blue" class="code-keyword">public</span> IElectricalEquipment equipment
  {
    <span style="color: blue" class="code-keyword">get</span>;
    <span style="color: blue" class="code-keyword">set</span>;
  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> On()
  {
    <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Switch has an on button
</span>  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> Off()
  {
    <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Switch has an off button
</span>  }
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Understood. Let me try to define the actual electrical equipments, the fan and the light. As I see, these are electrical equipments in general, so these would simply implement the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code>interface.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Following is the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span class="Apple-converted-space">&nbsp;</span>class:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain4" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg4" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="4"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse4" preid="4"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre4" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> Fan : IElectricalEquipment
{
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> PowerOn()
  {
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Fan is on"</span>);
  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> PowerOff()
  {
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Fan is off"</span>);
  }
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">And, the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span class="Apple-converted-space">&nbsp;</span>class would be as follows:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain5" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg5" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="5"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse5" preid="5"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre5" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> Light : IElectricalEquipment
{
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> PowerOn()
  {    
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Light is on"</span>);
  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> PowerOff()
  {
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Light is off"</span>);
  }
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Great. Now, let's make switches work. The switches should have the ability inside them to turn on and turn off the electrical equipment (it is connected to) when the switch is turned on and off.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">These are the key issues:</p>
<ul><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">When the<span class="Apple-converted-space">&nbsp;</span><em>On</em><span class="Apple-converted-space">&nbsp;</span>button is pressed on the switch, the electrical equipment connected to it should be turned on.</li><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">When the<span class="Apple-converted-space">&nbsp;</span><em>Off</em><span class="Apple-converted-space">&nbsp;</span>button is pressed on the switch, the electrical equipment connected to it should be turned off.</li></ul>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Basically, following is what we want to achieve:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain6" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg6" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="6"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse6" preid="6"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre6" lang="cs"><span style="color: blue" class="code-keyword">static</span> <span style="color: blue" class="code-keyword">void</span> Main(<span style="color: blue" class="code-keyword">string</span>[] args)
{
  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">We have some electrical equipments, say Fan, Light etc.
</span>  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">So, lets create them first.
</span>
  IElectricalEquipment fan = <span style="color: blue" class="code-keyword">new</span> Fan();
  IElectricalEquipment light = <span style="color: blue" class="code-keyword">new</span> Light();

  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">We also have some switches. Lets create them too.
</span>
  Switch fancySwitch = <span style="color: blue" class="code-keyword">new</span> FancySwitch();
  Switch normalSwitch = <span style="color: blue" class="code-keyword">new</span> NormalSwitch();

  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Lets connect the Fan to the fancy switch
</span>
  fancySwitch.equipment = fan;

  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">As the switch now has an equipment (Fan),
</span>  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">so switching on or off should 
</span>  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">turn on or off the electrical equipment  
</span>
  fancySwitch.On(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should turn on the Fan. 
</span>
  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">so, inside the On() method of Switch,  
</span>  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">we must turn on the electrical equipment.
</span>    
  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should turn off the Fan. So, inside the On() method of  
</span>  fancySwitch.Off();
  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Switch, we must turn off the electrical equipment
</span>
  <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">Now, lets plug the light to the fancy switch
</span>
  fancySwitch.equipment = light;
  fancySwitch.On(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should turn on the Light now
</span>  fancySwitch.Off(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should be turn off the Light now
</span>}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: I got it. So, the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">On()</code><span class="Apple-converted-space">&nbsp;</span>method of the actual switches should internally call the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">TurnOn()</code><span class="Apple-converted-space">&nbsp;</span>method of the electrical equipment, and the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Off()</code><span class="Apple-converted-space">&nbsp;</span>should call the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">TurnOff()</code><span class="Apple-converted-space">&nbsp;</span>method on the equipment. So, the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code>class should be as follows:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain7" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg7" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="7"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse7" preid="7"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre7" lang="cs"><span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">class</span> Switch
{
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> On()
  {
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Switch on the equipment"</span>);
    equipment.PowerOn();
  }
  <span style="color: blue" class="code-keyword">public</span> <span style="color: blue" class="code-keyword">void</span> Off()
  {
    Console.WriteLine(<span style="color: purple" class="code-string">"</span><span style="color: purple" class="code-string">Switch off the equipment"</span>);
    equipment.PowerOff();
  }
}</pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: Great work. Now, this certainly allows you to plug a fan from one switch to another. But you see, the opposite should also work. That means, you can change the switch of a fan or light without touching the fan or light. For example, you can easily change the switch of the light from<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code><span class="Apple-converted-space">&nbsp;</span>to<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span class="Apple-converted-space">&nbsp;</span>as follows:</p>
<div style="text-align: right !important; display: block; font-size: 8pt !important" id="premain8" class="small-text align-right" width="100%"><img style="overflow-x: auto; overflow-y: auto; cursor: pointer" id="preimg8" src="http://www.codeproject.com/images/minus.gif" width="9" height="9" preid="8"  alt="" /><span style="margin-bottom: 0px; cursor: pointer" id="precollapse8" preid="8"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div><pre style="border-bottom: rgb(251,237,187) 1px solid; border-left: rgb(251,237,187) 1px solid; padding-bottom: 6px; overflow-x: auto; overflow-y: auto; background-color: rgb(251,237,187); margin-top: 0px; padding-left: 6px; padding-right: 6px; font: 9pt 'Courier New', Courier, mono; word-wrap: break-word; white-space: pre-wrap; border-top: rgb(251,237,187) 1px solid; border-right: rgb(251,237,187) 1px solid; padding-top: 6px" id="pre8" lang="cs">normalSwitch .equipment = light;
normalSwitch.On(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should turn on the Light now
</span>normalSwitch.Off(); <span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">//</span><span style="font-style: italic; color: rgb(0,128,0)" class="code-comment">It should be turn off the Light now</span></pre>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">So, you see, you can vary both the switches and the electrical equipments without any effect on the other, and connecting an abstraction of the electrical equipment with a switch (via encapsulation) is letting you do that. This design looks elegant and good. The Gang of Four has named this a pattern: The Bridge Pattern.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Cool. I think I've understood the idea. Basically, two systems shouldn't be connected or dependent on another directly. Rather, they should be connected or dependent via abstraction (as the Dependency Inversion principle and the Open-Closed principle say) so that they are loosely coupled, and thus we are able to change our implementation when required without much effect on the other part of the system.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: You got it perfect darling. Let's see how the Bridge Pattern is defined:</p>
<h2 style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; color: rgb(255,153,0); font-size: 13pt; font-weight: bold">"Decouple an abstraction from its implementation so that the two can vary independently"</h2>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">You will see that our design perfectly matches the definition. If you have a class designer (in Visual Studio, you can do that, and other modern IDEs should also support this feature), you will see that you have a class diagram similar to the following:</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><img style="overflow-x: auto; overflow-y: auto" src="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1/bridge.gif" width="448" height="293" complete="true"  alt="" /></p>
<div style="font-style: italic; font-size: 8pt; font-weight: bold" class="Caption">Class diagram of Bridge pattern.</div>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">Here,<span class="Apple-converted-space">&nbsp;</span><strong>Abstraction</strong><span class="Apple-converted-space">&nbsp;</span>is the base<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span class="Apple-converted-space">&nbsp;</span>class.<span class="Apple-converted-space">&nbsp;</span><strong>RefinedAbstraction</strong><span class="Apple-converted-space">&nbsp;</span>is the specific switch classes (<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code>,<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span class="Apple-converted-space">&nbsp;</span>etc.).<span class="Apple-converted-space">&nbsp;</span><strong>Implementor</strong><span class="Apple-converted-space">&nbsp;</span>is the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span class="Apple-converted-space">&nbsp;</span>interface.<strong>ConcreteImplementorA</strong><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><strong>ConcreteImplementorB</strong><span class="Apple-converted-space">&nbsp;</span>are the<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Light</code><span class="Apple-converted-space">&nbsp;</span>classes.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Let me ask you a question, just curious. There are many other patterns as you said, why did you start with the Bridge pattern? Any important reason?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: A very good question. Yes, I started with the Bridge pattern and not any other pattern (unlike many others) because of a reason. I believe the Bridge pattern is the base of all Object Oriented Design Patterns. You see:</p>
<ul><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">It teaches how to think abstract, which is the key concept of all Object Oriented Design Patterns.</li><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">It implements the basic OOD principles.</li><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">It is easy to understand.</li><li style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt">If this pattern is understood correctly, learning other Design Patterns becomes easy.</li></ul>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: Do you think I have understood it correctly?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: I think you have understood it perfectly darling.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: So, what's next?</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Shubho</strong>: By understanding the Bridge pattern, we have just started to understand the concepts of Design Patterns. In our next conversation, we would learn other Design Patterns, and I hope you won't get bored learning them.</p>
<p style="line-height: 1.2em; font-family: Verdana, Arial, sans-serif; font-size: 10pt"><strong>Farhana</strong>: I won't. Believe me.<br /><br />from:<br /><a href="http://www.codeproject.com/KB/architecture/LearningDesignPatterns1.aspx">http://www.codeproject.com/KB/architecture/LearningDesignPatterns1.aspx</a></p></span></span><img src ="http://www.cppblog.com/beautykingdom/aggbug/148264.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2011-06-08 14:14 <a href="http://www.cppblog.com/beautykingdom/archive/2011/06/08/148264.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How to explain OOD to my wife-code project《转载》</title><link>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148263.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Wed, 08 Jun 2011 05:45:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148263.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/148263.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2011/06/08/148263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/148263.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/148263.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: IntroductionMy wife Farhana wants to resume her career as a software developer (she started her career as a software developer, but couldn't proceed much because of our first child's birth), and the...&nbsp;&nbsp;<a href='http://www.cppblog.com/beautykingdom/archive/2011/06/08/148263.html'>阅读全文</a><img src ="http://www.cppblog.com/beautykingdom/aggbug/148263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2011-06-08 13:45 <a href="http://www.cppblog.com/beautykingdom/archive/2011/06/08/148263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>追MM与设计模式的有趣见解</title><link>http://www.cppblog.com/beautykingdom/archive/2009/12/05/102587.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Sat, 05 Dec 2009 02:43:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2009/12/05/102587.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/102587.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2009/12/05/102587.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/102587.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/102587.html</trackback:ping><description><![CDATA[<div><span><strong><span>创建型模式 </span></strong></span><span><br><br><span><strong><span>1</span></strong></span></span><span><strong><span>、<span>FACTORY </span></span></strong></span><span><span>—</span></span><span><span>追<span>MM</span>少不了请吃饭了，麦当劳的鸡翅和肯德基的鸡翅都是<span>MM</span>爱吃的东西，虽然口味有所不同，但不管你带<span>MM</span>去麦当劳或肯德基，只管向服务员说<span>&#8220;</span>来四个鸡翅<span>&#8221;</span>就行了。麦当劳和肯德基就是生产鸡翅的<span>Factory </span></span></span><span><br><br></span><span><strong><span>工厂模式</span></strong></span><span><span>：客户类和工厂类分开。消费者任何时候需要某种产品，只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时，工厂类也要做相应的修改。如：如何创建及如何向客户端提供。 </span></span><span><br><br><span><strong><span>2</span></strong></span></span><span><strong><span>、<span>BUILDER&nbsp;</span></span></strong></span><span><span>—MM</span></span><span><span>最爱听的就是<span>&#8220;</span>我爱你<span>&#8221;</span>这句话了，见到不同地方的<span>MM,</span>要能够用她们的方言跟她说这句话哦，我有一个多种语言翻译机，上面每种语言都有一个按键，见到<span>MM</span>我只要按对应的键，它就能够用相应的语言说出<span>&#8220;</span>我爱你<span>&#8221;</span>这句话了，国外的<span>MM</span>也可以轻松搞掂，这就是我的<span>&#8220;</span>我爱你<span>&#8221;builder</span>。（这一定比美军在伊拉克用的翻译机好卖） </span></span><span><br><br></span><span><strong><span>建造模式</span></strong></span><span><span>：将产品的内部表象和产品的生成过程分割开来，从而使一个建造过程生成具有不同的内部表象的产品对象。建造模式使得产品内部表象可以独立的变化，客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。 </span></span><span><br><br><span><strong><span>3</span></strong></span></span><span><strong><span>、<span>FACTORY METHOD</span></span></strong></span><span><span> —</span></span><span><span>请<span>MM</span>去麦当劳吃汉堡，不同的<span>MM</span>有不同的口味，要每个都记住是一件烦人的事情，我一般采用<span>Factory Method</span>模式，带着<span>MM</span>到服务员那儿，说<span>&#8220;</span>要一个汉堡<span>&#8221;</span>，具体要什么样的汉堡呢，让<span>MM</span>直接跟服务员说就行了。 </span></span><span><br><br></span><span><strong><span>工厂方法模式</span></strong></span><span><span>：核心工厂类不再负责所有产品的创建，而是将具体创建的工作交给子类去做，成为一个抽象工厂角色，仅负责给出具体工厂类必须实现的接口，而不接触哪一个产品类应当被实例化这种细节。 </span></span><span><br><br><span><strong><span>4</span></strong></span></span><span><strong><span>、<span>PROTOTYPE</span></span></strong></span><span><span> —</span></span><span><span>跟<span>MM</span>用<span>QQ</span>聊天，一定要说些深情的话语了，我搜集了好多肉麻的情话，需要时只要<span>copy</span>出来放到<span>QQ</span>里面就行了，这就是我的情话<span>prototype</span>了。（<span>100</span>块钱一份，你要不要） </span></span><span><br><br></span><span><strong><span>原始模型模式</span></strong></span><span><span>：通过给出一个原型对象来指明所要创建的对象的类型，然后用复制这个原型对象的方法创建出更多同类型的对象。原始模型模式允许动态的增加或减少产品类，产品类不需要非得有任何事先确定的等级结构，原始模型模式适用于任何的等级结构。缺点是每一个类都必须配备一个克隆方法。 </span></span><span><br><br><span><strong><span>5</span></strong></span></span><span><strong><span>、<span>SINGLETON</span></span></strong></span><span><span> —</span></span><span><span>俺有<span>6</span>个漂亮的老婆，她们的老公都是我，我就是我们家里的老公<span>Sigleton</span>，她们只要说道<span>&#8220;</span>老公<span>&#8221;</span>，都是指的同一个人，那就是我<span>(</span>刚才做了个梦啦，哪有这么好的事<span>) </span></span></span><span><br><br></span><span><strong><span>单例模式</span></strong></span><span><span>：单例模式确保某一个类只有一个实例，而且自行实例化并向整个系统提供这个实例单例模式。单例模式只应在有真正的<span>&#8220;</span>单一实例<span>&#8221;</span>的需求时才可使用。 </span></span><span><br><br></span><span><strong><span>结构型模式 </span></strong></span><strong><span><br></span></strong><span><br><span><strong><span>6</span></strong></span></span><span><strong><span>、<span>ADAPTER</span></span></strong></span><span><span> —</span></span><span><span>在朋友聚会上碰到了一个美女<span>Sarah</span>，从香港来的，可我不会说粤语，她不会说普通话，只好求助于我的朋友<span>kent</span>了，他作为我和<span>Sarah</span>之间的<span>Adapter</span>，让我和<span>Sarah</span>可以相互交谈了<span>(</span>也不知道他会不会耍我<span>) </span></span></span><span><br><br></span><span><strong><span>适配器模式</span></strong></span><span><span>：把一个类的接口变换成客户端所期待的另一种接口，从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。 </span></span><span><br><br><span><strong><span>7</span></strong></span></span><span><strong><span>、<span>BRIDGE </span></span></strong></span><span><span>—</span></span><span><span>早上碰到<span>MM</span>，要说早上好，晚上碰到<span>MM</span>，要说晚上好；碰到<span>MM</span>穿了件新衣服，要说你的衣服好漂亮哦，碰到<span>MM</span>新做的发型，要说你的头发好漂亮哦。不要问我<span>&#8220;</span>早上碰到<span>MM</span>新做了个发型怎么说<span>&#8221;</span>这种问题，自己用<span>BRIDGE</span>组合一下不就行了 </span></span><span><br><br></span><span><strong><span>桥梁模式</span></strong></span><span><span>：将抽象化与实现化脱耦，使得二者可以独立的变化，也就是说将他们之间的强关联变成弱关联，也就是指在一个软件系统的抽象化和实现化之间使用组合<span>/</span>聚合关系而不是继承关系，从而使两者可以独立的变化。 </span></span><span><br><br><span><strong><span>8</span></strong></span></span><span><strong><span>、<span>COMPOSITE</span></span></strong></span><span><span> —Mary</span></span><span><span>今天过生日。<span>&#8220;</span>我过生日，你要送我一件礼物。<span>&#8221;&#8220;</span>嗯，好吧，去商店，你自己挑。<span>&#8221;&#8220;</span>这件<span>T</span>恤挺漂亮，买，这条裙子好看，买，这个包也不错，买。<span>&#8221;&#8220;</span>喂，买了三件了呀，我只答应送一件礼物的哦。<span>&#8221;&#8220;</span>什么呀，<span>T</span>恤加裙子加包包，正好配成一套呀，小姐，麻烦你包起来。<span>&#8221;&#8220;&#8230;&#8230;&#8221;</span>，<span>MM</span>都会用<span>Composite</span>模式了，你会了没有？ </span></span><span><br><br></span><span><strong><span>合成模式</span></strong></span><span><span>：合成模式将对象组织到树结构中，可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。 </span></span><span><br><br><span><strong><span>9</span></strong></span></span><span><strong><span>、<span>DECORATOR </span></span></strong></span><span><span>—Mary</span></span><span><span>过完轮到<span>Sarly</span>过生日，还是不要叫她自己挑了，不然这个月伙食费肯定玩完，拿出我去年在华山顶上照的照片，在背面写上<span>&#8220;</span>最好的的礼物，就是爱你的<span>Fita&#8221;</span>，再到街上礼品店买了个像框（卖礼品的<span>MM</span>也很漂亮哦），再找隔壁搞美术设计的<span>Mike</span>设计了一个漂亮的盒子装起来<span>&#8230;&#8230;</span>，我们都是<span>Decorator</span>，最终都在修饰我这个人呀，怎么样，看懂了吗？ </span></span><span><br><br></span><span><strong><span>装饰模式</span></strong></span><span><span>：装饰模式以对客户端透明的方式扩展对象的功能，是继承关系的一个替代方案，提供比继承更多的灵活性。动态给一个对象增加功能，这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。 </span></span><span><br><br><span><strong><span>10</span></strong></span></span><span><strong><span>、<span>FA&#199;ADE</span></span></strong></span><span><span> —</span></span><span><span>我有一个专业的<span>Nikon</span>相机，我就喜欢自己手动调光圈、快门，这样照出来的照片才专业，但<span>MM</span>可不懂这些，教了半天也不会。幸好相机有<span>Facade</span>设计模式，把相机调整到自动档，只要对准目标按快门就行了，一切由相机自动调整，这样<span>MM</span>也可以用这个相机给我拍张照片了。 </span></span><span><br><br></span><span><strong><span>门面模式</span></strong></span><span><span>：外部与一个子系统的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接口，使得子系统更易于使用。每一个子系统只有一个门面类，而且此门面类只有一个实例，也就是说它是一个单例模式。但整个系统可以有多个门面类。 </span></span><span><br><br><span><strong><span>11</span></strong></span></span><span><strong><span>、<span>FLYWEIGHT</span></span></strong></span><span><span> —</span></span><span><span>每天跟<span>MM</span>发短信，手指都累死了，最近买了个新手机，可以把一些常用的句子存在手机里，要用的时候，直接拿出来，在前面加上<span>MM</span>的名字就可以发送了，再不用一个字一个字敲了。共享的句子就是<span>Flyweight</span>，<span>MM</span>的名字就是提取出来的外部特征，根据上下文情况使用。 </span></span><span><br><br></span><span><strong><span>享元模式</span></strong></span><span><span>：<span>FLYWEIGHT</span>在拳击比赛中指最轻量级。享元模式以共享的方式高效的支持大量的细粒度对象。享元模式能做到共享的关键是区分内蕴状态和外蕴状态。内蕴状态存储在享元内部，不会随环境的改变而有所不同。外蕴状态是随环境的改变而改变的。外蕴状态不能影响内蕴状态，它们是相互独立的。将可以共享的状态和不可以共享的状态从常规类中区分开来，将不可以共享的状态从类里剔除出去。客户端不可以直接创建被共享的对象，而应当使用一个工厂对象负责创建被共享的对象。享元模式大幅度的降低内存中对象的数量。 </span></span><span><br><br><span><strong><span>12</span></strong></span></span><span><strong><span>、<span>PROXY </span></span></strong></span><span><span>—</span></span><span><span>跟<span>MM</span>在网上聊天，一开头总是<span>&#8220;hi,</span>你好<span>&#8221;,&#8220;</span>你从哪儿来呀？<span>&#8221;&#8220;</span>你多大了？<span>&#8221;&#8220;</span>身高多少呀？<span>&#8221;</span>这些话，真烦人，写个程序做为我的<span>Proxy</span>吧，凡是接收到这些话都设置好了自动的回答，接收到其他的话时再通知我回答，怎么样，酷吧。 </span></span><span><br><br></span><span><strong><span>代理模式</span></strong></span><span><span>：代理模式给某一个对象提供一个代理对象，并由代理对象控制对源对象的引用。代理就是一个人或一个机构代表另一个人或者一个机构采取行动。某些情况下，客户不想或者不能够直接引用一个对象，代理对象可以在客户和目标对象直接起到中介的作用。客户端分辨不出代理主题对象与真实主题对象。代理模式可以并不知道真正的被代理对象，而仅仅持有一个被代理对象的接口，这时候代理对象不能够创建被代理对象，被代理对象必须有系统的其他角色代为创建并传入。 </span></span><span><br><br></span><span><strong><span>行为模式 </span></strong></span><strong><span><br></span></strong><span><br><span><strong><span>13</span></strong></span></span><span><strong><span>、<span>CHAIN OF RESPONSIBLEITY</span></span></strong></span><span><span> —</span></span><span><span>晚上去上英语课，为了好开溜坐到了最后一排，哇，前面坐了好几个漂亮的<span>MM</span>哎，找张纸条，写上<span>&#8220;Hi,</span>可以做我的女朋友吗？如果不愿意请向前传<span>&#8221;</span>，纸条就一个接一个的传上去了，糟糕，传到第一排的<span>MM</span>把纸条传给老师了，听说是个老处女呀，快跑<span>! </span></span></span><span><br><br></span><span><strong><span>责任链模式</span></strong></span><span><span>：在责任链模式中，很多对象由每一个对象对其下家的引用而接起来形成一条链。请求在这个链上传递，直到链上的某一个对象决定处理此请求。客户并不知道链上的哪一个对象最终处理这个请求，系统可以在不影响客户端的情况下动态的重新组织链和分配责任。处理者有两个选择：承担责任或者把责任推给下家。一个请求可以最终不被任何接收端对象所接受。 </span></span><span><br><br><span><strong><span>14</span></strong></span></span><span><strong><span>、<span>COMMAND</span></span></strong></span><span><span> —</span></span><span><span>俺有一个<span>MM</span>家里管得特别严，没法见面，只好借助于她弟弟在我们俩之间传送信息，她对我有什么指示，就写一张纸条让她弟弟带给我。这不，她弟弟又传送过来一个<span>COMMAND</span>，为了感谢他，我请他吃了碗杂酱面，哪知道他说：<span>&#8220;</span>我同时给我姐姐三个男朋友送<span>COMMAND</span>，就数你最小气，才请我吃面。<span>&#8221;</span>，</span></span><span><shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"></shapetype> <stroke joinstyle="miter"></stroke>
<formulas>
</formulas>
<f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f>
<path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path>
<lock aspectratio="t" v:ext="edit"></lock><shape id=_x0000_i1025 alt="Sad" type="#_x0000_t75"></shape><br><br></span><span><strong><span>命令模式</span></strong></span><span><span>：命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任和执行命令的责任分割开，委派给不同的对象。命令模式允许请求的一方和发送的一方独立开来，使得请求的一方不必知道接收请求的一方的接口，更不必知道请求是怎么被接收，以及操作是否执行，何时被执行以及是怎么被执行的。系统支持命令的撤消。 </span></span><span><br><br><span><strong><span>15</span></strong></span></span><span><strong><span>、<span>INTERPRETER</span></span></strong></span><span><span> —</span></span><span><span>俺有一个《泡<span>MM</span>真经》，上面有各种泡<span>MM</span>的攻略，比如说去吃西餐的步骤、去看电影的方法等等，跟<span>MM</span>约会时，只要做一个<span>Interpreter</span>，照着上面的脚本执行就可以了。 </span></span><span><br><br></span><span><strong><span>解释器模式</span></strong></span><span><span>：给定一个语言后，解释器模式可以定义出其文法的一种表示，并同时提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。解释器模式将描述怎样在有了一个简单的文法后，使用模式设计解释这些语句。在解释器模式里面提到的语言是指任何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表文法的命令类的等级结构，也就是一系列的组合规则。每一个命令对象都有一个解释方法，代表对命令对象的解释。命令对象的等级结构中的对象的任何排列组合都是一个语言。 </span></span><span><br><br><span><strong><span>16</span></strong></span></span><span><strong><span>、<span>ITERATOR</span></span></strong></span><span><span> —</span></span><span><span>我爱上了<span>Mary</span>，不顾一切的向她求婚。 </span></span><span><br><span>&nbsp;&nbsp; </span><span>Mary</span></span><span><span>：<span>&#8220;</span>想要我跟你结婚，得答应我的条件<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span></span><span><span>我：<span>&#8220;</span>什么条件我都答应，你说吧<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span><span>Mary</span></span><span><span>：<span>&#8220;</span>我看上了那个一克拉的钻石<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span></span><span><span>我：<span>&#8220;</span>我买，我买，还有吗？<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span><span>Mary</span></span><span><span>：<span>&#8220;</span>我看上了湖边的那栋别墅<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span></span><span><span>我：<span>&#8220;</span>我买，我买，还有吗？<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span><span>Mary</span></span><span><span>：<span>&#8220;</span>你的小弟弟必须要有<chmetcnv unitname="cm" sourcevalue="50" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on"></chmetcnv><span>50cm</span>长<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span></span><span><span>我脑袋嗡的一声，坐在椅子上，一咬牙：<span>&#8220;</span>我剪，我剪，还有吗？<span>&#8221; </span></span></span><span><br><span>&nbsp;&nbsp; </span><span>&#8230;&#8230; </span><br><br></span><span><strong><span>迭代子模式</span></strong></span><span><span>：迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集，聚集对象是能够包容一组对象的容器对象。迭代子模式将迭代逻辑封装到一个独立的子对象中，从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象，每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。 </span></span><span><br><br><span><strong><span>17</span></strong></span></span><span><strong><span>、<span>MEDIATOR</span></span></strong></span><span><strong><span> </span></strong></span><span><span>—</span></span><span><span>四个<span>MM</span>打麻将，相互之间谁应该给谁多少钱算不清楚了，幸亏当时我在旁边，按照各自的筹码数算钱，赚了钱的从我这里拿，赔了钱的也付给我，一切就<span>OK</span>啦，俺得到了四个<span>MM</span>的电话。 </span></span><span><br><br></span><span><strong><span>调停者模式</span></strong></span><span><span>：调停者模式包装了一系列对象相互作用的方式，使得这些对象不必相互明显作用。从而使他们可以松散偶合。当某些对象之间的作用发生改变时，不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。调停者模式将多对多的相互作用转化为一对多的相互作用。调停者模式将对象的行为和协作抽象化，把对象在小尺度的行为上与其他对象的相互作用分开处理。 </span></span><span><br><br><span><strong><span>18</span></strong></span></span><span><strong><span>、<span>MEMENTO</span></span></strong></span><span><span> —</span></span><span><span>同时跟几个<span>MM</span>聊天时，一定要记清楚刚才跟<span>MM</span>说了些什么话，不然<span>MM</span>发现了会不高兴的哦，幸亏我有个备忘录，刚才与哪个<span>MM</span>说了什么话我都拷贝一份放到备忘录里面保存，这样可以随时察看以前的记录啦。 </span></span><span><br><br></span><span><strong><span>备忘录模式</span></strong></span><span><span>：备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下，将一个对象的状态捉住，并外部化，存储起来，从而可以在将来合适的时候把这个对象还原到存储起来的状态。 </span></span><span><br><br><span><strong><span>19</span></strong></span></span><span><strong><span>、<span>OBSERVER</span></span></strong></span><span><span>&nbsp;—</span></span><span><span>想知道咱们公司最新<span>MM</span>情报吗？加入公司的<span>MM</span>情报邮件组就行了，<span>tom</span>负责搜集情报，他发现的新情报不用一个一个通知我们，直接发布给邮件组，我们作为订阅者（观察者）就可以及时收到情报啦 </span></span><span><br><br></span><span><strong><span>观察者模式</span></strong></span><span><span>：观察者模式定义了一种一队多的依赖关系，让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时，会通知所有观察者对象，使他们能够自动更新自己。 </span></span><span><br><br><span><strong>20</strong></span></span><span><strong><span>、<span>STATE</span></span></strong></span><span><span> —</span></span><span><span>跟<span>MM</span>交往时，一定要注意她的状态哦，在不同的状态时她的行为会有不同，比如你约她今天晚上去看电影，对你没兴趣的<span>MM</span>就会说<span>&#8220;</span>有事情啦<span>&#8221;</span>，对你不讨厌但还没喜欢上的<span>MM</span>就会说<span>&#8220;</span>好啊，不过可以带上我同事么？<span>&#8221;</span>，已经喜欢上你的<span>MM</span>就会说<span>&#8220;</span>几点钟？看完电影再去泡吧怎么样？<span>&#8221;</span>，当然你看电影过程中表现良好的话，也可以把<span>MM</span>的状态从不讨厌不喜欢变成喜欢哦。 </span></span><span><br><br></span><span><strong><span>状态模式</span></strong></span><span><span>：状态模式允许一个对象在其内部状态改变的时候改变行为。这个对象看上去象是改变了它的类一样。状态模式把所研究的对象的行为包装在不同的状态对象里，每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候，其行为也随之改变。状态模式需要对每一个系统可能取得的状态创立一个状态类的子类。当系统的状态变化时，系统便改变所选的子类。 </span></span><span><br><br><span><strong><span>21</span></strong></span></span><span><strong><span>、<span>STRATEGY</span></span></strong></span><span><span> —</span></span><span><span>跟不同类型的<span>MM</span>约会，要用不同的策略，有的请电影比较好，有的则去吃小吃效果不错，有的去海边浪漫最合适，单目的都是为了得到<span>MM</span>的芳心，我的追<span>MM</span>锦囊中有好多<span>Strategy</span>哦。 </span></span><span><br><br></span><span><strong><span>策略模式</span></strong></span><span><span>：策略模式针对一组算法，将每一个算法封装到具有共同接口的独立的类中，从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。策略模式把行为和环境分开。环境类负责维持和查询行为类，各种算法在具体的策略类中提供。由于算法和环境独立开来，算法的增减，修改都不会影响到环境和客户端。 </span></span><span><br><br><span><strong><span>22</span></strong></span></span><span><strong><span>、<span>TEMPLATE METHOD</span></span></strong></span><span><span> </span></span><span><span>——</span></span><span><span>看过《如何说服女生上床》这部经典文章吗？女生从认识到上床的不变的步骤分为巧遇、打破僵局、展开追求、接吻、前戏、动手、爱抚、进去八大步骤<span>(Template method)</span>，但每个步骤针对不同的情况，都有不一样的做法，这就要看你随机应变啦<span>(</span>具体实现<span>)</span>； </span></span><span><br><br></span><span><strong><span>模板方法模式</span></strong></span><span><span>：模板方法模式准备一个抽象类，将部分逻辑以具体方法以及具体构造子的形式实现，然后声明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法，从而对剩余的逻辑有不同的实现。先制定一个顶级逻辑框架，而将逻辑的细节留给具体的子类去实现。 </span></span><span><br><br><span><strong><span>23</span></strong></span></span><span><strong><span>、<span>VISITOR</span></span></strong></span><span><span> —</span></span><span><span>情人节到了，要给每个<span>MM</span>送一束鲜花和一张卡片，可是每个<span>MM</span>送的花都要针对她个人的特点，每张卡片也要根据个人的特点来挑，我一个人哪搞得清楚，还是找花店老板和礼品店老板做一下<span>Visitor</span>，让花店老板根据<span>MM</span>的特点选一束花，让礼品店老板也根据每个人特点选一张卡，这样就轻松多了； </span></span><span><br><br></span><span><strong><span>访问者模式</span></strong></span><span><span>：访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话，接受这个操作的数据结构可以保持不变。访问者模式适用于数据结构相对未定的系统，它把数据结构和作用于结构上的操作之间的耦合解脱开，使得操作集合可以相对自由的演化。访问者模式使得增加新的操作变的很容易，就是增加一个新的访问者类。访问者模式将有关的行为集中到一个访问者对象中，而不是分散到一个个的节点类中。当使用访问者模式时，要将尽可能多的对象浏览逻辑放在访问者类中，而不是放到它的子类中。访问者模式可以跨过几个类的等级结构访问属于不同的等级结构的成员类。</span></span><span> </span></div>
<img src ="http://www.cppblog.com/beautykingdom/aggbug/102587.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2009-12-05 10:43 <a href="http://www.cppblog.com/beautykingdom/archive/2009/12/05/102587.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>