8 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
16 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
46 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
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.