DesignPatterns

CSE 110 Discussion - Week8 Decorator Pattern

Problem

You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class. Intent

• This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

• Client-specified embellishment of a core object by recursively wrapping it.

• Wrapping a gift, putting it in a box, and wrapping the box. Decorator Example Decorator: Question (Take 7 mins) Consider you have a car interface and a basic car class as defined below

public interface Car { public class BasicCar implements Car { public void assemble(); @Override } public void assemble() { System.out.print("Basic Car."); } }

Now based on these definitions, how would you design the following - 1) A luxury car 2) A sports car 3) A luxury car that is also a sports car Decorator : Answer

public class CarDecorator implements Car { protected Car car;

public CarDecorator(Car c){ this.car=c; }

@Override public void assemble() { this.car.assemble(); } } Decorator : Answer Contd.

public class SportsCar extends CarDecorator { public class LuxuryCar extends CarDecorator { public SportsCar(Car c) { public LuxuryCar(Car c) { super(c); super(c); } }

@Override @Override public void assemble(){ public void assemble(){ car.assemble(); car.assemble(); System.out.print(" Adding features of System.out.print(" Adding features of Sports Car."); Luxury Car."); } } } } Decorator : Answer Contd.

public class DecoratorPatternClass {

public void createCar() { Car sportsCar = new SportsCar(new BasicCar()); sportsCar.assemble();

Car sportsLuxuryCar = new SportsCar(new LuxuryCar(new BasicCar())); sportsLuxuryCar.assemble(); }

}

Consider an air traffic controller

• Many planes circle an airport.

• If they communicated with each other the skies above an airport would be a chaos.

• Accepted solution: have the planes communicate directly with an air traffic controller tower for permission to land.

• The planes do not even have to know about each other.

• This information is kept with the air traffic controller tower. Mediator Example A chat room has multiple users who can send message to chat room and it is the responsibility of chat room to show the messages to all users. We have created two classes ChatRoom and User. User objects will use ChatRoom method to share their messages. Mediator: Question (Take 5 mins) Consider an application that contains three tabs (three components, i.e. Colleague), and each of the tabs communicate with other tabs over the Mediator. Mediator: Solution MVC Pattern

MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate various application's concerns.

• Model - Model represents an object or POJO carrying data. It can also have logic to update controller if its data changes.

• View - View represents the visualization of the data that model contains.

• Controller - Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate. MVC Pattern: Structure Single Responsibility Principle

• As always, you want code independence.

• The Model should not be contaminated with control code or display code.

• The View should represent the Model as it really is, not some remembered status.

• The Controller should talk to the Model and View, not manipulate them.

• The Controller can set variables that the Model and View can read. MVC Pattern: Example MVC Pattern: Question (Take 10 mins)

We are going to create a Student object acting as a model. StudentView will be a view class which can print student details on console and StudentController is the controller class responsible to store data in Student object and update view StudentView accordingly. Show how you would implement this behavior using either UML or pseudo-code. MVC Pattern: Answer