Question: Steps: 1 . Create an interface called Widget. This is the highest level common interface shared by all classes. Inside Widget interface, define a method
Steps:
Create an interface called Widget. This is the highest level common interface shared by all classes. Inside Widget interface, define a method void draw
Create a core class called TextField that implements Widget interface. Add width and height as the two instance variables. Provide constructor in TextField, and implement the draw method as printing out a sentence showing this is TextField with the values of width and height.
Create an abstract Decorator class called Decorator and make it implements Widget interface as well. Inside Decorator class, the most important step is to create an instance variable Widget widget. This is an aggregation relationship. Provide constructor for Decorator class. Implement draw method in Decorator. This should be just simply calling widget.draw
Then, we need to add some additional features. Create a class called BorderDecorator as the subclass of Decorator. In the draw method of BorderDecorator, we should first call super.draw because this is how it delegates to the base core class. Then, write System.out.println BorderDecorator; to indicate this is an additional feature created in addition to the base core class.
Similarly, create another decorator lass called ScrollDecorator. Implement its draw method in the similar way, but modify the additional feature to print out ScrollDecorator
Finally, create a client class to show how they work. You can create objects with different features of TextField in this way:
Widget widget new TextField; basic one
widget.draw;
Widget widget new ScrollDecoratornew TextField; add scroll bar
Widgetdraw;
Widget widget new BorderDecoratornew BorderDecoratornew ScrollDecoratornew TextField; add some borders
Widgetdraw;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
