Inf1-OP Refactoring and Design Patterns Volker Seeker School of - - PowerPoint PPT Presentation

inf1 op
SMART_READER_LITE
LIVE PREVIEW

Inf1-OP Refactoring and Design Patterns Volker Seeker School of - - PowerPoint PPT Presentation

Inf1-OP Refactoring and Design Patterns Volker Seeker School of Informatics March 8, 2019 Refactoring Hockey Analysis Season Game -minutesPlayed:double -games:List<Game> -goalsAgainst:int -shotsOnGoalAgainst:int


slide-1
SLIDE 1

Inf1-OP

Refactoring and Design Patterns Volker Seeker

School of Informatics

March 8, 2019

slide-2
SLIDE 2

Refactoring

slide-3
SLIDE 3

Hockey Analysis

Game

+getShotsOnGoalAgainst():int

  • minutesPlayed:double
  • goalsAgainst:int
  • shotsOnGoalAgainst:int

+getGoalsAgainst():int +getMinutesPlayed():double

Season

+getGames():List<Game>

  • games:List<Game>

+removeGame(Game):void +getGoalieStatistics():GoalieStatistics +addGame(Game):void

GoalieStatistics

?

Implement a class to calculate goalie statistics from all games in a season.

slide-4
SLIDE 4

Hockey Analysis

◮ Goals Against Average (GAA): Number of goals scored against a goalie divided by the number of minutes the goalie played for a season multiplied by 60 (number of minutes in a hockey game).

slide-5
SLIDE 5

Hockey Analysis

◮ Goals Against Average (GAA): Number of goals scored against a goalie divided by the number of minutes the goalie played for a season multiplied by 60 (number of minutes in a hockey game). GAA = 60

  • GA

timeplayedmins

slide-6
SLIDE 6

Hockey Analysis

◮ Goals Against Average (GAA): Number of goals scored against a goalie divided by the number of minutes the goalie played for a season multiplied by 60 (number of minutes in a hockey game). GAA = 60

  • GA

timeplayedmins

  • ◮ Save Percentage (SV%): Total number of saves (shots on

goal, SOG, minus the number of goals scored on a goalie) divided by the total SOG.

slide-7
SLIDE 7

Hockey Analysis

◮ Goals Against Average (GAA): Number of goals scored against a goalie divided by the number of minutes the goalie played for a season multiplied by 60 (number of minutes in a hockey game). GAA = 60

  • GA

timeplayedmins

  • ◮ Save Percentage (SV%): Total number of saves (shots on

goal, SOG, minus the number of goals scored on a goalie) divided by the total SOG. SV % = SOG−Goals

SOG

slide-8
SLIDE 8

Hockey Analysis

Game

+getShotsOnGoalAgainst():int

  • minutesPlayed:double
  • goalsAgainst:int
  • shotsOnGoalAgainst:int

+getGoalsAgainst():int +getMinutesPlayed():double

Season

+getGames():List<Game>

  • games:List<Game>

+removeGame(Game):void +getGoalieStatistics():GoalieStatistics +addGame(Game):void

GoalieStatistics

?

Let’s make a rough plan first!

slide-9
SLIDE 9

Hockey Analysis

Game

+getShotsOnGoalAgainst():int

  • minutesPlayed:double
  • goalsAgainst:int
  • shotsOnGoalAgainst:int

+getGoalsAgainst():int +getMinutesPlayed():double

Season

+getGames():List<Game>

  • games:List<Game>

+removeGame(Game):void +getGoalieStatistics():GoalieStatistics +addGame(Game):void

GoalieStatistics

+getSavePercentage():double

  • season:Season

+getGoalsAgainstAverage:double

Let’s make a rough plan first!

slide-10
SLIDE 10

Test Driven Development

A set of unit tests is provided to check the expected behaviour

  • f the code.
  • 1. implement test to check expected behaviour
  • 2. implement feature to make test pass
slide-11
SLIDE 11

Hockey Analysis

Game

+getShotsOnGoalAgainst():int

  • minutesPlayed:double
  • goalsAgainst:int
  • shotsOnGoalAgainst:int

+getGoalsAgainst():int +getMinutesPlayed():double

Season

+getGames():List<Game>

  • games:List<Game>

+removeGame(Game):void +getGoalieStatistics():GoalieStatistics +addGame(Game):void

GoalieStatistics

+getSavePercentage():double

  • season:Season

+getGoalsAgainstAverage:double

We use test driven development.

slide-12
SLIDE 12

Hockey Analysis

Game

+getShotsOnGoalAgainst():int

  • minutesPlayed:double
  • goalsAgainst:int
  • shotsOnGoalAgainst:int

+getGoalsAgainst():int +getMinutesPlayed():double

Season

+getGames():List<Game>

  • games:List<Game>

+removeGame(Game):void +getGoalieStatistics():GoalieStatistics +addGame(Game):void

GoalieStatistics

+getSavePercentage():double

  • season:Season

+getGoalsAgainstAverage:double

GoalieStatisticsT est

We use test driven development.

slide-13
SLIDE 13

Demo

slide-14
SLIDE 14

Refactoring

We have now successfully improved the internal structure of the code using small incremental steps whilst maintaining the original program logic.

slide-15
SLIDE 15

Refactoring

We have now successfully improved the internal structure of the code using small incremental steps whilst maintaining the original program logic.

We have refactored the code.

slide-16
SLIDE 16

Importance of Tests

It is important that the expected behaviour of the code does not change during refactoring.

Source: https://dzone.com/articles/what-is-refactoring

slide-17
SLIDE 17

Importance of Tests

Optimally, refactoring is part of the test driven development

  • cycle. But this is not always possible.

Source: https://dzone.com/articles/what-is-refactoring

slide-18
SLIDE 18

Refactoring Techniques A large catalog of standard refactoring techniques exists, for example:

◮ loop splitting ◮ method extraction ◮ renaming ◮ . . .

https://www.refactoring.com/catalog/

slide-19
SLIDE 19

Code Smell Identify the need for refactoring based on Warning signs in your own code.

slide-20
SLIDE 20

Code Smell A large list of common code smells exist, for example:

◮ Bloaters ◮ Object-Orientation Abusers ◮ Change Preventers ◮ . . .

https://refactoring.guru/refactoring/smells

slide-21
SLIDE 21

Design Patterns

slide-22
SLIDE 22

Towards Software Engineering

First learn your basic tools and material.

Source: https://i.kinja-img.com/gawker-media/image/upload/s–Zo3E8URT– /c scale,f auto,fl progressive,q 80,w 800/18muwoa3oozw6jpg.jpg

slide-23
SLIDE 23

Towards Software Engineering

First learn your basic tools and material. Then build large houses ...

Source: http://hannesdorfmann.com/images/legohouse/legohouse.jpg

slide-24
SLIDE 24

Towards Software Engineering

First learn your basic tools and material. Then build large houses ...or even cities.

Source: https://i.kinja-img.com/gawker-media/image/upload/s–uTscbBDV– /c scale,f auto,fl progressive,q 80,w 800/tu3yxy86lxwmw5vw8yeu.jpg

slide-25
SLIDE 25

Design Patterns Software Design Patterns are blueprints of solutions for common software design problems.

Source: https://www.rff.com/cloverleaf.png

slide-26
SLIDE 26

Classification

◮ Creational Patterns ◮ Structural Patterns ◮ Behavioural Patterns

slide-27
SLIDE 27

Creational Example: Singleton

Problem

◮ access a resource in your program

slide-28
SLIDE 28

Creational Example: Singleton

Problem

◮ access a resource in your program → database resource

slide-29
SLIDE 29

Creational Example: Singleton

Problem

◮ access a resource in your program → database resource ◮ initialising resource access is expensive

slide-30
SLIDE 30

Creational Example: Singleton

Problem

◮ access a resource in your program → database resource ◮ initialising resource access is expensive → only one instance

slide-31
SLIDE 31

Creational Example: Singleton

Problem

◮ access a resource in your program → database resource ◮ initialising resource access is expensive → only one instance ◮ multiple classes need access

slide-32
SLIDE 32

Creational Example: Singleton

Problem

◮ access a resource in your program → database resource ◮ initialising resource access is expensive → only one instance ◮ multiple classes need access → globally available

slide-33
SLIDE 33

Creational Example: Singleton Solution?

public class Database { private final DBConnection connection; public Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public List<String> query(String q) { ... }

slide-34
SLIDE 34

Creational Example: Singleton Solution?

public class Database { private final DBConnection connection; public Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public List<String> query(String q) { ... } Globally available instance not guaranteed!

slide-35
SLIDE 35

Creational Example: Singleton Solution!

public class Database { private static Database dbase; private final DBConnection connection; public Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public List<String> query(String q) { ... } Add private static field for storing the singleton instance.

slide-36
SLIDE 36

Creational Example: Singleton Solution!

public class Database { private static Database dbase; private final DBConnection connection; public Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public static Database getInstance() { // ? } public List<String> query(String q) { ... } Declare public static creation method to access the singleton instance.

slide-37
SLIDE 37

Creational Example: Singleton Solution!

public class Database { private static Database dbase; private final DBConnection connection; public Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public static Database getInstance() { if(dbase == null) dbase = new Database(); return dbase; } public List<String> query(String q) { ... } Lazily create the instance of the singleton if necessary and return it.

slide-38
SLIDE 38

Creational Example: Singleton Solution!

public class Database { private static Database dbase; private final DBConnection connection; private Database() { connection = new DBConnection("myuser", "myhost", "mydatabase"); connection.connect(); } public static Database getInstance() { if(dbase == null) dbase = new Database(); return dbase; } public List<String> query(String q) { ... } Make the singleton constructor private.

slide-39
SLIDE 39

Creational Example: Singleton Solution!

public static void main(String[] args) { Database db = Database.getInstance(); db.query(args[0]); } In a client, use the getInstance method to access the singleton.

slide-40
SLIDE 40

Structural Example: Facade

Problem

◮ you need to integrate a complex library into your own codebase ◮ many interdependencies between your own code and the third party code

slide-41
SLIDE 41

Structural Example: Facade

Problem

◮ you need to integrate a complex library into your own codebase ◮ many interdependencies between your own code and the third party code What if a new version of this library is suddenly broken? What if you find a better library?

slide-42
SLIDE 42

Structural Example: Facade

Solution

Use a facade class which provides a simple interface to the library code.

Source: https://refactoring.guru/design-patterns/facade

slide-43
SLIDE 43

Behavioural Example: Observer

Problem

How to best communicate events between classes?

Source: https://refactoring.guru/design-patterns/observer

slide-44
SLIDE 44

Behavioural Example: Observer

Solution

Source: https://refactoring.guru/design-patterns/observer

slide-45
SLIDE 45

Design Pattern Catalog A large catalog of common design patterns exists:

https://refactoring.guru/design-patterns/catalog

slide-46
SLIDE 46

Summary

◮ Refactoring improves the inner structure of code using small incremental steps without changing the expected behaviour ◮ It should be part of every development process ◮ With experience, you develop a nose for code smells ◮ Design patterns are blueprints of solutions for common software engineering problems

slide-47
SLIDE 47

Reading

Books

◮ Refactoring: Improving the Design of Existing Code by Martin Fowler ◮ Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Ralph Johnson, John Vlissides, Richard Helm ◮ Effective Java by Joshua Bloch

Web Resources

◮ https://www.refactoring.com/ ◮ https://dzone.com/articles/what-is-refactoring ◮ https://refactoring.guru/