$30 off During Our Annual Pro Sale. View Details »

CSE460 Lecture 18

CSE460 Lecture 18

Software Analysis and Design
Additional Design Patterns
(202103)

Javier Gonzalez-Sanchez
PRO

July 18, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 460
    Software Analysis and Design
    Lecture 18: Additional Design Patterns
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    460 00010010
    Announcement
    § Quiz 05 is due tonight. It opens at the end of the lecture

    View Slide

  3. jgs
    460 00010010
    Singleton

    View Slide

  4. jgs
    460 00010010
    Singleton
    class Main {
    public static void main(String []a){
    // Constructor is protected -- cannot use new
    Singleton s1 = Singleton.getInstance();
    Singleton s2 = Singleton.getInstance();
    // Test for same instance
    if (s1 == s2){
    // true - Objects are the same instance
    }
    }
    }

    View Slide

  5. jgs
    460 00010010
    Singleton
    class Singleton{
    private static Singleton instance;
    // Constructor is 'protected'
    protected Singleton() {}
    public static Singleton getInstance(){
    if (instance == null){
    instance = new Singleton();
    }
    return instance;
    }
    }

    View Slide

  6. jgs
    460 00010010
    Practice

    View Slide

  7. jgs
    460 00010010
    A Singleton

    View Slide

  8. jgs
    460 00010010
    Add More Objects

    View Slide

  9. jgs
    460 00010010
    Previously …

    View Slide

  10. jgs
    460 00010010
    Next

    View Slide

  11. jgs
    460 00010010
    Chain of Responsibilities, Factory, Adapter

    View Slide

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

    View Slide

  13. jgs
    460 00010010
    Chain of Responsibility

    View Slide

  14. jgs
    460 00010010
    Chain of Responsibility vs Composite vs Decorator

    View Slide

  15. jgs
    460 00010010
    Chain of Responsibility
    public class Client {
    public static void main(String[] args) {
    // Setup Chain of Responsibility
    ConcreteHandler2 h1 = new ConcreteHandler2();
    ConcreteHandler2 h2 = new ConcreteHandler2();
    ConcreteHandler1 h3 = new ConcreteHandler1();
    h1.setSuccessor(h2);
    h2.setSuccessor(h3);
    // Generate and process request
    int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
    for (int i = 0; i < requests.length; i++) {
    h1.HandleRequest(requests[i]);
    }
    }
    }

    View Slide

  16. jgs
    460 00010010
    Handler
    abstract class Handler {
    public abstract void HandleRequest(int request);
    }

    View Slide

  17. jgs
    460 00010010
    ConcreteHandler1
    class ConcreteHandler1 extends Handler {
    public void HandleRequest(int request) {
    System.out.println("I am the last in line");
    }
    }

    View Slide

  18. jgs
    460 00010010
    ConcreteHandler2
    class ConcreteHandler2 extends Handler {
    protected Handler successor;
    public void setSuccessor(Handler successor) {
    this.successor = successor;
    }
    public void HandleRequest(int request) {
    if (request >= 10 && request < 20) {
    // code here
    System.out.println("I am the handling it");
    } else if (successor != null) {
    System.out.println("Passing it...");
    successor.HandleRequest(request);
    }
    }
    }

    View Slide

  19. jgs
    Factory
    Wrapper of a constructor
    One entire object to be built in a single method call

    View Slide

  20. jgs
    460 00010010
    Next

    View Slide

  21. jgs
    460 00010010
    Abstract Factory

    View Slide

  22. jgs
    460 00010010
    Products
    public interface Shape {
    void draw();
    }
    public class RoundedRectangle implements Shape {
    public void draw() {
    System.out.println("Inside RoundedRectangle/draw() method.");
    }
    }
    public class RoundedSquare implements Shape {
    public void draw() {
    System.out.println("Inside RoundedSquare/draw() method.");
    }
    }
    public class Rectangle implements Shape {
    public void draw() {
    System.out.println("Inside Rectangle/draw() method.");
    }
    }

    View Slide

  23. jgs
    460 00010010
    Factories
    public abstract class AbstractFactory {
    Shape getShape(String shapeType) {}
    }
    public class ShapeFactory extends AbstractFactory {
    public Shape getShape(String shapeType){
    if(shapeType.equalsIgnoreCase("RECTANGLE")){
    return new Rectangle();
    }else if(shapeType.equalsIgnoreCase("SQUARE")){
    return new Square();
    }
    return null;
    }
    }

    View Slide

  24. jgs
    460 00010010
    Factories
    public class RoundedShapeFactory extends AbstractFactory {
    public Shape getShape(String shapeType){
    if(shapeType.equalsIgnoreCase("RECTANGLE")){
    return new RoundedRectangle();
    }else if(shapeType.equalsIgnoreCase("SQUARE")){
    return new RoundedSquare();
    }
    return null;
    }
    }

    View Slide

  25. jgs
    460 00010010
    Client
    public class Client {
    public static void main(String[] args) {
    AbstractFactory shapeFactory = new ShapeFactory();
    //get an object of Shape Rectangle
    Shape shape1 = shapeFactory.getShape("RECTANGLE");
    //call draw method of Shape Rectangle
    shape1.draw();
    //get an object of Shape Square
    Shape shape2 = shapeFactory.getShape("SQUARE");
    //call draw method of Shape Square
    shape2.draw(); ]
    //get shape factory
    AbstractFactory shapeFactory1 = new RoundedShapeFactory();
    //get an object of Shape Rectangle
    Shape shape3 = shapeFactory1.getShape("RECTANGLE");
    //call draw method of Shape Rectangle
    shape3.draw();
    //get an object of Shape Square
    Shape shape4 = shapeFactory1.getShape("SQUARE");
    //call draw method of Shape Square
    shape4.draw();
    }
    }

    View Slide

  26. jgs
    CSE 460 Software Analysis and Design
    Javier Gonzalez-Sanchez
    [email protected]
    Fall 2020
    Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.

    View Slide