You are on page 1of 4

AOOPS ALM 2

1.Choose any 2 Structural Design Pattern of your choice.

2. List out their similarities and differences.

3. Identify participants.

4. Identify one scenario and develop the sample code.

5. Give any real time example.

The Structural patterns selected by our group are:

M Likhitha chowdary, G Jahnavi – Decorator Design pattern

G Sriya, K Vaishnavi – Bridge Design pattern

1. Differences between Decorator and Bridge design patterns are:

Bridge will have two different type hierarchies. One for the super class abstraction
and the other for the implementation. The super class abstraction has a reference to
the implementation.
On the other hand, decorator structure will have one common type at the top. The
decorated object and decorator object will share the same super type. The
decorator will have a reference to the decorated object.
Bridge tries to decouple an abstraction from its implementation, and make both
independent to vary when needed.
Decorator where the abstraction of behaviour has a reference to the parent
abstraction, but the concrete implementation (our text view core in the example)
doesn't know about decorators.
The Decorator should match the interface of the object you're decorating. i.e. it has
the same methods, and permits interception of the arguments on the way in, and of
the result on the way out.
The Bridge has no such constraint. The client-facing interface can be different from
the underlying component providing the implementation, and thus it bridges
between the client's interface, and the actual implementation (which may not be
client-friendly, subject to change etc.)

2. Similarities between Decorator and Bridge design pattern:

The Decorator and Bridge pattern both use Composition. IMHO the difference can
be found in the way the client code uses these patterns.
The Bridge pattern injects the implementation into the abstraction. Then it invokes
methods from the Abstraction
Both patterns try to simplified complex inherit class structures, so they can look
similar in intent, but the problem they try to solve and the way they do is different.
3. Participants in Decorator Design Pattern
The participants classes in the decorator pattern are:

• Component - Interface for objects that can have responsibilities added to them dynamically.

• Concrete Component - Defines an object to which additional responsibilities can be added.

• Decorator - Maintains a reference to a component object and defines an interface that conforms
to Component's interface.

• Concrete Decorators - Concrete Decorators extend the functionality of the component by adding
state or adding behaviour.

4. Source Code
Suppose we want to implement different kinds of cars - we can create interface Car to define the
assemble method and then we can have a Basic car, further more we can extend it to Sports car and
Luxury Car.

Component Interface - The interface or abstract class defining the methods that will be
implemented. In our case Car will be the component interface.

package com.journaldev.design.decorator;

public interface Car {

public void assemble();


}
Component Implementation - The basic implementation of the component interface. We can
have BasicCar class as our component implementation.

package com.journaldev.design.decorator;

public class BasicCar implements Car {

@Override
public void assemble () {
System.out.print("Basic Car.");
}
}
Decorator - Decorator class implements the component interface and it has a HAS-A relationship
with the component interface. The component variable should be accessible to the child decorator
classes, so we will make this variable protected.

package com.journaldev.design.decorator;

public class CarDecorator implements Car {

protected Car car;


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

@Override
public void assemble () {
this.car.assemble();
}
}
Concrete Decorators - Extending the base decorator functionality and modifying the component
behavior accordingly. We can have concrete decorator classes as LuxuryCar and SportsCar.

package com.journaldev.design.decorator;

public class SportsCar extends CarDecorator {

public SportsCar(Car c) {
super(c);
}

@Override
public void assemble(){
super.assemble();
System.out.print(" Adding features of Sports Car.");
}
}

package com.journaldev.design.decorator;

public class LuxuryCar extends CarDecorator {

public LuxuryCar(Car c) {
super(c);
}

@Override
public void assemble(){
super.assemble();
System.out.print(" Adding features of Luxury Car.");
}
}

package com.journaldev.design.test;

import com.journaldev.design.decorator.BasicCar;
import com.journaldev.design.decorator.Car;
import com.journaldev.design.decorator.LuxuryCar;
import com.journaldev.design.decorator.SportsCar;

public class DecoratorPatternTest {

public static void main(String[] args) {


Car sportsCar = new SportsCar(new BasicCar());
sportsCar.assemble();
System.out.println("\n*****");

Car sportsLuxuryCar = new SportsCar(new LuxuryCar(new BasicCar()));


sportsLuxuryCar.assemble();
}

OUTPUT:

Basic Car. Adding features of Sports Car.

*****

Basic Car. Adding features of Luxury Car. Adding features of Sports


Car.

5. Real Time Examples:

Let's say you are writing an encryption module. This encryption can encrypt the clear
file using DES - Data encryption standard. Similarly, in a system you can have the
encryption as AES - Advance encryption standard. Also, you can have the
combination of encryption - First DES, then AES. Or you can have first AES, then DES.
Example where you need to build an app that calculates the price of different kinds
of burgers. You need to handle different variations of burgers, such as "large" or
"with cheese", each of which has a price relative to the basic burger.
Let's take example of PubG. Assault rifles works best with 4x zoom and while we are
on it, we would also need compensator and suppressor. It will reduce recoil and
reduce firing sound as well as echo. We will need to implement this feature where
we will allow players to buy their favourite gun and their accessories. Players can
buy the gun or some of the accessory or all of the accessory and they would be
charged accordingly.

M LIKHITHA CHOWDARY,2110030310.

SEC 3 – HONORS.

You might also like