jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 6 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
jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 18 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
jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 20 § 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. Decorator vs Inheritance
jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 30 public class HelperCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to help you. "); } } HelperCompanion
jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 31 public class AffectiveCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to cheer you."); } } AffectiveCompanion
jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 34 Office Hours Tuesday and Thursday 3 - 5 pm But an appointment required Sent me an email – [email protected]
jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2023 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.