Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER516 S20 Lecture 14

SER516 S20 Lecture 14

Software Agility
Clean Design II
(202103)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

February 18, 2019
Tweet

Transcript

  1. jgs SER 516 Software Agility Lecture 14: Clean Design II

    Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Proctoring Respondus LockDown Browser

  3. jgs 0516 0000 1110 Proctoring

  4. jgs 0516 0000 1110 Proctoring You need A camera

  5. jgs 0516 0000 1110 Proctoring

  6. jgs 0516 0000 1110 Agenda § Sample Exam is available.

    Test your system ASAP § Read the Online Proctored Exam Guidelines to know what to do in case of a problem during the exam § Midterm Exam available March 11 only during lecture time from 4:30 pm to 5:45 pm. § No lecture that day. Go directly to your exam § It is a CLOSED BOOK exam. Do not use any material § Scratch paper is allowed. Empty White Paper. Show it on camera when you show your environment!
  7. jgs Previously

  8. jgs 0516 0000 1110 Requirement Space 2 … Project 4

    O X - Space 1 ( ) < > | | - @ Save Load New Space Compile 4. Compile ?!
  9. jgs 0516 0000 1110 Design Principles § Concerns and Separation

    of concerns § Dependencies and Dependency Injection § Low Coupling
  10. jgs Kent Beck’s Rules for Simple Design

  11. jgs 0516 0000 1110 1. Write tests § Writing test

    cases drive to Single Responsibility (SRP), dependency injection (DI), and interfaces. § Writing test cases minimize coupling because tight coupling makes it difficult to write tests. § Refactoring: increase cohesion, decrease coupling, separate concerns, modularize, shrink functions and classes, choose better names, and so on. § Having tests eliminates the fear that cleaning up the code will break it.
  12. jgs 0516 0000 1110 2. Eliminate duplication § Duplication (LOC

    or methods) represents additional work, risk, and unnecessary complexity. § Use Inheritance à overloading § Common mistake: get our code working and then move on to the next problem. Take care of what you have created!
  13. jgs 0516 0000 1110 3. Ensure Expressiveness § The majority

    of the cost is in long-term maintenance. § It is critical to understand what the system does. § Select good names, keep items small, use patterns (observer, visitor, command, interpreter, mediator, decorator, adapter, singleton, factory, etc.). § Tests should also be expressive.
  14. jgs 0516 0000 1110 4. Minimize the number of items

    § Do not end with a program that have too many tiny classes and methods. Keep classes and methods counts low. § Dogmatism vs Pragmatism. § This rule is lowest priority compared with the other principles and rules. But it is a rule. Do not ignore it.
  15. jgs 0516 0000 1110 Assignment Grading Clean Code, Clean Design,

    etc. Is it Readable (guidelines, patterns, …)? Is it Understandable (short, expressiveness, …)? § Ask yourselves: Are the graders going to be able to understand this?
  16. jgs Test Yourselves

  17. jgs 0516 0000 1110 Correct?

  18. jgs 0516 0000 1110 Correct?

  19. jgs Separation of Concerns (SoC)

  20. jgs 0516 0000 1110 2. Use Patterns § Observer (behavior)

    § Delegate (behavior) § Factory (creation) § Singleton (centralize) § MVC (Model-View-Controller). No, it is not about having classes named, View, Controller, and Model
  21. jgs 0516 0000 1110 2.1. Observer § Maybe, you have

    a Console, a PlotPanel, a FacePanel, and all of them need access to the data collected from the server. § Queries vs Notifications § What about this?
  22. jgs 0516 0000 1110 2.2. Do you remember this?

  23. jgs 0516 0000 1110 2.2 Do you remember this?

  24. jgs 0516 0000 1110 2.2 Delegation public interface Behavior {

    public void makeSomething(); } } public class B1 implements Behavior { public void makeSomething() { // ... } } public class B2 implements Behavior { public void makeSomething() { // ... } } public class User { private Behavior b; public void action() { b.makeSomething(); } public User(Behavior b) { this.b = b; } }
  25. jgs 0516 0000 1110 Factory class Creator { public Product

    create() { return new Product (); } } class Client { private Iproduct product; public Client(Creator factory){ product= factory.create (); } public void run() { // ... } } public static void Main() { Creator creator = new Creator(); Client client = new Client(creator); client.run(); }
  26. jgs 0516 0000 1110 Factory § JSON string to JSON

    object § Gson and/or Guava library Gson g = new Gson(); MyClass example = g.fromJson(stringToParse, MyClass.class) § JSON-Simple library JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(stringToParse); § Jackson library MyClass object = new ObjectMapper().readValue(jsonString, MyClass.class);
  27. jgs 0516 0000 1110 Step 1 PanelLeft JMenuBar Main JMenu

    JMenuItem JFrame JPanel Workspace Icon * A B C ____Listener Graph Edge Compiler
  28. jgs Dependency Injection (DI)

  29. jgs 0516 0000 1110 Dependency Injection (DI) § A Java

    class has a dependency on another class, if it uses an instance of this class. § A class should not configure its dependencies statically (new) but should be configured from the outside (using vs having). § hard dependency – a class creates an instance of another class via the new operator § dependency injection – passing of a dependency to a dependent object (as a parameter)
  30. jgs 0516 0000 1110 Dependency Injection (DI) public class ClientFrame

    { ... public ClientFrame() { ... = new PlotPanel(); ... = new FacePanel(); ... = new ConsolePanel(); ... = new JButton(); ... = new JLabel(); } } public class ClientFrame { public ClientFrame(Jpanel a, Jpanel b) { } }
  31. Low Coupling

  32. jgs 0516 0000 1110 Definition § Low coupling. Coupling refers

    to the degree to which the different classes depend on each other. All classes should be independent as far as possible. § Create Mock objects (dummy implementations that emulate real code). § Create Interfaces
  33. jgs 0516 0000 1110 Separation of Main § Execution vs

    Construction § Design the system assuming that all objects are constructed and wired up
  34. jgs 0516 0000 1110 Reference § Clean Code, Chapter 11

    and 12.
  35. jgs SER 516 Software Agility Javier Gonzalez-Sanchez javiergs@asu.edu Spring 2021

    Disclaimer. These slides can only be used as study material for the class SER516 at ASU. They cannot be distributed or used for another purpose.