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

CSC308B Lecture 14

CSC308B Lecture 14

Software Engineering I
More Patterns
(202401) - two-days per week version

Javier Gonzalez-Sanchez

March 04, 2024
Tweet

Transcript

  1. jgs CSC 308 Software Engineering 1 Lecture 14: More Patterns

    Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227
  2. jgs Chain of Responsibility Avoid coupling the sender of a

    request to its receiver by giving more than one object a chance to handle the request.
  3. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    8 Chain of Responsibility // Setup Chain of Responsibility Handler h1 = new ConcreteHandler2(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler1(); h1.SetSuccessor(h2); h2.SetSuccessor(h3); // Generate and process request int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; foreach (int i=0; i<requests.lenght; i++) { h1.HandleRequest(request[i]); }
  4. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    9 Handler abstract class Handler { public abstract void HandleRequest(int request); } class ConcreteHandler1 extends Handler { public override void HandleRequest(int request) { if (request >= 0 && request < 10) { // code here… } else { // nothing can be done :( } } }
  5. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    10 Handler class ConcreteHandler2 extends Handler { protected Handler successor; public void setSuccessor(Handler successor) { this.successor = successor; } public override void HandleRequest(int request) { if (request >= 10 && request < 20) { // code here } else if (successor != null) { successor.HandleRequest(request); } } }
  6. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    11 Chain of Responsibility vs Composite vs Decorator
  7. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    13 Fluent-Builder § The Fluent Builder is a builder whose design relies on method chaining. In doing so, it aims to promote code legibility.
  8. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    14 Fluent-Builder import java.awt.*; public class Client { public static void main(String[] args) { FluentBuilder builder = new BoxFluentBuilder() .setColor(Color.RED) .setSize(20) .setX(10) .setY(20); Product box = ((BoxFluentBuilder)builder).get(); } }
  9. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    15 Fluent-Builder import java.awt.Color; public interface FluentBuilder { public Builder setColor(Color color); public Builder setSize(int size); public Builder setX(int x); public Builder setY(int y); }
  10. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    16 Fluent-Builder import java.awt.*; public class BoxFluentBuilder implements FluentBuilder { private Color color; private int size, x, y; public Builder setColor (Color color) { this.color = color; return this; } public Builder setSize(int size) { this.size = size; return this; } public Builder setX(int x) { this.x = x; return this; } public Builder setY(int y) { this.y = y; return this; } public Box get() { return new Box (color, size, x, y); } }
  11. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    22 Assignment 03 - UI Main JFrame ActionListener PlotPanel JButton Run
  12. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    23 Assignment 03 – Observer Pattern Main Observable PlotPanel Run Source Observer
  13. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    24 Assignment 03 – Decorator Pattern Main PlotPanel Observer Worker WorkerStandard Tool ToolLine ToolSquare
  14. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    25 Assignment 03 – Decorator Pattern ToolBar ToolSquare WorkerStandard ToolSquare WorkerStandar WorkerStandar
  15. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    26 Assignment 03 – Singleton Pattern PlotPanel Run << Singleton >> Configurator
  16. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    27 Source // This will became your Singleton
  17. jgs

  18. jgs CSC 308 Software Engineering 1 Lab 14: Plotter Dr.

    Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment
  19. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    43 Let’s Work Can you Make it Work?
  20. jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected]

    Winter 2024 Copyright. These slides can only be used as study material for the class CSC 308 at Cal Poly. They cannot be distributed or used for another purpose.