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

CSC308B Lecture 12

CSC308B Lecture 12

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

Javier Gonzalez-Sanchez

March 02, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSC 308 Software Engineering 1 Lecture 12: Patterns II

    Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227
  2. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    3 Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Teacher Student Main
  3. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    5 Important Depreciated java.util.Observer java.util.Observable The implementation of these packages That is NOT about the Observer pattern being depreciated.
  4. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    6 Important Use java.beans.PropertyChangeListener java.beans.PropertyChangeSupport
  5. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    15 class MainApp { 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 } } } Singleton
  6. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    16 class Singleton{ private static Singleton _instance; // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } } Singleton
  7. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    22 abstract class Component { public abstract void operation(); } Component
  8. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    23 class ConcreteComponent extends Component { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } } ConcreteComponent
  9. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    24 abstract class Decorator extends Component { protected Component component; public void setComponent(Component component) { this.component = component; } @override public void operation() { if (component != null) { component.operation(); } } } Decorator
  10. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    25 class ConcreteDecoratorA extends Decorator { @override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA-Operation()”); } } ConcreteDecoratorA
  11. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    26 class ConcreteDecoratorB extends Decorator { @override public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } } ConcreteDecoratorB
  12. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    27 class MainApp { static void main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } } Main
  13. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    28 class MainApp { static void main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } } Main ConcreteComponent - Operation() ConcreteDecoratorA - Operation() ConcreteDecoratorB - Operation()
  14. jgs

  15. jgs CSC 308 Software Engineering 1 Lab 11: Project Sprint

    2 (Continue) Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment
  16. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    33 Scenario (1) Repository is a Singleton It stores the information for Squares and circles objects (2) A CircleBuilder is a Runnable Creating circles 1 per second At random places (3) A BoxBuilder is a Runnable Creating boxes 1 per second At random places (4) A Screen is a Panel Drawing the circles and boxes observed in the Whiteboard (5) And we need a Main that assembly everything and provide a Frame
  17. 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.