design patterns for fun and profit

33
Design Patterns For Fun And Profit Nikolas Vourlakis

Upload: nikolas-vourlakis

Post on 17-Jan-2017

83 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Design patterns for fun and profit

Design PatternsFor Fun And Profit

Nikolas Vourlakis

Page 2: Design patterns for fun and profit

Agenda● What

● Evolution

● Benefits

● Problems

● Composite

● Builder

Page 3: Design patterns for fun and profit

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

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

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

1977 1987 1994

Evolution

Pattern Languages of Programs Conference

Page 7: Design patterns for fun and profit

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

Classification● Creational

● Structural

● Behavioral

● Security

● Concurrency

● Database/Sql

● User Interface

● Social

● Network

● Distributed

Page 9: Design patterns for fun and profit

… and many more, the list is in constant growth

Page 10: Design patterns for fun and profit

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

Over-engineer / Patterns Happy

Page 12: Design patterns for fun and profit

How to use then?

Page 13: Design patterns for fun and profit

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

Design patterns provide targets for your refactorings ! ! !

Page 15: Design patterns for fun and profit

But How?

Page 16: Design patterns for fun and profit

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

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

What do we have here?

Page 19: Design patterns for fun and profit

Orders

Order

Product

Product

Product

Order

Product

Product

Product

Page 20: Design patterns for fun and profit

A Tree!

Page 21: Design patterns for fun and profit

Composite

Page 22: Design patterns for fun and profit

● 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

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

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

+ 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

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

Builder

Page 28: Design patterns for fun and profit

● 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

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

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

+ 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 33: Design patterns for fun and profit

Questions?