Problem 3: Interfaces

1. Abstract functions

An abstract function is a mathematical entity that takes an input, performs some calculation and produces an output. Such a definition is called a lambda. In OOP, we can model abstract functions as an interface, called ILambda, that defines a method apply that takes an Object as input and returns an Object as output.
  1. Define the ILambda interface.
  2. Define two concrete lambda functions, called Lambda1 and Lambda2, for which the apply method should return: 
    - a String that concatenates “Lambda1 applied to ” with the textual description of the Object received as parameter, in the case of Lambda1;
    - a String that concatenates “Lambda2 applied to ” with the textual description of the Object received as parameter, in the case of Lambda2.
  3. Consider another interface, called ILogical, which represents objects with the ability to apply one of two possible lambdas. In OOP the ILogical interface defines a method select with three parameters: two ILambda’s and an Object. The select method calls either the 1st or the 2nd apply method with the given Object, and returns the Object returned in the call to the apply method. Define the ILogical interface.
  4. Define two concrete classes, called True and False, which implements ILogical and satisfy the following specification. When 
    - b.select(new Lambda1(), new Lambda2(), “hello”); 
    is executed with a variable b of type ILogical, the return value will be 
    - “Lambda1 applied to hello”, when b is of type True, and 
    - “Lambda2 applied to hello”, when b is of type False.
    Your code for True and False may not contain any String literals.
  5. Exemplify the call of select over a True object in a main method within a Main class.

2. The command pattern

The command pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests and support undoable operations.

Consider a programmable remote control (PRC) with five buttons. Of the five buttons, only four are programmable. The fifth button does a global undo operation. The goal is to program the first four buttons of the PRC with turn on/off the light (2 buttons) and open/close garage door (2 buttons). The fifth button of the PRC should reverse the last operation performed. Assume that the position of the fifth button of the PRC is differentiated from the others given that it is not programmable.
  1. Consider an interface ICommand that offers 2 methods, execute and revert. The no-arg methods should return void. Define the ICommand interface.
  2. Consider a class Light with two methods, turnon and turnoff, both no-arg methods that return void; Light has a no-arg constructor. Provide two implementations of ICommand, named TurnOnLight and TurnOffLight.
  3. Consider the class GarageDoor with two methods, open and close, both no-arg methods that return void; GarageDoor has a no-arg constructor. Provide two implementation of ICommand, named OpenGarageDoor and CloseGarageDoor.     
  4. Consider a class RemoteControl which implements a PRC with 5 buttons. It should offer 3 methods: 
    - setCommand, which receives an integer that identifies the position of the button and the respective command; it must record the respective command in the desired position;
    - buttonPushed, which receives the position of the button and performs the action associated with that button;
    - undo, which should perform the overall undo operation.
  5. Exemplify the use of a RemoteControl in a main method within a Main class by associating turning the light on and off and opening and closing the garage door to the buttons available on the PRC. To this end, the following sequence of actions should be done:
        - open the garage door;
        - close the garage door;
        - turn on the light;
        - do undo (to turn off the light).

Attachments