Compositional Design Principles

Slides:



Advertisements
Similar presentations
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Advertisements

Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Henrik Bærbak Christensen1 Frameworks / Product Lines The frame for reuse.
Test Stubs... getting the world under control. TDD of State Pattern To implement GammaTown requirements I CS, AUHenrik Bærbak Christensen2.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Coming up: Inheritance
Deriving State…...and an example of combining behaviour.
Multi Dimensional Variance: How to make ultra flexible software!
Applying the Principles Two Examples. Example 1 New Requirement It would be nice with a simple GUI “to see something” instead of just xUnit tests...
MiniDraw Introducing a Framework... and a few patterns.
CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design.
Strategy in 15 minutes... derived from the principles.
Instantiating the MiniDraw Framework
Patterns in programming
Introducing a Framework ... and a few patterns
CSC 222: Object-Oriented Programming
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
GRASP – Designing Objects with Responsibilities
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
A Brief Introduction to Design Patterns
Low Budget Productions, LLC
Applying the Principles
03/10/14 Inheritance-2.
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Copyright © by Curt Hill
Week 4 Object-Oriented Programming (1): Inheritance
The Object-Oriented Thought Process Chapter 08
Presented by Igor Ivković
Chapter 6: Using Design Patterns
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Design Patterns Part 1
Polymorphism and access control
Informatics 122 Software Design II
DEV-08: Exploring Object-oriented Programming
Overriding Methods & Class Hierarchies
CS 350 – Software Design Principles and Strategies – Chapter 14
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Object Oriented Design
Chapter 8 Inheritance Part 2.
Chapter 8, Design Patterns Introduction
Composite Design Pattern By Aravind Reddy Patlola.
Presented by Igor Ivković
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Presentation transcript:

Compositional Design Principles The “GoF” principles Or Principles of Flexible Design CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Gang of Four (GoF) Erich Gamma, Richard Helm Ralph Johnson & John Vlissides Design Patterns – Elements of Reusable Object-Oriented Software Addison-Wesley, 1995. (As CD, 1998) First systematic software pattern description. CS, AU Henrik Bærbak Christensen

The most important chapter Section 1.6 of GoF has a section called: How design patterns solve design problems This section is the gold nugget section It ties the patterns to the underlying coding principles that delivers the real power. CS, AU Henrik Bærbak Christensen

Compositional Design Principles CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen As the 3-1-2 process CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen First Principle CS, AU Henrik Bærbak Christensen

(the responsibilities+protocol) GoF’s 1st principle Program to an interface, not an implementation In other words Assume only the role (the responsibilities+protocol) … and never allow yourself to be coupled to implementation details and concrete behaviour CS, AU Henrik Bærbak Christensen

First Principle Program to an interface because You only collaborate with the role – not an individual object You are free to use any service provider class! You do not delimit other developers for providing their service provider class! You avoid binding others to a particular inheritance hierarchy Which you would do if you use (abstract) classes…

Henrik Bærbak Christensen Example Early pay station GUI used JPanel for visual output I only use method: ’setText’ I found this GUI in sa-E05 CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Example The I found SoftCollection’s number display, got permission to use it, but... ... And use: CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Morale I would have been easy to make the code completely identical, and thus support full reuse, in which I simply configure PayStationGUI with the proper ’text panel’ to use. But I cannot! Because LCDDigitDisplay does not inherit JPanel!!! Thus instead of dependency injection and change by addition I get Change by modification I have to start eclipse just to change one declaration! I can never get a framework out of this! CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen If JPanel was an interface instead! setText(String s); Then there would be no hard coupling to a specific inheritance hierarchy. GUI IJPanel JPanel LCDDigitDisplay CS, AU Henrik Bærbak Christensen

Interfaces allow fine-grained behavioural abstractions Clients can be very specific about the exact responsibility it requires from its service provider Example: Collections.sort( List l ) can sort a list of any type of object if each object implements the interface Comparable i.e. must implement method CompareTo(Object o) Low coupling – no irrelevant method dependency! CS, AU Henrik Bærbak Christensen

Interfaces better express roles Interfaces express specific responsibilities whereas classes express concepts. Concepts usually include more responsibilities and they become broader! Small, very well defined, roles are easier to reuse as you do not get all the “stuff you do not need...” public interface Drawing extends SelectionHandler, FigureChangeListener, DrawingChangeListenerHandler { ... } public class StandardSelectionHandler implements SelectionHandler {...} CS, AU Henrik Bærbak Christensen

Umbrella responsibility class Car extends Umbrella ? class Umbrella extends Car ? class Car implements UmbrellaRole NONSENSE! Sensible CS, AU Henrik Bærbak Christensen Henrik Bærbak Christensen 15

Henrik Bærbak Christensen Second Principle CS, AU Henrik Bærbak Christensen

And the compositional one should be favored! GoF’s 2nd principle Favor object composition over class inheritance What this statement says is that there are basically two ways to reuse code in OO! And the compositional one should be favored! CS, AU Henrik Bærbak Christensen

Benefits of class inheritance You get the “whole packet” and “tweak a bit” by overriding a single or few methods Fast and easy (very little typing!) Explicit in the code, supported by language (you can directly write “extends”) But... CS, AU Henrik Bærbak Christensen

“inheritance breaks encapsulation” Snyder 1986 CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Why? No encapsulation because Subclass can access every instance variable/property data structure Method Of any superclass (except those declared private) Thus a subclass and superclass are tightly coupled You cannot change the root class’ data structure without refactoring every subclass in the complete hierarchy  CS, AU Henrik Bærbak Christensen

Only add responsibilities, never remove You buy the full package! All methods, all data structures Even those that are irrelevant or down right wrong! CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Example Java utility library version 1.1 Vector (= modern ArrayList) Void add(int index, E element) Stack extends Vector E pop() void push(E item) Argue why this is a design with many liabilities? How can you rewrite it elegantly using composition? CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Compile time binding The only way to change behaviour in the future (tweak a bit more) is through the edit-compile-debug-debug-debug-debug cycle CS, AU Henrik Bærbak Christensen

Recurring modifications Constantly bubling of behaviour up into the root class in a hierarchy Review the analysis in the State pattern chapter Another example Nice service based upon ArrayList Now – want better performance in new variant All three classes modified  CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Separate Testing Often, small and well focused abstractions are easier to test than large classes a) Only integration testing possible (NewS. + ExistS.) b) Allows unit testing of ‘ExistingService2’ However, often requires test stubs CS, AU Henrik Bærbak Christensen

Increase possibility of reuse Smaller abstractions are easier to reuse Example (from MiniDraw) Sub responsibility Allow compositional reuse of selection handler in all present and future impl. of Drawing! CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Liabilities Increased number of abstractions and objects  Delegation requires more boiler-plate code  CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen (what is he saying???) Inheritance is an interesting construct, but I haven’t seen any good examples  It does not elegantly handle ad hoc reuse modelling roles variance of behaviour CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Third Principle CS, AU Henrik Bærbak Christensen

Consider what should be variable in your design GoF’s 3rd principle Consider what should be variable in your design [GoF §1.8, p.29] Another way of expressing the 3rd principle: Encapsulate the behaviour that varies CS, AU Henrik Bærbak Christensen

This statement is closely linked to the shorter Analysis This statement is closely linked to the shorter Change by addition, not by modification That is – you identify the design/code that should remain stable the design/code that may vary and use techniques that ensure that the stable part – well – remain stable These techniques are 1st and 2nd principle most of the time  CS, AU Henrik Bærbak Christensen

The principles in action CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Principles in action Applying the principles lead to basically the same structure of most patterns: New requirement to our client code Client CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Principles in action Applying the principles lead to basically the same structure of most patterns:  Consider what should be variable Variability Client CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Principles in action Applying the principles lead to basically the same structure of most patterns:  Program to an interface «interface» Variability Client CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Principles in action Applying the principles lead to basically the same structure of most patterns:  Favour object composition «interface» Variability Client ConcreteVariation1 ConcreteVariation2 CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Summary  We identified some behaviour that was likely to change…  We stated a well defined responsibility that covers this behaviour and expressed it in an interface  Instead of performing behaviour ourselves we delegated to an object implementing the interface  Consider what should be variable in your design  Program to an interface, not an implementation  Favor object composition over class inheritance CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Consideration Beware – it is not a process to follow blindly Often the key point is principle 2: look over how you may compose the resulting behavior most reasonable Examples Abstract Factory: We did not make a ReceiptIssuer specifically for receipts but found a more general concept Decorator + Proxy: Sometimes the ‘encapsulation of what varies’ can be the whole abstraction and the solution relies on composition of ‘large’ objects. CS, AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Summary GoF list 23 patterns – but they also list three principles that are essential... ... elements of reusable object-oriented software... CS, AU Henrik Bærbak Christensen