Advertisement
STANAANDREY

decorator pattern

Sep 12th, 2023 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. interface Car {
  2.     public void assemble();
  3. }
  4.  
  5. class BasicCar implements Car {
  6.     @Override
  7.     public void assemble() {
  8.         System.out.println("BASIC CAR!");
  9.     }
  10. }
  11.  
  12. class CarDecorator implements Car {
  13.     protected Car car;
  14.  
  15.     CarDecorator(Car car) {
  16.         this.car = car;
  17.     }
  18.  
  19.     @Override
  20.     public void assemble() {
  21.         this.car.assemble();
  22.     }
  23. }
  24.  
  25. class SportsCar extends CarDecorator {
  26.     SportsCar(Car car) {
  27.         super(car);
  28.     }
  29.     @Override
  30.     public void assemble() {
  31.         super.assemble();
  32.         System.out.println("+ SPORTS CAR FEATURES");
  33.     }
  34. }
  35.  
  36. class LuxuryCar extends CarDecorator {
  37.     LuxuryCar(Car car) {
  38.         super(car);
  39.     }
  40.  
  41.     @Override
  42.     public void assemble() {
  43.         super.assemble();
  44.         System.out.println("+ LUXURY CAR FEATURES");
  45.     }
  46. }
  47.  
  48. public class Main {
  49.  
  50.     public static void main(String[] args) throws CloneNotSupportedException {
  51.         Car car = new LuxuryCar(new SportsCar(new BasicCar()));
  52.         car.assemble();
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement