CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design.

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

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Chapter 7 Testing Class Hierarchies. SWE 415 Chapter 7 2 Reading Assignment  John McGregor and David A. Sykes, A Practical Guide to Testing Object-Oriented.
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
Chapter 6: Using Design Patterns
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
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,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
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.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Design Patterns.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
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.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
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: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
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.
Object- oriented Design Principles
Deriving Abstract Factory Loosening the coupling when creating objects...
Strategy in 15 minutes... derived from the principles.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Design Patterns: MORE Examples
MPCS – Advanced java Programming
Low Budget Productions, LLC
Applying the Principles
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Compositional Design Principles
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Composite Design Pattern By Aravind Reddy Patlola.
Presented by Igor Ivković
Software Engineering and Architecture
Software Engineering and Architecture
Presentation transcript:

CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design

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

CS, AUHenrik Bærbak Christensen3 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, AUHenrik Bærbak Christensen4 Compositional Design Principles

As the process CS, AUHenrik Bærbak Christensen5

CS, AUHenrik Bærbak Christensen6 First Principle

CS, AUHenrik Bærbak Christensen7 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

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…

CS, AUHenrik Bærbak Christensen9 Example Early pay station GUI used JPanel for visual output I only use method: ’setText’

CS, AUHenrik Bærbak Christensen10 Example The I found SoftCollection’s number display, got permission to use it, but And use:

CS, AUHenrik Bærbak Christensen11 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, AUHenrik Bærbak Christensen12 If JPanel was an interface instead! –setText(String s); Then there would be no hard coupling to a specific inheritance hierarchy. GUIIJPanel JPanel LCDDigitDisplay

CS, AUHenrik Bærbak Christensen13 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, AUHenrik Bærbak Christensen14 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, AUHenrik Bærbak Christensen15Henrik Bærbak Christensen15 Umbrella responsibility class Car extends Umbrella ? class Umbrella extends Car ? class Car implements UmbrellaRole NONSENSE! Sensible

CS, AUHenrik Bærbak Christensen16 Second Principle

CS, AUHenrik Bærbak Christensen17 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, AUHenrik Bærbak Christensen18 Benefits of class inheritance 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, AUHenrik Bærbak Christensen19 Encapsulation “inheritance breaks encapsulation” Snyder 1986

CS, AUHenrik Bærbak Christensen20 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, AUHenrik Bærbak Christensen21 Only add responsibilities, never remove You buy the full package! –All methods, all data structures –Even those that are irrelevant or down right wrong!

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) CS, AUHenrik Bærbak Christensen22 Argue why this is a design with many liabilities? How can you rewrite it elegantly using composition? Argue why this is a design with many liabilities? How can you rewrite it elegantly using composition?

CS, AUHenrik Bærbak Christensen23 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, AUHenrik Bærbak Christensen24 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, AUHenrik Bærbak Christensen25 Separate Testing Often, small and well focused abstractions are easier to test than large classes –a) Only ‘bulk testing’ possible –b) Test ‘ExistingService2’ in isolation However, often requires test stubs

CS, AUHenrik Bærbak Christensen26 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, AUHenrik Bærbak Christensen27 Liabilities Increased number of abstractions and objects  Delegation requires more boiler-plate code 

CS, AUHenrik Bærbak Christensen28 (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, AUHenrik Bærbak Christensen29 Third Principle

CS, AUHenrik Bærbak Christensen30 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, AUHenrik Bærbak Christensen31 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, AUHenrik Bærbak Christensen32 The principles in action

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

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

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

CS, AUHenrik Bærbak Christensen36 Principles in action Applying the principles lead to basically the same structure of most patterns:  Favour object composition Client «interface» Variability ConcreteVariation1ConcreteVariation2

CS, AUHenrik Bærbak Christensen37 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, AUHenrik Bærbak Christensen38 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, AUHenrik Bærbak Christensen39 Summary GoF list 23 patterns – but they also list three principles that are essential elements of reusable object- oriented software...