inf1 op
play

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


  1. Inf1-OP Refactoring and Design Patterns Volker Seeker School of Informatics March 8, 2019

  2. Refactoring

  3. Hockey Analysis Season Game -minutesPlayed:double -games:List<Game> -goalsAgainst:int -shotsOnGoalAgainst:int +getGoalsAgainst():int +addGame(Game):void +getShotsOnGoalAgainst():int +removeGame(Game):void +getMinutesPlayed():double +getGames():List<Game> +getGoalieStatistics():GoalieStatistics GoalieStatistics ? Implement a class to calculate goalie statistics from all games in a season.

  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).

  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). � � GA GAA = 60 timeplayed mins

  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). � � GA GAA = 60 timeplayed mins ◮ 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.

  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). � � GA GAA = 60 timeplayed mins ◮ 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

  8. Hockey Analysis Game Season -games:List<Game> -minutesPlayed:double -goalsAgainst:int -shotsOnGoalAgainst:int +getGoalsAgainst():int +addGame(Game):void +getShotsOnGoalAgainst():int +removeGame(Game):void +getGames():List<Game> +getMinutesPlayed():double +getGoalieStatistics():GoalieStatistics GoalieStatistics ? Let’s make a rough plan first!

  9. Hockey Analysis Game Season -games:List<Game> -minutesPlayed:double -goalsAgainst:int -shotsOnGoalAgainst:int +getGoalsAgainst():int +addGame(Game):void +getShotsOnGoalAgainst():int +removeGame(Game):void +getGames():List<Game> +getMinutesPlayed():double +getGoalieStatistics():GoalieStatistics GoalieStatistics -season:Season +getGoalsAgainstAverage:double +getSavePercentage():double Let’s make a rough plan first!

  10. Test Driven Development A set of unit tests is provided to check the expected behaviour of the code. 1. implement test to check expected behaviour 2. implement feature to make test pass

  11. Hockey Analysis Game Season -games:List<Game> -minutesPlayed:double -goalsAgainst:int -shotsOnGoalAgainst:int +getGoalsAgainst():int +addGame(Game):void +getShotsOnGoalAgainst():int +removeGame(Game):void +getGames():List<Game> +getMinutesPlayed():double +getGoalieStatistics():GoalieStatistics GoalieStatistics -season:Season +getGoalsAgainstAverage:double +getSavePercentage():double We use test driven development.

  12. Hockey Analysis Game Season -games:List<Game> -minutesPlayed:double -goalsAgainst:int -shotsOnGoalAgainst:int +getGoalsAgainst():int +addGame(Game):void +getShotsOnGoalAgainst():int +removeGame(Game):void +getGames():List<Game> +getMinutesPlayed():double +getGoalieStatistics():GoalieStatistics GoalieStatistics GoalieStatisticsT est -season:Season +getGoalsAgainstAverage:double +getSavePercentage():double We use test driven development.

  13. Demo

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

  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.

  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

  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

  18. Refactoring Techniques A large catalog of standard refactoring techniques exists, for example: ◮ loop splitting ◮ method extraction ◮ renaming ◮ . . . https://www.refactoring.com/catalog/

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

  20. Code Smell A large list of common code smells exist, for example: ◮ Bloaters ◮ Object-Orientation Abusers ◮ Change Preventers ◮ . . . https://refactoring.guru/refactoring/smells

  21. Design Patterns

  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

  23. Towards Software Engineering First learn your basic tools and material. Then build large houses ... Source: http://hannesdorfmann.com/images/legohouse/legohouse.jpg

  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

  25. Design Patterns Software Design Patterns are blueprints of solutions for common software design problems. Source: https://www.rff.com/cloverleaf.png

  26. Classification ◮ Creational Patterns ◮ Structural Patterns ◮ Behavioural Patterns

  27. Creational Example: Singleton Problem ◮ access a resource in your program

  28. Creational Example: Singleton Problem ◮ access a resource in your program → database resource

  29. Creational Example: Singleton Problem ◮ access a resource in your program → database resource ◮ initialising resource access is expensive

  30. Creational Example: Singleton Problem ◮ access a resource in your program → database resource ◮ initialising resource access is expensive → only one instance

  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

  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

  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) { ... }

  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!

  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.

  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.

  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.

  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.

  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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend