PA103 - Object-oriented Methods for Design of Information Systems How to Think When Applying Design Patterns © Radek Ošlejšek Fakulta informatiky MU oslejsek@fi.muni.cz PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Initial model -lines : Set +addItem(product) +removeItem(product) +getLines() : Set Order -product -price -numPieces OrderLine * lines PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goals  Goals: extend the model so that:  A) We are able to sort order lines by name, date, price, and other criteria.s  B) We have various types of orders, e.g. for retail customers vs. bulk customers. They differ in stored data.  C) both.  Approach:  Ad A) Employ behavioral design patterns because sorting is behavior.  Ad B) Employ structural design patterns because we have to deal with spreading pieces of information across classes.  Ad C) Put them together. PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goal A: Sorted lines  Design decision: Should be the Order class responsible for sorting lines or is it the responsibility of that who wants it?  Probably the second case is correct. But to demonstrate the application of design pattern we give this responsibility to Order class. PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Sorted lines: Naive solution  Adding a new ordering requires to change the existing Order class. Two naive solutions: -lines : Set +addItem(product) +removeItem(product) +getLines(sorting) : SortedSet Order -product -price -numPieces OrderLine * lines -lines : Set +addItem(product) +removeItem(product) +getLinesByProduct() : SortedSet +getLinesByPrice() : SortedSet Order -product -price -numPieces OrderLine * lines PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Sorted lines: Iterator  Iterators providing lines in a specific sequence.  But how to say what iterator is to be used?  By parameter or providing more variants of the getIterator() method?  Similar problem to the previous naive solution. Nevertheless, the code of the getIterator() is more simple than in the naive approach -lines : Set +addItem(product) +removeItem(product) +getIterator() : Iterator Order -product -price -numPieces OrderLine * +current() : OrderLine +next() : boolean Iterator +current() : OrderLine +next() : boolean SortByProductIterator +current() : OrderLine +next() : boolean SortByPriceIterator <> PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Sorted lines: Strategy  A client decides which strategy to use! -product -price -numPieces OrderLine -lines : Set +addItem(product) +removeItem(product) +getLines(SortingStrategy strategy) : SortedSet SortingStrategy 1 +sort(Set lines) : SortedSet +addItem(product) +removeItem(product) +getLines(Comparator c) : SortedSet> Comparator +compare(OrderLine o1, Orderline o2) :... ProductComparator +compare(OrderLine o1, Orderline o2) :... PriceComparator 1 PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goal B: Variable Orders … just make sub-classes -lines : Set +addItem(product) +removeItem(product) +getLines() : Set Order -product -price -numPieces OrderLine * lines -lines : Set +addItem(product) +removeItem(product) +getLines() : Set Order -product -price -numPieces OrderLine * BulkOrder RetailOrder PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goal C: Variable Orders With Sorting Strategies … explosion of sub-classes => apply Bridge -lines : Set +addItem(product) +removeItem(product) +getLines() : SortedSet Order BulkOrder RetailOrder +getLines() : SortedSet BulkOrderSortedByProduct +getLines() : SortedSet BulkOrderSortedByPrice +getLines() : SortedSet RetailOrderSortedByProduct +getLines() : SortedSet RetailOrderSortedByPrice -product -price -numPieces OrderLine * PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goal C: Bridge in Action  OK, but Bridge, as a structural pattern, does not explain neither who chooses the Items sub-class nor who sets the schoosen sub-class to Order. These dynamic aspects are defined by behavioral patterns.  In this case, the Items class can be understood as a sorting strategy. -shippingAddress -date -items : Items +addItem(product) +removeItem(product) +getLines() : SortedSet <> Order BulkOrder RetailOrder -lines : Set +addItem(product) +removeItem(product) +getLines() : SortedSet <> Items1 -product -price -numPieces OrderLine * +getLines() : SortedSet ItemsSortedByProduct +getLines() : SortedSet ItemsSortedByPrice PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MUSpring 2016 Goal C: Strategy in Action (Again ;)  Is it Bridge or Strategy? It depents on the point of view. Static view => Bridge, dynamic view => Strategy.  Only sorting by product or by price is allowed. But what if we want to combine sorting methods?  Define combined strategy or use Decorator -lines : Set +addItem(product) +removeItem(product) +getLines(SortingStrategy strategy) : SortedSet Order +sort(Set lines) : SortedSet <> SortingStrategy +sort(Set lines) : SortedSet +addItem(product) +removeItem(product) +getLines() : SortedSet Order +specificBulkMethod() BulkOrder +specificRetailMethod() RetailOrder SortedOrder 1 OrderSortedByProduct OrderSortiedByPrice -product -price -numPieces OrderLine * PA103: Object-oriented Methods for Design of Information Systems IS © R. Ošlejšek, FI MU 14Spring 2016 Questions?