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)

Javier Gonzalez-Sanchez
PRO

February 18, 2019
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    SER 516
    Software Agility
    Lecture 14: Clean Design II
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Proctoring
    Respondus LockDown Browser

    View Slide

  3. jgs
    0516 0000 1110
    Proctoring

    View Slide

  4. jgs
    0516 0000 1110
    Proctoring
    You need
    A camera

    View Slide

  5. jgs
    0516 0000 1110
    Proctoring

    View Slide

  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!

    View Slide

  7. jgs
    Previously

    View Slide

  8. jgs
    0516 0000 1110
    Requirement
    Space 2 …
    Project 4 O
    X
    -
    Space 1
    (
    )
    <
    >
    | |
    -
    @
    Save
    Load
    New Space Compile
    4.
    Compile ?!

    View Slide

  9. jgs
    0516 0000 1110
    Design Principles
    § Concerns and Separation of concerns
    § Dependencies and Dependency Injection
    § Low Coupling

    View Slide

  10. jgs
    Kent Beck’s Rules for Simple Design

    View Slide

  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.

    View Slide

  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!

    View Slide

  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.

    View Slide

  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.

    View Slide

  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?

    View Slide

  16. jgs
    Test Yourselves

    View Slide

  17. jgs
    0516 0000 1110
    Correct?

    View Slide

  18. jgs
    0516 0000 1110
    Correct?

    View Slide

  19. jgs
    Separation of Concerns (SoC)

    View Slide

  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

    View Slide

  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?

    View Slide

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

    View Slide

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

    View Slide

  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;
    }
    }

    View Slide

  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();
    }

    View Slide

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

    View Slide

  27. jgs
    0516 0000 1110
    Step 1
    PanelLeft
    JMenuBar Main
    JMenu
    JMenuItem JFrame
    JPanel
    Workspace
    Icon
    *
    A
    B
    C
    ____Listener Graph
    Edge
    Compiler

    View Slide

  28. jgs
    Dependency Injection (DI)

    View Slide

  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)

    View Slide

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

    View Slide

  31. Low Coupling

    View Slide

  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

    View Slide

  33. jgs
    0516 0000 1110
    Separation of Main
    § Execution vs Construction
    § Design the system assuming that all objects are constructed and wired up

    View Slide

  34. jgs
    0516 0000 1110
    Reference
    § Clean Code, Chapter 11 and 12.

    View Slide

  35. jgs
    SER 516 Software Agility
    Javier Gonzalez-Sanchez
    [email protected]
    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.

    View Slide