Software Engineering and Architecture

Slides:



Advertisements
Similar presentations
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Advertisements

Objects First with Java A Practical Introduction using BlueJ
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
1 Computer Science 340 Software Design & Testing Inheritance.
Refactoring1 Improving the structure of existing code.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The HotCiv GUI Instantiating the MiniDraw Framework.
Test Stubs... getting the world under control. TDD of State Pattern To implement GammaTown requirements I CS, AUHenrik Bærbak Christensen2.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Deriving State…...and an example of combining behaviour.
Refactoring1 Improving the structure of existing code.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Hints - Mandatory 2 / Strategies. Learning... life-a-curve.html B Christensen2 Jeg fatter ikke.
Multi Dimensional Variance: How to make ultra flexible software!
Mandatory 1 / AlphaCiv … Traps to be aware of …. Warn or not? I once asked Kent Beck the following –I have a lot of students in a course in design patterns.
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.
Mandatory 2 / Strategies Note: I publish this presentation at the week plan for week 4.
Mandatory 1 / AlphaCiv … a few comments…. Overall: Generally – good work – good effort Seems you are generally doing TDD Minor hick-ups –”My own way is.
Patterns are Roles What patterns are and what not…
CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design.
Mandatory 3 Test Stubs State Abstract Factory. Stubs, Spies, and Fake Objects One Example Making distribution testable... Henrik Bærbak Christensen2.
Mandatory 3 Test Stubs State Abstract Factory. Do not panic! dSoftArk is about Good analyzable design not about HotCiv! Henrik Bærbak Christensen2.
Strategy in 15 minutes... derived from the principles.
CSHenrik Bærbak Christensen1 Flexibility and Maintainability And their metrics: coupling and cohesion.
Hints - Mandatory 5 Blackbox Testing Pattern Hunting.
Patterns Protect from Change Rule: if something is going to change, make it an object. Strategy: make algorithm an object so it can change State: make.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Instantiating the MiniDraw Framework
Introducing a Framework ... and a few patterns
Where are we ? Setup Sprites Input Collision Drawing Sprites
Objects First with Java A Practical Introduction using BlueJ
Applying the Principles
CS 350 – Software Design The Strategy Pattern – Chapter 9
Behavioral Design Patterns
Mandatory 1 / AlphaCiv … Traps to be aware of ….
Leftover Patterns Chain of Responsibility
Compositional Design Principles
Week 4 Object-Oriented Programming (1): Inheritance
Section 11.1 Class Variables and Methods
Interfaces and Inheritance
Behavioral and Structural Patterns
MSIS 670 Object-Oriented Software Engineering
CS 350 – Software Design A Standard Object-Oriented Solution – Chapter 4 Before studying design patterns, many programmers solve a problem by starting.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Advanced Java Programming
Improving the structure of existing code
DESIGN PATTERNS : Strategy Pattern
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Introduction to Design Patterns
7. Decorator SE2811 Software Component Design
Interfaces.
Final and Abstract Classes
European conference.
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
HFOOAD Chapter 5 Interlude
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Presentation transcript:

Software Engineering and Architecture Comments on Mandatory and Stuff

‘Do not talk to strangers’ If you talk through a chain of objects, you have high coupling which is bad Do not have 2+ ‘dots’ in any statement x.getY().getZ().getS().getT().getU().doThing() What about Java Stream API then??? CS@AU Henrik Bærbak Christensen

Exceptions and Error Codes Question from Student Modern code have stuff like Optional<T> and Result<Val,Code> to be returned; are exceptions not old fashioned? HTTP calls return also a multivalued object Body and Status Code, like 404 not found or 200 OK Still: Prefer exceptions because Exceptions travel up the call stack without testing at all levels! CS@AU Henrik Bærbak Christensen

Name Booleans? Learning = overdo it  Or why not: // Test that there are cities in list If (!cities.size() > 0) return null; No, comments get out of date quickly! CS@AU Henrik Bærbak Christensen

WinnerStrategies and State ZetaCiv is a pain…

Henrik Bærbak Christensen Problem statement When doing compositional designs we Cut behavior that is often otherwise in the same abstraction, and therefore have access to the same state (i.e. The same set of instance variables)... Parametric: all in the same class Polymorphic: subclasses still contain all of superclass var. ... into multiple objects, like A and B But how do they share/exchange state?!? Some in A, some in B? All in B? All in A? Henrik Bærbak Christensen

Henrik Bærbak Christensen Example WinnerStrategy: Algorithm to determine winner Based upon knowledge of Alpha: The age (correlates to rounds) Beta: Owner of all cities Epsilon: 3 attacks won by player Zeta: Rounds+Beta+Epsilon’ Epsilon’ = counting won attacks only starts after 20 rounds But who is responsible for ”knowing it”? Henrik Bærbak Christensen

Henrik Bærbak Christensen Example Who should know it? I.e. have the state variable? Game or the WinnerStrategy or some third object? Number of rounds played/age Owner of all cities Attacks won by player Counting only starts after 20 rounds Obviously a responsibility of Game to know Not obviously something that Game should care about… Henrik Bærbak Christensen

Henrik Bærbak Christensen The options What are our options A) Game / GameImpl Putting state information there which is variant specific That is: Only demanded by a single or two variants Lowering cohesion And the big issue is how to handle ‘reset’ in ZetaCiv after 20 rounds? B) Strategies Putting variant specific state information in the variant strategy High cohesion But – We then have to tell the strategies C) Move responsibility to a third object Game has-a GameStatistics object CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen B) In the Strategies Say we store it in the strategies (battles won) How to tell the strategy when battle is won, then? A) Add methods to WinnerStrategy incrementBattlesWonBy(Player p) incrementRoundsPlayed(); B) Make WinnerStrategy observer on game events WinnerStrategy extends GameObserver battleWonEvent(Player p); roundEndEvent(); Both solutions require that Game informs strategy object If (battleWon) {winnerstrategy.incrementBattlesWonBy(player);} CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen B) Resetting ZetaCiv just delegates to its ‘currentState’ isBattleWon(…) { currentState.isBattleWon(…); } Means resetting battle counter is easy In betaState it forwards request to beta’s battle counting method Which does nothing! Only in epsilonState does it forward to epsilon’s state And as it changes state only 20 rounds, no calls are made before that; and epsilon only starts counting then! CS@AU Henrik Bærbak Christensen

C) Exploring the GameStatistics I implemented the GameStatistics option last year… int w = game.getStatistics().getBattlesWonBy(Player.GREEN); Game will increment this counter every time a battle is won Reset becomes a headache! Do not want to reset the counters after 20 rounds; it is then not game’s statistics but just a silly strategy’s statistics Solve it by providing epsilon’s winner strategy with a decorated game instance We will talk about Decorator Pattern later… CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Tedious… Conclusion: Close to a Do Over CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen So – a Conclusion? I would go for option B) Put highly variant specific state in the variant specific strategies/delegates Add code in GameImpl to tell delegates about state changes Direct method calls or Observer pattern Regarding Observer pattern A bit of an over-engineered solution (You Ain’t Gonna Need It) UNLESS you can see obvious other benefits of the approach Like have a in-game statistics system that lists all major battles, events, in the game, as indeed Civ games often have… CS@AU Henrik Bærbak Christensen

A general approach (Remember: consider cost and benefits!) Context Information A general approach (Remember: consider cost and benefits!)

Context for Strategies Source: Finn Rosenbech Jensen, former TA, 2009 The problem we have been struggling with WinnerStrategy: Have to use all kinds of info from Game Shall we pass ”Game” or ”GameImpl” ? UnitActionStrategy: Have to modify game Shall we pass data structures back and forth? Shall we introduce new mutator methods in Game? CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Private Interface James Newkirk pattern Private interface Responsibility: Publish just the context information for WinnerStrat. Only provide that to the strategy CS@AU Henrik Bærbak Christensen

How to Call? Or: b) Have a private inner class c) Let ‘GameImpl implements Game, WinnerContext’ CS@AU Henrik Bærbak Christensen

Benefits/Liabilities Better decoupling Strategies are not coupled to game, i.e. cannot invoke ‘moveUnit()’ No bloating of Game/GameImpl with new methods Liabilities Yet another interface to overview Constant modifications to WinnerStrategyContext as we add more and more winner strategies CS@AU Henrik Bærbak Christensen

Interface Segregation Observation (Robert Martin) Cut Game into two: Query and Command interface Command methods / mutators here CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Can then be extended Public interface GameInternalManipulor extends Game public void createCityAt(Position p); public void createUnitAt(Position p); } Which GameImpl then implements and then UnitActionStategy { performAction(GameInternalManipulator g);} Now GammaCiv’s settler can create city as it has access to the ‘createCityAt()’ method CS@AU Henrik Bærbak Christensen

Benefits/Liabilities High cohesion in each of the interfaces Low coupling Each interface only talks with just enough game behaviour Liabilities But it comes at the cost of many more interfaces An each new feature probably still requires ‘change by modification’ That is, the same cost as if I had passed the GameImpl Conclusion: It depends  CS@AU Henrik Bærbak Christensen