<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-6827867</id><updated>2009-07-29T21:09:44.144-07:00</updated><title type='text'>Design Patterns, C#, SQL Server...</title><subtitle type='html'>A place to put my thoughts on designing with patterns, technical stuff related to C#, .Net and SQL Server.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6827867.post-113035184183196182</id><published>2005-10-26T10:59:00.000-07:00</published><updated>2005-10-26T12:20:15.406-07:00</updated><title type='text'>Sudoku Game Design</title><content type='html'>&lt;span style="font-size:85%;"&gt;In &lt;a href="http://avnrao.blogspot.com/2005/10/designing-sudoku-game.html"&gt;previous&lt;/a&gt; post, I introduced Sudoku game and wanted to design to see what patterns will fit into it. Here are few use cases using which we will design the game.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;1. Display Sudoku Board to the User : user selects board size (3x3 or 9x9 or anyother), and difficulty level (easy, medium or difficult).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;2. View result of the game : Once user plays the game, he would like to get game result. If he succeeded in completing it or not.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;keeping these use cases in mind, here is a UML diagram showing the design. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/SudokuGame2.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/SudokuGame2.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;(click on image and open it in a new window to see it bigger).&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;1. Lets start with the design. To develop a game like Sudoku, we need to identify the classes that we have to put in the design and assign responsibilities to them. The most prominent entities in the game are Game Board, Cell or Box, number.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;2. So we draw 2 classes as Digit, Box. As shown in the diagram, a Digit instance will have a numeric value ( 0 to 9 ) and a get/set methods to access this numeric property. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;3. Box class represents a cell in the board. Each box will either have a Digit filled in or it will be empty. If a Digit is already filled, the cell will not be editable right. An empty cell hides the actual digit in it allowing user to type in a value. Each cell should be smart enough to validate itself based on what user enters. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;4. Every cell will validate itself it the user entered a correct value in it. Similar validation applies to smaller matrix (3x3) and also to the complete game board. So we need a way to abstract client from a cell or a small matrix or a complete game board. Client, based on it needs, can ask a cell or small matrix or a game board to validate it self and give the result. This sounds like &lt;strong&gt;Composite pattern&lt;/strong&gt;. A composite pattern lets client treet a leaf and complex tree uniformly. Client should not be aware wether it is talking to a cell, matrix, or a board.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;5. Thinking about composite pattern, lets create an AbstractBox class that will just have IsEditable, IsValid abstract methods. Now, lets create AtomicBox (you could name it as Cell) that derives from AbstractBox. Since AtomicBox should be able to have information like the actual number, user entered number..lets create two properties to hold these values. Only user entered digit property will have get/set methods. If you observe, these two properties are instances of Digit class. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;6. As in composite pattern, Lets create one more class CompositeBox which will be a collection of AbstractBoxes. An obvious field for this class will Boxes. Lets create 1 more classe as SquareBox to represent a square matrix deriving from CompositeBox class (as shown in the diagram). Though there is no current need to create a CompositeBox class, instead we directly create SquareBox deriving from AbstractBox, lets have the CompositeBox for future.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;7. So we successfully used a Composite pattern in our design. Whats the advantage we get out of it? One, client need not know about AtomicBox, SquareBox. All it needs a reference to AbstractBox class and its interfaces. Another advantage is, SquareBox need not know about its internal structure. As our use cases say, a Sudoko board can be of any size (3x3 or 9x9 etc). So a SquareBox will be collection of SquareBoxes of smaller sizes and each of smaller SquareBox can also be a collection of SquareBoxes or finally leaf nodes which are AtomicBoxes. This kind of abstraction is possible with Composite pattern. The best advantage is when we have to validate a SquareBox, it just needs to ask if the child boxes are valid. And each child box will ask their children and so on... if any of these children return false, that means the whole board is wrong and game is incomplete. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;8. Did you observe that we created a Digit class that just holds a numeric value? Isnt that too much information that a class can have? ( :-)..i am just kidding). The reason why we created a Digit class is to have centralized control over the numbers, make comparision easy and also to abstract working with numbers. This reduces code that that deals with integers all over the project, had it been implemented with numbers. You would say that having a Digit class will kill the application performance because of number of instances of Digit classes is going to be more based on size of the board, and we are also maintaining actual digit vs user entered digit??? Yes. this is going to be problem with performance. We have to reduce the number of instances of Digit classes somehow. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;9. Think of Flyweight pattern. Yes. Flyweight pattern lets reduce number instances by making instances sharable. Ok. Lets create a class to handle this sharing business. A class named DigitsFlyweight will serve our purpose. It will maintain a list of Digits ( 0 to 9 ) and also an interface to return a Digit instance for given integer. DigitFor method will check if an instance for ths integer already exists, if so, its going to return same instance or else it would create an instance and return that.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;10. Till now, we have Digit, AtomixBox, SquareBox in place. But we need someone to construct Sudoku Board. As you would have observed, the sudoku board will be an instance of SquareBox. Based on the size of the board user selects, we need to construct that kind of SquareBox and its composite structure. We need some one build the composite structure. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;11. Think of Builder pattern. A builder pattern separates construction of complex product from its representation. Most of the times, a builder will build composite structures, meaning builder pattern and composite pattern will be used together. Lets create a class named SudokuBoardBuilder that builds an instance of SquareBox based its size. Builder needs to know how many instances of AtomicBoxes, how many instances 3x3 SquareBoxes and how many instances of 9x9 SquareBoxes it needs to create for a 9x9 board. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;12. Builder creates all AtomicBoxes on the board right. But each atomic box needs a Digit to make the game. Thats the interesting part of the actually creating a game. You need to be a game creator to create a sudoku kind of game. Your approach would be like, place all numbers in a big matrix and start hiding few of the numbers to make game difficult to solve. This is kind of algorithm or logic to create a sudoku game. So when i say algorithm or logic, there will be many implementations from various people. Our design should be extendable to easily change algorithms without needing changes to existing classes. We should dynamically change algorithms at runtime (not at compile time). &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;13. Think of Strategy pattern. It lets vary algorithms dynamically at runtime. So, lets separate logic of game creation from Builder class and delegate it to a strategy. Lets create an interface named ISudokuProvider with few methods to create a new game, get digit at certain cell, and also to find out if that value needs to be hidden. The concrete implementations of ISudokuProvider interface will talk to any algorithm that creates a sudoku game. Our builder class will have an instance of ISudokuProvider and will use it to build sudoku board. Builder class does not need to have direct reference to a concrete implementation. To make it work, we will have the concrete implementation's type name configured in any config file and load it from there. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;This makes our design complete. As per use cases, our design allows varying sizes of sudoku boards. But we missed to include difficulty level. That should be simple. We should pass a difficulty level to strategy when we call NewGame method on it. Now, we can develop a client that uses this domain model to implement sudoku game.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-113035184183196182?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/113035184183196182/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=113035184183196182' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113035184183196182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113035184183196182'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/sudoku-game-design.html' title='Sudoku Game Design'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-113018828730908803</id><published>2005-10-24T13:58:00.000-07:00</published><updated>2005-10-24T14:11:27.320-07:00</updated><title type='text'>Designing Sudoku Game</title><content type='html'>&lt;span style="font-size:85%;"&gt;Do you play &lt;a href="http://websudoku.com"&gt;Sudoku&lt;/a&gt; game? I recently got this on web and addicted to it. The game is fairly simple and its numbers and applying your brain game.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/SudokuGame1.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/SudokuGame1.jpg" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;(from &lt;a href="http://www.WebSudoku.com"&gt;WebSudoku.com&lt;/a&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;The rules are simple. As you see in the picture, this Sudoku is 9 X 9 matrix. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;1. Each row and each column of matrix must be filled with 1 to 9 digits.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;2. Each small matrix ( 3 X 3) must also have 1 to 9 digits.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;3. You cannot change already filled in digits.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;So, in the picture, you start filling the missing digits and also make sure you dont break any rules mentioned above. Its interesting game and becomes difficult as you fill in the slots. Play it when you have free time. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;My point to put this game on my post is to design this game. I will post my design and point out why i have used certain design patterns by explaining them. I would like to know how you would design such a game?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Post your comments..&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-113018828730908803?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/113018828730908803/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=113018828730908803' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113018828730908803'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113018828730908803'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/designing-sudoku-game.html' title='Designing Sudoku Game'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-113017974449062896</id><published>2005-10-24T11:43:00.000-07:00</published><updated>2005-10-24T11:49:04.496-07:00</updated><title type='text'>Pattern Resources</title><content type='html'>&lt;span style="font-size:85%;"&gt;In an effort to collate resources i find useful to learn patterns and to do better design improving day by day, i will start posting the resources in my blog and update it regularly.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://c2.com/cgi-bin/wiki"&gt;Pattern Wiki&lt;/a&gt; - a place where you find definitions for GoF patterns and examples. Go through the discussions for each pattern..its an interesting read.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-113017974449062896?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/113017974449062896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=113017974449062896' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113017974449062896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/113017974449062896'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/pattern-resources.html' title='Pattern Resources'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112990776806310216</id><published>2005-10-24T08:05:00.000-07:00</published><updated>2005-10-24T08:30:05.693-07:00</updated><title type='text'>Car Dealer sample - Strategy Pattern</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-design-wo-strategy-pattern.html"&gt;Prev Post&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In the last post, we designed move behaviour of car for our famous DOOP car dealer web site. We observed couple of problems with that particular design. To explain the issue in brief, we had added MoveWithV4, MoveWithV6 kind of methods in base class Car, thinking that we do not need to duplicate code in all deriving classes. But the problem we ended up with is making certain cars move with powerful engines though the car does not actually support the engine. Finally, we thought to externalize or separate the varying behaviour out of Car class. The varying behaviour was Moving car with engine. In this post, lets see how Strategy pattern comes to rescue us from the problems explained above.&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Strategy Pattern :&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Intent from GoF : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;This means, when you a more than way to implement certain feature or to solve certain problem (call it an algorithm), the client need not be tied to a particular implementation. Client should get flexibility to shift to any other algorithm without being modified. Strategy pattern applies to a design problem where client need to dynamically interchange the algorithms (feature or responsibility implementation) and vary them independently, without being tied to certain way of implementation. UML below describes the strategy pattern.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPStrategy1.jpg"&gt;&lt;span style="font-size:85%;"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPStrategy1.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;As you observe, Strategy class defines an interface to access the algorithm. Client is only exposed to Strategy class, and not to the ConcreteStrategy classes which are actual implementations of algorithm. In above notation, client can have reference to more than one concrete implementation, based on the requirement for design. Now you know what is strategy pattern is and what its UML looks like. Lets apply this pattern to our car dealer example's move car behaviour. Take a look at the following UML diagram. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPStrategy3.jpg"&gt;&lt;span style="font-size:85%;"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPStrategy3.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;The design shows that we have separated Engine out of Car(no no..we didnt take engine out of the car..we just delegated the move behaviour to engine). Car class has reference to Engine instance and &lt;em&gt;&lt;span style="color:#ff9900;"&gt;Move&lt;/span&gt;&lt;/em&gt; method in Car class just calls &lt;em&gt;&lt;span style="color:#ff9900;"&gt;this.i_Engine.Move&lt;/span&gt;&lt;/em&gt;. If you observe, we see 2 deriving heirarchies, one for Car and another one for Engine. Car has very deriving subclasses as Corrola, Accord, whereas Engine has V4 and V6. A car instance will be created with a particular Engine. That way, respective engine instance can be passed that particular car instance, such as V4 to Corrola and V6 to Accord. So when you call Car.Move, it calls respective engine's move method. Move behaviour is well encapsulated into Engine class, and its not duplicated. The strategy interface here is Engine class, and V4 and V6 are concrete implementation of strategy class. Dynamically different car types can load different strategy based on their moving behaviour.&lt;br /&gt;&lt;br /&gt;Strategy pattern is most widely used pattern. You will find this pattern in various designs and it serves the purpose the best. Other examples of Strategy pattern are listed below.&lt;br /&gt;&lt;br /&gt;1. ArrayList.Sort( IComparer ), ArrayList.BinarySearch( IComparer ),&lt;br /&gt;String.Format( IFormatProvider.. )&lt;br /&gt;&lt;br /&gt;In all of the above methods, the behaviour of Comparing, formatting is externalized to another class. This provides flexibility to change implementation at runtime dynamically.&lt;br /&gt;&lt;br /&gt;2. System.Collections.Generics make heavy use of Strategy.&lt;br /&gt;&lt;br /&gt;3. System.Security.Cryptography.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPStrategy4.jpg"&gt;&lt;span style="font-size:85%;"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPStrategy4.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;We have seen how to use decorator and strategy patterns in various scenarios. Next we will go over Composite pattern and apply it to car dealer sample. until then..happy patterning..&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Next post&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112990776806310216?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112990776806310216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112990776806310216' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112990776806310216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112990776806310216'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/car-dealer-sample-strategy-pattern.html' title='Car Dealer sample - Strategy Pattern'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112990777745181343</id><published>2005-10-21T08:16:00.000-07:00</published><updated>2005-10-24T08:32:27.740-07:00</updated><title type='text'>Car Dealer Design - w/o Strategy Pattern</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-sample-decorator-pattern.html"&gt;Prev post&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Continuing series of&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/pattern-examples-car-dealer-sample.html"&gt; &lt;/a&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/pattern-examples-car-dealer-sample.html"&gt;designing Car dealer&lt;/a&gt; example with patterns, my approach is to design without patterns, observe the problems, apply patterns and feel the advantages of patterns. As we have applied this approach to &lt;/span&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/pattern-examples-car-dealer-sample.html"&gt;&lt;span style="font-size:85%;"&gt;design w/o decorator&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;, &lt;/span&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-sample-decorator-pattern.html"&gt;&lt;span style="font-size:85%;"&gt;with decorator&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;, now we are going to design another feature in car dealer example.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;One of the requirements or use cases in car dealer web site design is to show interactive demo and virtual driving experience to user. This involves giving the user some kind of control to make car start, move, accellerate to show how fast it can go (may be something like..this car reaches 0 to 60mph in 5 seconds) and all. So, to support such kind of requirement, we start adding certain methods and properties to various classes in our design. The UML below shows a raw design to support move car feature.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator7.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator7.jpg" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;Car speed varies based on its engine that manufacturor uses to build the car. In the design Corrolla, Accord have varying initial moving speeds because former uses V4 and later uses V6. Lets add &lt;em&gt;&lt;span style="color:#ff9900;"&gt;MoveWithV4&lt;/span&gt;&lt;/em&gt; method inside Car class so that any deriving car that uses V4 engine need not have the code duplicated. But the issue is there will be few cars which use V6 engine, and not to duplicate code in all cars that use V6 engine, we can add one more method &lt;em&gt;&lt;span style="color:#ff9900;"&gt;MoveWithV6&lt;/span&gt;&lt;/em&gt; in Car class. Though we achieve the goal of not duplicating code all across, but there is an inherent problem with this design. Some one can call &lt;em&gt;&lt;span style="color:#ff9900;"&gt;CorollaSedan.MoveWithV6&lt;/span&gt;&lt;/em&gt; and get the corrolla move faster. That means, any deriving class can also use the methods which it is not supposed to. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;So to get around this problem, we could override &lt;em&gt;&lt;span style="color:#ff9900;"&gt;MoveWithV6&lt;/span&gt;&lt;/em&gt; in &lt;em&gt;&lt;span style="color:#ff9900;"&gt;CorollaSedan&lt;/span&gt;&lt;/em&gt; class and throw &lt;em&gt;&lt;span style="color:#ff9900;"&gt;MethodNotImplemented&lt;/span&gt;&lt;/em&gt; exception from it. This needs to be done in all deriving car classes which do not support certain methods that exist in their base classes...maintaince overhead. What if a new car comes into the market that uses a very smart engine that makes car fly. So you would either override the respective sub class or put the method in Car class to not to replicate code. we end up in same problem again. To solve this problem, we need to externalize the varying behaviour. The varying behaviour in this example is moving with engine. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;In the next post, you will see how Strategy pattern comes to rescue from this kind of problem. stay tuned.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-sample-strategy-pattern.html"&gt;&lt;span style="font-size:85%;"&gt;Next post&lt;/span&gt; &lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112990777745181343?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112990777745181343/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112990777745181343' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112990777745181343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112990777745181343'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/car-dealer-design-wo-strategy-pattern.html' title='Car Dealer Design - w/o Strategy Pattern'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112983471971921142</id><published>2005-10-20T11:35:00.000-07:00</published><updated>2005-10-21T09:03:49.336-07:00</updated><title type='text'>Car Dealer sample - Decorator Pattern</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/pattern-examples-car-dealer-sample.html"&gt;Prev post&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Decorator Pattern :&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Intent from GoF : Attach additional responsibilities to an object dynamically. Decorators provide flexible alternative to subclassing for extending functionality.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Decorator pattern allows design to be flexible when there is a need to add responsibilities to an object at runtime, based on configuration or by someother means. The responsibilities could be anything such as, in the car dealer example, the car features will be added dynamically based on customer selection. The UML for decorator pattern is shown below. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator4.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator4.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;The intent is to add features or responsibilities to the component. Each feature or responsibility will be created as a decorator class as shown. The decorator wraps the component class (check the constructor of decorator in UML) and adds the responsibility onto that component. This composition of component class into decorator makes Decorator pattern flexible enough to pass any component (even Decorators because, decorators are also components as they derive from it) and add features over feature over feature...&lt;br /&gt;&lt;br /&gt;In car dealer sample, we faced the problem of numerous classes created when we designed car types and each car type supporting various car features. Check my &lt;a href="http://avnrao.blogspot.com/2005_10_01_avnrao_archive.html#112973325346420129"&gt;previous post&lt;/a&gt; for more details on this. To solve the problem with decorator pattern, we are going to design the system as shown in the following UML diagram.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator5.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator5.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Above diagram shows 2 sides. One side we have all car types deriving from Car class, and another side car features deriving from a decorator class that intern derives from a Car. Observe that the Decorator class constructor takes in Car instance, which can be any Car ( car type instance or decorator instance too ). Now, lets consider how our design supports the functionality we need. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Our goal is to create a coupe that has All power options, Leather seat and moon roof ( and come what may..). Earlier we had used inheritance and defined a class AllPowerLeatherMoonRoofCoupe. But now, to get a car with such features what we need is this..&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#ff9900;"&gt;myCar = new AllPoweredDecorator( new LeatherDecorator( new MoonRoofDecorator( new Coupe() )));&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#003300;"&gt;Remember, the decorator wraps a car instance, that allows me to add one feature at a time to coupe. Similarly, i can add any number of features to any car type dynamically based on what customer selects, and the number of classes in design is small. Observe that car types are completely independent of car features. Adding a new car type or a new car feature does not effect the existing design or code. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#003300;"&gt;So applying decorator pattern solved the problems like tremendously reduced number of classes, flexibility to add features dynamically (you can even add a feature twice to the component..cool haa), eases maintainance. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#003300;"&gt;&lt;strong&gt;Few more Examples&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-size:85%;color:#003300;"&gt;&lt;br /&gt;1. System.IO namespace in .Net&lt;br /&gt;&lt;span style="color:#ff9900;"&gt;this.Stream = new BufferredStream( new MemoryStream( new Stream() )));&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;color:#003300;"&gt;2. In car dealer example, if we have to add color twice to the car&lt;br /&gt;&lt;span style="color:#ff9900;"&gt;this.Car = new ColorCoatedDecorator( new ColorCoatedDecorator( new Sedan() )));&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;color:#003300;"&gt;&lt;span style="color:#003300;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#003300;"&gt;&lt;span style="color:#003300;"&gt;Next, we are going to design Car dealer example without and with Strategy Pattern, and observe the advantages. stay tuned.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;color:#003300;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-design-wo-strategy-pattern.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112983471971921142?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112983471971921142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112983471971921142' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112983471971921142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112983471971921142'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/car-dealer-sample-decorator-pattern.html' title='Car Dealer sample - Decorator Pattern'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112973325346420129</id><published>2005-10-19T07:33:00.000-07:00</published><updated>2005-10-21T09:07:41.163-07:00</updated><title type='text'>Pattern examples - Car Dealer Sample</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;u&gt;Prev post&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;As said in my previous posts, the best way to understand a pattern is to understand the problem without the pattern and then apply pattern to see what issues it solves. In this series of examples to understand how to apply patterns and change the way we think to apply patterns, the first example is DOOP car dealer example. DOOP stands for design patterns and OO (just coined a new acronym to name the example :).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;Scenario :&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;DOOP car dealer sells cars from various manufactorers (such as lexus, toyota, BMW...) to various customers. The dealer wants to set up a website where he can show what cars are in store, display cars, get orders from the customers, order tracking and more. Here are some of use cases that dealer wants to allow customers to do be able to use the web site for.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;1. Display list of all cars, along with manufactorer's information.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;2. Select a car to view complete information about that car.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;3. Display car interactive demo to show customer virtual driving experience with keyboard navigation.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;4. Display default features available to the car, and additional features that are available. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;5. Display quote (prices) to the customer based on what features he selects for the car.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;For now, lets assume the dealer wants to have these use cases implemented in initial iterations (know about iterative development?? lets talk about that later). In this series, we are not going to develop the complete website, but we are only going to design the domain layer of the application by applying design patterns. These domain layer classes then will be used to develop the web site. Considering above requirements, we are ready to start our design. Lets first put design patterns aside and design. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Note :&lt;/span&gt; we are not going to design a full fledged project. so classes, their properties, methods are going to limited to purpose of explaining the patterns.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;1. Identifying the main classes in our design, the most prominent one is a Car class. We can make this as base class for other derivatives and place some of abstract methods for Car behaviour. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;2. As there are going to be different types of cars, such as Coupe ( a 2 door car), Sedan (4 door ), SUV, etc. Each one is based on the body of the car. We can create few deriving classes from Car class as shown in the picture below.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator12.JPG"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator11.JPG" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;3. There is going to be a separate deriving class for each type of the car. Now, to include support for various additional features for each car such as Leather seats, power windows, moon roof etc.. we start deriving each from the type of the car. To have support for LeatherSeatsCoupeCar, LeatherSeatsSedanCar or AllPoweredCoupeCar or AllPoweredSedanCar, we are going to derive from the respective car type. Check the UML diagram below.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator2.jpg"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator2.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;3. Great. We have derived the feature added car from the respective car type. What if i want a coupe with AllPowered, LeatherSeated? No issues. I can derive a class named AllPwdLeatherCoupe from Coupe. Similarly, i would like to do for sedan, SUV or any other car type. Ok fine that works. What if i want a coupe with combination of features such {FeatureA, FeatureB}, {FeatureA, FeatureC}, {FeatureB, FeatureC}, {FeatureA, FeatureB, FeatureC}??? oh..i can support such kind of requirements too. I am going to derive each combination as a separte class. ok. so lets take a look of the design when only 3 features and 3 car types added. &lt;/span&gt;&lt;/p&gt;&lt;a href="http://photos1.blogger.com/blogger/6148/389/1600/DOOPDecorator31.JPG"&gt;&lt;img style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://photos1.blogger.com/blogger/6148/389/320/DOOPDecorator3.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Fine. Did you see the problem with the design? For every car type and every car feature and combination of features, we are going to add classes and increase number of classes. There will be numerous problems with such a design. Maintaining huge number of classes is going to be a nightmare. Each new car type that comes into the market, we need to add a large number of classes to our design to support such new design. Similarly, a new feature to a the car, we gotto add that feature and its combination to all car types. hhaaaaa...lot of problems. How can we solve these problems? When we end up in such kind of mess during design, think of Decorator pattern. My next post is going to explain what Decorator pattern is, and how it solves the problem we have here.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://avnrao.blogspot.com/2005/10/car-dealer-sample-decorator-pattern.html"&gt;Next post&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112973325346420129?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112973325346420129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112973325346420129' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112973325346420129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112973325346420129'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/pattern-examples-car-dealer-sample.html' title='Pattern examples - Car Dealer Sample'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112958568504663087</id><published>2005-10-17T14:27:00.000-07:00</published><updated>2005-10-18T13:52:57.103-07:00</updated><title type='text'>List of GoF Patterns : Overview</title><content type='html'>&lt;span style="font-size:85%;"&gt;A pattern, as name suggests, denotes a recurring problem in software design and explains its solution in a c&lt;span style="font-size:100%;"&gt;o&lt;/span&gt;ntext. These problem/solution pairs have been applied to the best, time tested solutions that apply to any of such problems irrespective of the field or domain.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Software design pattern deals with how to design classes based on the responsibility, defining their collaborations with other classes. GoF defined 3 categories for the patterns such as Creational, structural and behavioural. Creational patterns deal with who creates the classes and how some of the complicated creation process is encapsulated into certain classes. Structural, as it sounds, deals with class collaborations and associations. Behavioural category is concerned with assigning responsibilities to classes. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Below are the one liners of all the patterns. As i go into details of each pattern, when i explain the DOOP car dealer design (remember? i was referring to DOOP design in my last post), each of these one liners will make more sense.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Creational Patterns&lt;/strong&gt; -&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Abstract Factory&lt;/span&gt; : &lt;/span&gt;&lt;span style="font-size:85%;"&gt;This pattern allows to create family of objects without directly referring to the concrete classes.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Builder&lt;/span&gt; : Separate complex process of constructing a product from its representation. Its kind of encapsulating complex creation process and let client need not know about that process.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Factory Method&lt;/span&gt; : This is nothing but a method to create certain class instance, but this pattern also lets subclasses override this method to create their flavor of class instance. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Prototype&lt;/span&gt; : As name suggests, the class acts as prototype to create other instances of the class. New instances will be constructed or cloned from this prototype.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Singleton &lt;/span&gt;: This pattern controls count of instances of a class. Every client request for an instance of the class will be responded with the same instance. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Structural Patterns - &lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Adapter Pattern&lt;/span&gt; : An adapter class acts an adapter (remember socket adapter ??) between 2 parties where their interfaces are incompatible to each other.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Bridge Pattern&lt;/span&gt; : Decouples an abstraction from implementation so both can vary independently. Will be more clear when we talk in context of an example.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Composite&lt;/span&gt; : Represents part-whole hierarchies and lets client treet a leaf and complex tree uniformly.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Decorator &lt;/span&gt;: Add responsibilities to classes at runtime (dynamically), not at compile time.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Flyweight&lt;/span&gt; : Use sharing to handle huge number of small objects. Limit creation of the very high number of small objects for performance improvement of the app.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Proxy &lt;/span&gt;: acts as proxy for another class and diverts actual calls to the class.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Behavioural Patterns - &lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Chain of Responsibility&lt;/span&gt; - Make client unware of who fulfills the request in the chain of handlers. Avoid coupling between client and ultimate request processor.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Command &lt;/span&gt;- exposes an interface various requests and lets client call same interface for different requests.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Interpreter &lt;/span&gt;- for given language, defines grammer and an interpreter to interpret that language.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Iterator &lt;/span&gt;- as name suggests, provides a way to iterate over collections.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Mediator&lt;/span&gt; - acts as mediator between 2 parties and encapsulates how these parties interact.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Memento &lt;/span&gt;- lets capture and store objects internal state (without violating encapsulation ofcourse), and let the class come backto that state when required (such as serialization..)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Observer&lt;/span&gt; - defines dependency between objects, so that when object's state changes, other objects get notified.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;State&lt;/span&gt; - a pattern that shows the state changes of an entity. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Strategy&lt;/span&gt; - dynamically vary or change the algorithms without modifying the client. examples will follow.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Template method&lt;/span&gt; - Defines an interface method and certain template methods in base class. The pattern lets subclasses override the template methods such that the actual interface method implementation changes.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color:#3366ff;"&gt;Visitor &lt;/span&gt;- visitor lets an operation on an object structure. it allows new operations to be defined on that structure w/o changing old operations.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;In coming post, i will directly jump into DOOP car dealer design and apply some of these patterns to the design to see what problems these patterns solve.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112958568504663087?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112958568504663087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112958568504663087' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112958568504663087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112958568504663087'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/list-of-gof-patterns-overview.html' title='List of GoF Patterns : Overview'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6827867.post-112906375045689433</id><published>2005-10-11T13:42:00.000-07:00</published><updated>2005-10-18T13:52:43.263-07:00</updated><title type='text'>Design Patterns Tutorial</title><content type='html'>To start over, i am going to begin blogging on Design patterns, starting with Gang Of Four. The way i am going to approach is to pickup an example of the sample real world project and design without patterns, and explain the pains without using the patterns. And next refactor the design using patterns one by one to make it better design.&lt;br /&gt;&lt;br /&gt;In the forth coming sessions, we will design a web site for a fictitious car deeler named DOOP. no, its not dupe. its DOOP.. short and mixed form of Design Patters and OO. Before we start making actual requirement analysis, design without and with patterns, you may want to understand what design patterns are all about and how to go about using them.&lt;br /&gt;&lt;br /&gt;Here are some online catalog of patterns.&lt;br /&gt;&lt;a href="http://patternshare.com/"&gt;http://patternshare.com/&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.dofactory.com/Patterns/Patterns.aspx"&gt;http://www.dofactory.com/Patterns/Patterns.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Few books i recommend :&lt;br /&gt;&lt;a href="http://www.amazon.com/gp/product/0201633612/103-3154830-1007045?v=glance&amp;n=283155&amp;amp;n=507846&amp;s=books&amp;amp;v=glance"&gt;Design Patterns by GoF&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.amazon.com/gp/product/0596007124/103-3154830-1007045?v=glance&amp;n=283155&amp;amp;n=507846&amp;s=books&amp;amp;v=glance"&gt;Head First Design Patterns&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6827867-112906375045689433?l=avnrao.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://avnrao.blogspot.com/feeds/112906375045689433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=6827867&amp;postID=112906375045689433' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112906375045689433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6827867/posts/default/112906375045689433'/><link rel='alternate' type='text/html' href='http://avnrao.blogspot.com/2005/10/design-patterns-tutorial.html' title='Design Patterns Tutorial'/><author><name>Narasimha Rao</name><uri>http://www.blogger.com/profile/18179407263572420868</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14918482881734326892'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry></feed>