Design patterns for fun and profit
-
Upload
nikolas-vourlakis -
Category
Software
-
view
81 -
download
0
Transcript of Design patterns for fun and profit
![Page 1: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/1.jpg)
Design PatternsFor Fun And Profit
Nikolas Vourlakis
![Page 2: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/2.jpg)
Agenda● What
● Evolution
● Benefits
● Problems
● Composite
● Builder
![Page 3: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/3.jpg)
Each pattern describes a problem which occurs over and over again in our
environment, and then describes the core of the solution to that problem, in such a
way that you can use this solution a million times over, without ever doing it the same
way twice.
Christopher Alexander - A Pattern language: Towns, Buildings, Construction (1977)
1977 1987 1994
Evolution
![Page 4: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/4.jpg)
5 patterns for designing window-based user interfaces
Kent Beck & Ward Cunningham at OOPSLA (1987)
http://c2.com/doc/oopsla87.html
1977 1987 1994
Evolution
![Page 5: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/5.jpg)
1977 1987 1994
Evolution
Program to an interface, not
an implementation
Favor object composition
over class inheritance
![Page 6: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/6.jpg)
1977 1987 1994
Evolution
Pattern Languages of Programs Conference
![Page 7: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/7.jpg)
Classification● Point of view changes what is and isn’t a pattern
● Choice of programming language is important
● Programming paradigm (Object-Oriented, Functional, Procedural, etc)
![Page 8: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/8.jpg)
Classification● Creational
● Structural
● Behavioral
● Security
● Concurrency
● Database/Sql
● User Interface
● Social
● Network
● Distributed
![Page 9: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/9.jpg)
… and many more, the list is in constant growth
![Page 10: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/10.jpg)
What we Gain?● Common vocabulary
● Identify less-obvious abstractions and the objects that can capture them
● Easier to understand existing systems …
● Increase project code quality …
sometimes
sometimes
![Page 11: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/11.jpg)
Over-engineer / Patterns Happy
![Page 12: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/12.jpg)
How to use then?
![Page 13: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/13.jpg)
Refactoring is the process of changing
a software system in such a way that
it does not alter the external behavior
of the code yet improves its internal
structure.
Martin Fowler
Refactoring
![Page 14: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/14.jpg)
Design patterns provide targets for your refactorings ! ! !
![Page 15: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/15.jpg)
But How?
![Page 16: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/16.jpg)
public class OrderSerializer { public String toXML(Order[] orders) { StringBuffer xml = new StringBuffer(); xml.append("<orders>"); for (Order order : orders) { xml.append("<order id='"); xml.append(order.getId()); xml.append("'>"); for (Product product : order.getProducts()) { xml.append("<product id='"); xml.append(product.getId()); xml.append("'>"); xml.append("<name>"); xml.append(product.getName()); xml.append("</name>"); xml.append("</product>"); } xml.append("</order>"); } xml.append("</orders>"); return xml.toString(); }}
Transform orders to XML Taken from Refactoring to Patterns
![Page 17: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/17.jpg)
Problems● Show 10 more properties of product?
● How about implementing ClientSerializer?
● Difficult to work with
● Similar logic all over the place
● Build & representation coupled together
![Page 18: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/18.jpg)
What do we have here?
![Page 19: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/19.jpg)
Orders
Order
Product
Product
Product
Order
Product
Product
Product
![Page 20: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/20.jpg)
A Tree!
![Page 21: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/21.jpg)
Composite
![Page 22: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/22.jpg)
● Structural pattern
● Represent part-whole hierarchies
● Treat these objects uniformly
Composite Component
Operation()Add(Component)Remove(Component)GetChild(int)
Leaf
BuildPart()
Composite
Operation()Add(Component)Remove(Component)GetChild(int)
![Page 23: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/23.jpg)
public class TagNode { /* private vars */ public TagNode(String name) { /* ... */ } public void addValue(String value) { this.value = value; } public void addAttribute(String attribute, String value) { this.attributes.append(" "); this.attributes.append(attribute); this.attributes.append("='"); this.attributes.append(value); this.attributes.append("'"); } public String getName() { /* ... */ } public void setParent(TagNode parent) { /* ... */ } public TagNode getParent() { /* ... */ }
public void add(TagNode node) { /* ... */ } public String toString() { String result; result = "<" + name + attributes + ">"; for (Object childNode : this.children) { TagNode node = (TagNode) childNode; result += node.toString(); } result += value; result += "</" + name + ">"; return result; }}
![Page 24: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/24.jpg)
public class OrderSerializer { public String toXML(Order[] orders) { TagNode ordersTag = new TagNode("orders");
for (Order order : orders) { TagNode orderTag = new TagNode("order"); orderTag.addAttribute("id", String.valueOf(order.getId()));
for (Product product : order.getProducts()) { TagNode nameTag = new TagNode("name"); nameTag.addValue(product.getName()); TagNode productTag = new TagNode("product"); productTag.addAttribute("id", String.valueOf(product.getId())); productTag.add(nameTag); orderTag.add(productTag); } ordersTag.add(orderTag); } return ordersTag.toString(); }}
![Page 25: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/25.jpg)
+ Hide repetitive instructions
+ Generalized way to handle similar logic
+ Removes responsibilities from client
- Complicate design
Consequences
OrderSerializer TagNode
![Page 26: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/26.jpg)
New Problems● Knowledge how a complex object is constructed
● Client coupled with composite
● Difficult if we need to swap TagNode with something else in the future
![Page 27: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/27.jpg)
Builder
![Page 28: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/28.jpg)
● Creational pattern
● Separate construction of a
complex object from its internal
representation
● Able to create different
representations with the same
construction process
BuilderDirector
Construct()
Builder
BuildPart()
ConcreteBuilder
BuildPart()GetResult()
Product
![Page 29: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/29.jpg)
class TagBuilder { private final TagNode root; /* other private members */
public TagBuilder(String name) { root = new TagNode(name); }
public String toXml() { return root.toString(); }
public void addToParent(String parentName, String childName) { /* ... */ } public void addChild(String name) { /* ... */ } public void addSibling(String name) { /* ... */ } public void addAttribute(String attribute, String value) { /* ... */ } public void addValue(String value) { /* ... */ }}
![Page 30: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/30.jpg)
public class OrderSerializer { public String toXML(Order[] orders) { TagBuilder builder = new TagBuilder("orders"); for (Order order : orders) { builder.addToParent("orders", "order"); builder.addAttribute("id", "1"); for (Product product : order.getProducts()) { builder.addToParent("order", "product"); builder.addAttribute("id", String.valueOf(product.getId())); builder.addChild("name"); builder.addValue(product.getName()); } } return builder.toXml(); }}
![Page 31: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/31.jpg)
+ Simplify client construction code
+ Reduces repetitive and error-prone
creation process
+ Client is not aware of the composite
+ We are free to change composite
whenever we want
- Complicate design
Consequences
OrderSerializer
TagNode
TagBuilder
![Page 32: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/32.jpg)
What to Read?● Head First Design Patterns
● Design Patterns, Elements of Reusable Object-Oriented Software
● Refactoring to Patterns
● Patterns of Enterprise Application Architecture
![Page 33: Design patterns for fun and profit](https://reader033.fdocuments.us/reader033/viewer/2022042723/587df9df1a28abab7e8b68c1/html5/thumbnails/33.jpg)
Questions?