Description
1) Implement the below Diagram for decorator pattern. Test using code given below.
Interface
ICar
assemble() : void
Class
BasicCar
BasicCar() assemble() : void
Class
CarDecorator
CarDecorator(Car) assemble() : void
Class
LuxuryCar
LuxuryCar(Car) assemble() : void
Class
SportsCar
SportsCar(Car) assemble() : void
public class DecoratorPatternTest { |
Car sportsCar = new SportsCar(new BasicCar()); sportsCar.assemble(); |
System.out.println(); |
sportsLuxuryCar.assemble(); } |
} o/p: |
Basic Car. Adding features of Luxury Car. Adding features of Sports Car. |
2) Implement the below Diagram for Façade design pattern. Test using code given below.
Interface
Shape
draw() : void
Class
ShapeMaker
circle : Shape rectangle: Shape square : Shape
ShapeMaker() drawCircle() : void drawRectangle() : void drawSquare() : void
Class
Circle
draw() : void
Class
Rectangle
draw() : void
Class
Square
draw() : void
public class FacadePatternDemo { |
public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); |
shapeMaker.drawSquare(); } |
} O/P: |
Drawing a Circle |
Drawing a Rectangle Drawing a Square |
3) Implement the below Diagram for Strategy pattern. Test using code given below.
Class
ShoppingCart
ShoppingCart() addItem(Item) : void removeItem(Item) : void
calculateTotal() : int pay(PaymentStrategy) : void
Interface
PaymentStrategy
pay(int) : void
Class
CreditCard
cardNumber : String
CreditCard(String) pay(int) : void
Class
PayPal
email : String
PayPal(String) pay(int) : void
Class
Item
Id : String price : int
Item(String, int) getId() : String getPrice() : int
public class StrategyPatternDemo { ShoppingCart cart = new ShoppingCart(); |
Item item1 = new Item(“1234”,10); Item item2 = new Item(“5678”,40); |
cart.addItem(item1); cart.addItem(item2); |
cart.pay(new PaypalStrategy(“[email protected]”)); cart.pay(new CreditCardStrategy( “1234567890123456”)); |
} } o/p: |
Paypal : $50 CreditCard : $50 |