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

CSE564 Lecture 17

CSE564 Lecture 17

Software Design
Design Patterns
(202103)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 17, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 17: Design Patterns: Extra

    Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs 564 00010001 Homework § Read An Introduction to Software

    Architecture by Garlan and Shaw Carnegie Mellon, 1994 Available on Canvas
  3. jgs 564 00010001 GoF Patterns

  4. jgs Factory Wrapper of a constructor One entire object to

    be built in a single method call
  5. jgs 564 00010001 Abstract Factory

  6. jgs 564 00010001 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."); } }
  7. jgs 564 00010001 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; } }
  8. jgs 564 00010001 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; } }
  9. jgs 564 00010001 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(); } }
  10. jgs Adapter Convert the interface of a class into another

    interface clients expect
  11. jgs 564 00010001 Adapter

  12. jgs 564 00010001 Example RectangleAdapter + draw Rectangle + draw

    + draw
  13. jgs 564 00010001 Client public class Client { public static

    void main(String[] args) { Shape shape = new RectangleAdapter(new Rectangle(); int x1 = 10, y1 = 20; int x2 = 30, y2 = 60; shape.draw(x1, y1, x2, y2); } }
  14. jgs 564 00010001 Adapter class RectangleAdapter implements Shape { private

    Rectangle adaptee; public RectangleAdapter(Rectangle rectangle) { this.adaptee = rectangle; } public void draw(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x2 - x1); int height = Math.abs(y2 - y1); adaptee.draw(x, y, width, height); } }
  15. jgs 564 00010001 Adaptee class Rectangle { public void draw(int

    x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point”); } }
  16. jgs Composite Compose objects into tree structures to represent part-whole

    hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
  17. jgs 564 00010001 Composite

  18. jgs 564 00010001 Composite vs Decorator § The difference is

    more one of purpose than implementation
  19. jgs 564 00010001 Create a Tree Structure Composite root =

    new Composite("root"); root.Add(new Leaf("Leaf A")); root.Add(new Leaf("Leaf B")); Composite comp = new Composite("Composite X"); comp.Add(new Leaf("Leaf XA")); comp.Add(new Leaf("Leaf XB")); root.Add(comp); root.Add(new Leaf("Leaf C")); // Add and remove a leaf Leaf leaf = new Leaf("Leaf D"); root.Add(leaf); root.Remove(leaf); // Recursively display tree root.Display(1);
  20. jgs Bridge Decouple an abstraction from its implementation so that

    the two can vary independently.
  21. jgs 564 00010001 Bridge

  22. jgs 564 00010001 Bridge Abstraction ab = new RefinedAbstraction(); //

    Set implementation and call ab.setImplementor (new ConcreteImplementorA() ); ab.Operation(); // Change implemention and call ab.setImplementor (new ConcreteImplementorB() ); ab.Operation();
  23. jgs 564 00010001 Questions

  24. jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez javiergs@asu.edu Fall

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