??xml version="1.0" encoding="utf-8" standalone="yes"?>精品综合久久久久久888蜜芽,91精品国产色综久久 ,国产精品成人无码久久久久久 http://www.shnenglu.com/beautykingdom/category/12496.htmlzh-cnThu, 09 Jun 2011 17:47:22 GMTThu, 09 Jun 2011 17:47:22 GMT60How I explained Design Patterns to my wife《reprint?/title><link>http://www.shnenglu.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.shnenglu.com/beautykingdom/archive/2011/06/08/148264.html</guid><wfw:comment>http://www.shnenglu.com/beautykingdom/comments/148264.html</wfw:comment><comments>http://www.shnenglu.com/beautykingdom/archive/2011/06/08/148264.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/beautykingdom/comments/commentRss/148264.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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 id="j99rdvl" class="Apple-converted-space"> </span><a style="color: rgb(0,76,213); text-decoration: none" >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 id="jltb3v9" class="Apple-converted-space"> </span><a style="color: rgb(0,76,213); text-decoration: none" >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 id="9f3j99d" class="Apple-converted-space"> </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 id="r3tn3vn" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span id="3zf999r" class="Apple-converted-space"> </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 id="9v9zvlf" class="Apple-converted-space"> </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 id="13zlxnt" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code><span id="f19vn9r" class="Apple-converted-space"> </span>and<span id="991d9l9" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span id="v9tx9r9" class="Apple-converted-space"> </span>classes extending the<span id="l1xzd9l" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span id="v3dxjjh" class="Apple-converted-space"> </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 id="h99fzrx" class="Apple-converted-space"> </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 id="txzxp9d" class="Apple-converted-space"> </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 id="f3v93pb" class="Apple-converted-space"> </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 id="x3vtvn9" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span id="r9nz99b" class="Apple-converted-space"> </span>that it can turn on or off. So, that means, an<span id="99f9bdx" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">ISwitch</code><span id="pd9jt3v" class="Apple-converted-space"> </span>should have an<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span id="9bhr9bb" class="Apple-converted-space"> </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 id="pfjvhz9" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span id="9ztdn99" class="Apple-converted-space"> </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 id="9pj33rr" class="Apple-converted-space"> </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 id="vt3fz33" class="Apple-converted-space"> </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 id="dhv9ll9" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span id="939nx1l" class="Apple-converted-space"> </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 id="hb9fz9d" class="Apple-converted-space"> </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 id="3ztd9rr" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span id="htz9fff" class="Apple-converted-space"> </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 id="3pbdv39" class="Apple-converted-space"> </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 id="lvh399h" class="Apple-converted-space"> </span><em>On</em><span id="9tfx3nb" class="Apple-converted-space"> </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 id="b3hr9p9" class="Apple-converted-space"> </span><em>Off</em><span id="lhrbvfh" class="Apple-converted-space"> </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 id="9jtf99l" class="Apple-converted-space"> </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 id="9zd3h99" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">On()</code><span id="b9bnxh3" class="Apple-converted-space"> </span>method of the actual switches should internally call the<span id="jbnfrj9" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">TurnOn()</code><span id="fnh93vd" class="Apple-converted-space"> </span>method of the electrical equipment, and the<span id="9lv9jjb" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Off()</code><span id="3r3l9rx" class="Apple-converted-space"> </span>should call the<span id="dnxjt39" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">TurnOff()</code><span id="xpblvn9" class="Apple-converted-space"> </span>method on the equipment. So, the<span id="dl9zb3x" class="Apple-converted-space"> </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 id="hxb3f9f" class="Apple-converted-space"> </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 id="rjtfhhr" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code><span id="hjdfzrb" class="Apple-converted-space"> </span>to<span id="z3b939z" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span id="pbdn3tb" class="Apple-converted-space"> </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 id="9rlx9lj" class="Apple-converted-space"> </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 id="9hrtnb3" class="Apple-converted-space"> </span><strong>Abstraction</strong><span id="3f99r1f" class="Apple-converted-space"> </span>is the base<span id="3bdprfx" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Switch</code><span id="1bvnrx9" class="Apple-converted-space"> </span>class.<span id="vvpjtbj" class="Apple-converted-space"> </span><strong>RefinedAbstraction</strong><span id="xjt9hbp" class="Apple-converted-space"> </span>is the specific switch classes (<code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">FancySwitch</code>,<span id="lprr9rp" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">NormalSwitch</code><span id="3hrbnbh" class="Apple-converted-space"> </span>etc.).<span id="99pjtzx" class="Apple-converted-space"> </span><strong>Implementor</strong><span id="rr9xh3h" class="Apple-converted-space"> </span>is the<span id="3lnfzpv" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">IElectricalEquipment</code><span id="1pt9td9" class="Apple-converted-space"> </span>interface.<strong>ConcreteImplementorA</strong><span id="nxrjtbp" class="Apple-converted-space"> </span>and<span id="fpbdfdj" class="Apple-converted-space"> </span><strong>ConcreteImplementorB</strong><span id="3t39999" class="Apple-converted-space"> </span>are the<span id="9b93399" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Fan</code><span id="nhjd3tr" class="Apple-converted-space"> </span>and<span id="13zt9hd" class="Apple-converted-space"> </span><code style="font: 11pt 'Courier New', Courier, mono; color: rgb(153,0,0)">Light</code><span id="ptl9zhn" class="Apple-converted-space"> </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 >http://www.codeproject.com/KB/architecture/LearningDesignPatterns1.aspx</a></p></span></span><img src ="http://www.shnenglu.com/beautykingdom/aggbug/148264.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/beautykingdom/" target="_blank">chatler</a> 2011-06-08 14:14 <a href="http://www.shnenglu.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.shnenglu.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.shnenglu.com/beautykingdom/archive/2011/06/08/148263.html</guid><wfw:comment>http://www.shnenglu.com/beautykingdom/comments/148263.html</wfw:comment><comments>http://www.shnenglu.com/beautykingdom/archive/2011/06/08/148263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/beautykingdom/comments/commentRss/148263.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/beautykingdom/services/trackbacks/148263.html</trackback:ping><description><![CDATA[     摘要: 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...  <a href='http://www.shnenglu.com/beautykingdom/archive/2011/06/08/148263.html'>阅读全文</a><img src ="http://www.shnenglu.com/beautykingdom/aggbug/148263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/beautykingdom/" target="_blank">chatler</a> 2011-06-08 13:45 <a href="http://www.shnenglu.com/beautykingdom/archive/2011/06/08/148263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>qMM与设计模式的有趣见解http://www.shnenglu.com/beautykingdom/archive/2009/12/05/102587.htmlchatlerchatlerSat, 05 Dec 2009 02:43:00 GMThttp://www.shnenglu.com/beautykingdom/archive/2009/12/05/102587.htmlhttp://www.shnenglu.com/beautykingdom/comments/102587.htmlhttp://www.shnenglu.com/beautykingdom/archive/2009/12/05/102587.html#Feedback0http://www.shnenglu.com/beautykingdom/comments/commentRss/102587.htmlhttp://www.shnenglu.com/beautykingdom/services/trackbacks/102587.html创徏型模?

1
?span>FACTORY ?/span>q?span>MM不了请吃饭了,麦当劳的鸡翅和肯德基的鸡都?span>MM爱吃的东西,虽然口味有所不同Q但不管你带MM去麦当劳或肯德基Q只向服务员说来四个鸡?span>”p了。麦当劳和肯德基是生鸡翅?span>Factory

工厂模式Q客L和工厂类分开。消费者Q何时候需要某U品,只需向工厂请求即可。消费者无M改就可以接纳C品。缺Ҏ当品修ҎQ工厂类也要做相应的修改。如Q如何创建及如何向客L提供?

2
?span>BUILDER —MM最爱听的就?span>“我爱?span>”q句话了Q见C同地方的MM,要能够用她们的方a跟她说这句话哦,我有一个多U语a译机,上面每种语言都有一个按键,见到MM我只要按对应的键Q它p够用相应的语a说出我爱?span>”q句话了Q国外的MM也可以轻松搞掂,q就是我?span>“我爱?span>”builder。(q一定比军在伊拉克用的译机好卖)

建造模?/span>Q将产品的内部表象和产品的生成过E分割开来,从而一个徏造过E生成具有不同的内部表象的品对象。徏造模式得品内部表象可以独立的变化Q客户不必知道品内部组成的l节。徏造模式可以强制实行一U分步骤q行的徏造过E?

3
?span>FACTORY METHOD ?/span>?span>MM去麦当劳吃汉堡,不同?span>MM有不同的口味Q要每个都记住是一件烦人的事情Q我一般采?span>Factory Method模式Q带着MM到服务员那儿Q说要一个汉?span>”Q具体要什么样的汉堡呢Q让MM直接跟服务员说就行了?

工厂Ҏ模式Q核心工厂类不再负责所有品的创徏Q而是具体创建的工作交给子类dQ成Z个抽象工厂角Ԍ仅负责给出具体工厂类必须实现的接口,而不接触哪一个品类应当被实例化q种l节?

4
?span>PROTOTYPE ?/span>?span>MM?span>QQ聊天Q一定要说些深情的话语了Q我搜集了好多肉ȝ情话Q需要时只要copy出来攑ֈQQ里面p了,q就是我的情?span>prototype了。(100块钱一份,你要不要Q?

原始模型模式Q通过l出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的Ҏ创徏出更多同cd的对象。原始模型模式允许动态的增加或减品类Q品类不需要非得有M事先定的等U结构,原始模型模式适用于Q何的{l构。缺Ҏ每一个类都必配备一个克隆方法?

5
?span>SINGLETON ?/span>俺有6个漂亮的老婆Q她们的老公都是我,我就是我们家里的老公SigletonQ她们只要说?span>“老公Q都是指的同一个hQ那是?span>(刚才做了个梦啦,哪有q么好的?span>)

单例模式Q单例模式确保某一个类只有一个实例,而且自行实例化ƈ向整个系l提供这个实例单例模式。单例模式只应在有真正的单一实例的需求时才可使用?

l构型模?

6
?span>ADAPTER ?/span>在朋友聚会上到了一个美?span>SarahQ从香港来的Q可我不会说_语Q她不会说普通话Q只好求助于我的朋友kent了,他作为我?span>Sarah之间?span>AdapterQ让我和Sarah可以怺交谈?span>(也不知道他会不会耍我)

适配器模?/span>Q把一个类的接口变换成客户端所期待的另一U接口,从而原本因接口原因不匚w而无法一起工作的两个c能够一起工作。适配cd以根据参数返q一个合适的实例l客L?

7
?span>BRIDGE ?/span>早上到MMQ要说早上好Q晚上碰?span>MMQ要说晚上好Q碰?span>MMI了件新衣服Q要说你的衣服好漂亮哦,到MM新做的发型,要说你的头发好漂亮哦。不要问?span>“早上到MM新做了个发型怎么?span>”q种问题Q自qBRIDGEl合一下不p?

桥梁模式Q将抽象化与实现化脱耦,使得二者可以独立的变化Q也是说将他们之间的强兌变成弱关联,也就是指在一个Y件系l的抽象化和实现化之间用组?span>/聚合关系而不是承关p,从而两者可以独立的变化?

8
?span>COMPOSITE —Mary今天q生日?span>“我过生日Q你要送我一件礼物?span>”“嗯,好吧Q去商店Q你自己挑?span>”“qgT恤挺漂亮Q买Q这条裙子好看,乎ͼq个包也不错Q买?span>”“喂,C三g了呀Q我只答应送一件礼物的哦?span>”“什么呀Q?span>T恤加裙子加包包,正好配成一套呀Q小姐,ȝ你包h?span>”“……”Q?span>MM都会?span>Composite模式了,你会了没有?

合成模式Q合成模式将对象l织到树l构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树l构表示出来。合成模式得客L把一个个单独的成分对象和׃们复合而成的合成对象同{看待?

9
?span>DECORATOR —Maryq完轮到Sarlyq生日,q是不要叫她自己挑了Q不然这个月伙食费肯定玩完,拿出我去q在华山上照的照片Q在背面写上最好的的礼物,是׃?span>Fita”Q再到街上礼品店C个像框(卖礼品的MM也很漂亮哦)Q再N壁搞术设计?span>Mike设计了一个漂亮的盒子装v?span>……Q我们都?span>DecoratorQ最l都在修饰我q个人呀Q怎么P看懂了吗Q?

装饰模式Q装饰模式以对客L透明的方式扩展对象的功能Q是l承关系的一个替代方案,提供比承更多的灉|性。动态给一个对象增加功能,q些功能可以再动态的撤消。增加由一些基本功能的排列l合而生的非常大量的功能?

10
?span>FAÇADE ?/span>我有一个专业的Nikon相机Q我喜Ƣ自己手动调光圈、快门,q样照出来的照片才专业,?span>MM可不懂这些,教了半天也不会。幸好相机有Facade设计模式Q把相机调整到自动档Q只要对准目标按快门p了,一切由相机自动调整Q这?span>MM也可以用q个相机l我拍张照片了?

门面模式Q外部与一个子pȝ的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接口,使得子系l更易于使用。每一个子pȝ只有一个门面类Q而且此门面类只有一个实例,也就是说它是一个单例模式。但整个pȝ可以有多个门面类?

11
?span>FLYWEIGHT ?/span>每天?span>MM发短信,手指都篏MQ最q买了个新手机,可以把一些常用的句子存在手机里,要用的时候,直接拿出来,在前面加?span>MM的名字就可以发送了Q再不用一个字一个字敲了。共享的句子是FlyweightQ?span>MM的名字就是提取出来的外部特征Q根据上下文情况使用?

享元模式Q?span>FLYWEIGHT在拳L赛中指最轻量U。n元模式以׃n的方式高效的支持大量的细_度对象。n元模式能做到׃n的关键是区分内蕴状态和外蕴状态。内蕴状态存储在享元内部Q不会随环境的改变而有所不同。外蕴状态是随环境的改变而改变的。外蕴状态不能媄响内蕴状态,它们是相互独立的。将可以׃n的状态和不可以共享的状态从常规cM区分开来,不可以׃n的状态从c里剔除出去。客L不可以直接创׃n的对象,而应当用一个工厂对象负责创׃n的对象。n元模式大q度的降低内存中对象的数量?

12
?span>PROXY ?/span>?span>MM在网上聊天,一开头L“hi,你好”,“你从哪儿来呀Q?span>”“你多大了Q?span>”“w高多少呀Q?span>”q些话,真烦人,写个E序做ؓ我的Proxy吧,凡是接收到这些话都设|好了自动的回答Q接收到其他的话时再通知我回{,怎么P酷吧?

代理模式Q代理模式给某一个对象提供一个代理对象,q由代理对象控制Ҏ对象的引用。代理就是一个h或一个机构代表另一个h或者一个机构采取行动。某些情况下Q客户不x者不能够直接引用一个对象,代理对象可以在客户和目标对象直接起到中介的作用。客L分L不出代理主题对象与真实主题对象。代理模式可以ƈ不知道真正的被代理对象,而仅仅持有一个被代理对象的接口,q时候代理对象不能够创徏被代理对象,被代理对象必Lpȝ的其他角色代为创建ƈ传入?

行ؓ模式

13
?span>CHAIN OF RESPONSIBLEITY ?/span>晚上Mp课,Z好开溜坐C最后一排,哇,前面坐了好几个漂亮的MM哎,扑ּU条Q写?span>“Hi,可以做我的女朋友吗?如果不愿意请向前?span>”Q纸条就一个接一个的传上MQ糟p,传到W一排的MM把纸条传l老师了,听说是个老处奛_Q快?span>!

责Q链模?/span>Q在责Q链模式中Q很多对象由每一个对象对其下家的引用而接h形成一条链。请求在q个链上传递,直到链上的某一个对象决定处理此h。客户ƈ不知道链上的哪一个对象最l处理这个请求,pȝ可以在不影响客户端的情况下动态的重新l织铑֒分配责Q。处理者有两个选择Q承担责L者把责Q推给下家。一个请求可以最l不被Q何接收端对象所接受?

14
?span>COMMAND ?/span>俺有一?span>MM安得特别严,没法见面Q只好借助于她弟弟在我们俩之间传送信息,她对我有什么指C,写一张纸条让她弟弟带l我。这不,她弟弟又传送过来一?span>COMMANDQؓ了感谢他Q我请他吃了杂酱面Q哪知道他说Q?span>“我同时给我姐姐三个男朋友?span>COMMANDQ就C最气Q才h吃面?span>”Q?/span>

命o模式Q命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命o的责d执行命o的责d割开Q委z不同的对象。命令模式允许请求的一方和发送的一方独立开来,使得h的一方不必知道接收请求的一方的接口Q更不必知道h是怎么被接Ӟ以及操作是否执行Q何时被执行以及是怎么被执行的。系l支持命令的撤消?

15
?span>INTERPRETER ?/span>俺有一个《MM真经》,上面有各UMM的攻略,比如说去吃西的步骤、去看电qҎ{等Q跟MMU会Ӟ只要做一?span>InterpreterQ照着上面的脚本执行就可以了?

解释器模?/span>Q给定一个语a后,解释器模式可以定义出其文法的一U表C,q同时提供一个解释器。客L可以使用q个解释器来解释q个语言中的句子。解释器模式描q怎样在有了一个简单的文法后,使用模式设计解释q些语句。在解释器模式里面提到的语言是指M解释器对象能够解释的Ml合。在解释器模式中需要定义一个代表文法的命ocȝ{l构Q也是一pd的组合规则。每一个命令对象都有一个解释方法,代表对命令对象的解释。命令对象的{l构中的对象的Q何排列组合都是一个语a?

16
?span>ITERATOR ?/span>我爱上了MaryQ不一切的向她求婚?
   Mary
Q?span>“惌我跟你结婚,得答应我的条?span>”

  
我:什么条件我都答应,你说?span>”
   Mary
Q?span>“我看上了那个一克拉的钻?span>”
  
我:我买Q我乎ͼq有吗?
   Mary
Q?span>“我看上了湖边的那栋别?span>”
  
我:我买Q我乎ͼq有吗?
   Mary
Q?span>“你的弟弟必要?chmetcnv unitname="cm" sourcevalue="50" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">50cm?span>”
  
我脑袋嗡的一壎ͼ坐在椅子上,一咬牙Q?span>“我剪Q我剪,q有吗?
   ……

q代子模?/span>QP代子模式可以序讉K一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起Ş成的MUC集,聚集对象是能够包容一l对象的容器对象。P代子模式P代逻辑装C个独立的子对象中Q从而与聚集本n隔开。P代子模式化了聚集的界面。每一个聚集对象都可以有一个或一个以上的q代子对象,每一个P代子的P代状态可以是彼此独立的。P代算法可以独立于聚集角色变化?

17
?span>MEDIATOR ?/span>四个MM打麻,怺之间谁应该给谁多钱不清楚了,q怺当时我在旁边Q按照各自的{码数算钱,赚了q从我q里拿,赔了q也付l我Q一切就OK啦,俺得C四个MM的电话?

调停者模?/span>Q调停者模式包装了一pd对象怺作用的方式,使得q些对象不必怺明显作用。从而他们可以松散偶合。当某些对象之间的作用发生改变时Q不会立卛_响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。调停者模式将多对多的怺作用转化Z对多的相互作用。调停者模式将对象的行为和协作抽象化,把对象在尺度的行ؓ上与其他对象的相互作用分开处理?

18
?span>MEMENTO
?/span>同时跟几?span>MM聊天Ӟ一定要记清楚刚才跟MM说了些什么话Q不?span>MM发现了会不高兴的哦,q怺我有个备忘录Q刚才与哪个MM说了什么话我都拯一份放到备忘录里面保存Q这样可以随时察看以前的记录啦?

备忘录模?/span>Q备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏装的条件下Q将一个对象的状态捉住,q外部化Q存储v来,从而可以在来合适的时候把q个对象q原到存储v来的状态?

19
?span>OBSERVER ?/span>想知道咱们公司最?span>MM情报吗?加入公司?span>MM情报邮gl就行了Q?span>tom负责搜集情报Q他发现的新情报不用一个一个通知我们Q直接发布给邮gl,我们作ؓ订阅者(观察者)可以及时收到情报啦

观察者模?/span>Q观察者模式定义了一U一队多的依赖关p,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化Ӟ会通知所有观察者对象,使他们能够自动更新自己?

20
?span>STATE ?/span>?span>MM交往Ӟ一定要注意她的状态哦Q在不同的状态时她的行ؓ会有不同Q比如你U她今天晚上ȝ电媄Q对你没兴趣?span>MM׃?span>“有事情啦Q对你不讨厌但还没喜Ƣ上?span>MM׃?span>“好啊Q不q可以带上我同事么?Q已l喜Ƣ上你的MM׃?span>“几点钟?看完电媄再去泡吧怎么PQ当然你看电pE中表现良好的话Q也可以?span>MM的状态从不讨厌不喜欢变成喜欢哦?

状态模?/span>Q状态模式允怸个对象在其内部状态改变的时候改变行为。这个对象看上去象是改变了它的类一栗状态模式把所研究的对象的行ؓ包装在不同的状态对象里Q每一个状态对象都属于一个抽象状态类的一个子cR状态模式的意图是让一个对象在其内部状态改变的时候,其行Z随之改变。状态模式需要对每一个系l可能取得的状态创立一个状态类的子cR当pȝ的状态变化时Q系l便改变所选的子类?

21
?span>STRATEGY ?/span>跟不同类型的MMU会Q要用不同的{略Q有的请电媄比较好,有的则去吃小吃效果不错,有的LvҎ漫最合适,单目的都是ؓ了得?span>MM的芳心,我的q?span>MM锦囊中有好多Strategy哦?

{略模式Q策略模式针对一l算法,每一个算法封装到h共同接口的独立的cMQ从而得它们可以相互替换。策略模式得算法可以在不媄响到客户端的情况下发生变化。策略模式把行ؓ和环境分开。环境类负责l持和查询行为类Q各U算法在具体的策略类中提供。由于算法和环境独立开来,法的增减,修改都不会媄响到环境和客L?

22
?span>TEMPLATE METHOD —?/span>看过《如何说服女生上床》这部经典文章吗Q女生从认识C床的不变的步骤分为y遇、打破僵局、展开q求、接吅R前戏、动手、爱抚、进d大步?span>(Template method)Q但每个步骤针对不同的情况,都有不一L做法Q这p看你随机应变?span>(具体实现)Q?

模板Ҏ模式Q模板方法模式准备一个抽象类Q将部分逻辑以具体方法以及具体构造子的Ş式实玎ͼ然后声明一些抽象方法来q子类实现剩余的逻辑。不同的子类可以以不同的方式实现q些抽象ҎQ从而对剩余的逻辑有不同的实现。先制定一个顶U逻辑框架Q而将逻辑的细节留l具体的子类d现?

23
?span>VISITOR ?/span>情h节到了,要给每个MM送一束鲜花和一张卡片,可是每个MM送的花都要针对她个h的特点,每张卡片也要Ҏ个h的特Ҏ挑,我一个h哪搞得清楚,q是找花店老板和礼品店老板做一?span>VisitorQ让花店老板ҎMM的特炚w一束花Q让C品店老板也根据每个h特点选一张卡Q这样就L多了Q?

讉K者模?/span>Q访问者模式的目的是封装一些施加于某种数据l构元素之上的操作。一旦这些操作需要修改的话,接受q个操作的数据结构可以保持不变。访问者模式适用于数据结构相Ҏ定的pȝQ它把数据结构和作用于结构上的操作之间的耦合解脱开Q得操作集合可以相对自q演化。访问者模式得增加新的操作变的很ҎQ就是增加一个新的访问者类。访问者模式将有关的行为集中到一个访问者对象中Q而不是分散到一个个的节点类中。当使用讉K者模式时Q要尽可能多的对象览逻辑攑֜讉K者类中,而不是放到它的子cM。访问者模式可以跨q几个类的等U结构访问属于不同的{l构的成员类?/span>

chatler 2009-12-05 10:43 发表评论
]]>
޹þþۺվ| þþþ޾Ʒվ| 2021˾Ʒþ| 91þ㽶Ů߿| þþƷ99þ㽶| ҹþþþþ| þþƷƷ޾Ʒ| þù| þþþþþþþþþĻ| þþƷһ| þ99Ʒþþôѧ| ݺݾƷþþĻ| ŷ޹Ʒþø | žȾþƵ| þþƷа| ɫ͵͵88ŷƷþþ| 99þۺϺݺۺϾþֹ| 2021þþƷѹۿ| þþƷѲ| ޹Ʒþ| 99þĻ| þþŷղ| þþþþþþ˳| þþƷAV| þۺۺϾþúݺݺ97ɫ88 | ӰԺ˾þ| þþƷ| һþaþþƷvrۺ| ŷɫۺϾþþþþ| þþþAV| þþþƷ| 99þùѸ| ձɫۺϾþӰԺ| 칫ҾþþƷ| LƷþ| Ʒgzþþ| þseֻоƷ| þþƷ18| þ¶ݺɫ| þùֱ| Ʒþþþ㽶|