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

CSE360 Lecture 19

CSE360 Lecture 19

Introduction to Software Engineering
Software Design Patterns: Singleton
(201806)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE360 Introduction to Software Engineering Lecture 19: Software Design Patterns:

    Singleton and Decorator Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 2 GoF

    Patterns Creational Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton Structural Patterns • Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor
  3. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 5 Singleton

    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 } } }
  4. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 6 Singleton

    class Singleton{ private static Singleton _instance; // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } }
  5. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 7 Scenario

    Quiz Tutor System Companion ControlCenter << singleton>>
  6. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 9 GoF

    Patterns Creational Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton Structural Patterns • Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor
  7. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 11 Main

    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(); } }
  8. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 12 Main

    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(); } } ConcreteComponent - Operation() ConcreteDecoratorA - Operation() ConcreteDecoratorB - Operation()
  9. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 13 Component

    abstract class Component { public abstract void operation(); }
  10. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 14 ConcreteComponent

    class ConcreteComponent extends Component { @override public void Operation() { System.out.print("ConcreteComponent-Operation()"); } }
  11. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 15 Decorator

    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(); } } }
  12. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 16 ConcreteDecoratorA

    class ConcreteDecoratorA extends Decorator { @override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA-Operation()”); } }
  13. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 17 ConcreteDecoratorB

    class ConcreteDecoratorB extends Decorator { @override public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } }
  14. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 18 Decorator

    vs Inheritance • Both allow you to change how an object behaves • The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime. • Inheritance adds behavior at compilation-time.
  15. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 19 Scenario

    CompanionHelper CompanionTroll Companion
  16. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 20 Scenario

    CompanionHelper CompanionTroll Companion CompanionTrollHelper
  17. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 21 Scenario

    CompanionHelper CompanionTroll Companion CompanionTrollHelper
  18. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 22 Decorator

    WhiteBody body = new WhiteBody(); Mask mask = new Mask(); Helmet helmet = new Helmet(); // Link decorators mask.SetComponent(body); helmet.SetComponent(mask); helmet.Operation();
  19. CSE360 – Introduction to Software Engineering Javier G onzalez-Sanchez javiergs@

    asu.edu Sum m er 2017 Disclaim er. These slides can only be used as study m aterial for the class C SE360 at ASU. They cannot be distributed or used for another purpose.