Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The Sensor interface class
- public interface Sensor{
- public void draw(); // Draws the Window
- public String getDescription(); // Returns a description of the Window
- }
- // Extension of a simple Window without any scrollbars
- class SensorPH implements Sensor {
- public void leitura() {
- }
- public String getDescription() {
- return "ph";
- }
- }
- // abstract decorator class - note that it implements Sensor
- abstract class SensorDecorator implements Sensor{
- protected Sensor sensorToBeDecorated; // the Window being decorated
- public Sensor (Sensor sensorToBeDecorated) {
- this.sensorToBeDecorated= sensorToBeDecorated;
- }
- public void leitura() {
- windowToBeDecorated.draw(); //Delegation
- }
- public String getDescription() {
- return windowToBeDecorated.getDescription(); //Delegation
- }
- }
- // The first concrete decorator which adds vertical scrollbar functionality
- class SensorNew1Decorator extends SensorDecorator{
- public SensorNew1Decorator (Window sensorToBeDecorated) {
- super(sensorToBeDecorated);
- }
- @Override
- public void leitura() {
- super.draw();
- newLeitura();
- }
- private void newLeitura() {
- //do
- }
- @Override
- public String getDescription() {
- return super.getDescription() + ", including new leitura";
- }
- }
- // The second concrete decorator which adds new sensor functionality
- class SensorNew2Decorator extends SensorDecorator{
- public SensorNew2Decorator (Window sensorToBeDecorated) {
- super(sensorToBeDecorated);
- }
- @Override
- public void leitura() {
- super.draw();
- newLeitura();
- }
- private void newLeitura() {
- //do
- }
- @Override
- public String getDescription() {
- return super.getDescription() + ", including new leitura";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement