Decorator design pattern

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

[Wikipedia]

Intro

In this example we want to create a Shopping cart And set it as a empty shopping cart. After the shopping cart is empty , we want to getCost Shirts, pants and coats while the price and the product string of the shopping cart are updated.

ShoppingCart

public interface ShoppingCart {
    public String getProducts();
    public int getCost();
}

ShoppingCartDecorator


  public class ShoppingCartDecorator implements ShoppingCart {
    /*
    The decorator class,Implements the interface and has a "Has a" relationship with it.
     */

    //the object is protected ,because we want to grant access to it's children.
    protected ShoppingCart shoppingCart;

    public ShoppingCartDecorator(ShoppingCart shoppingCart) {
        this.shoppingCart = shoppingCart;
    }
    @Override
    public String getProducts() {
        return shoppingCart.getProducts();
    }
    @Override
    public int getCost() {
        return shoppingCart.getCost();
    }
}

EmptyShoppingCart


  public class EmptyShoppingCart implements ShoppingCart {
    /*
    Implementation of the empty shopping cart. every shopping cart will start as empty shopping cart .
     */
    public EmptyShoppingCart() {
        System.out.println("Empty shopping cart is for free");
    }
    @Override
    public String getProducts() {
        return "";
    }
    @Override
    public int getCost() {
        return 0;
    }
}

Coats


  public class Coats extends ShoppingCartDecorator {
    /*
      Extends the decorator and modifying the prices and the products accordingly.
     */
    public Coats(ShoppingCart shoppingCart) {
        super(shoppingCart);
        System.out.println("Added Coat to the cart, the price is: 30");
    }
    @Override
    public String getProducts() {
            return super.getProducts()+"Coat, ";
    }
    @Override
    public int getCost() {
        return super.getCost()+30;
    }
}

Pants


  public class Pants extends ShoppingCartDecorator {
         /*
          Extends the decorator and modifying the prices and the products accordingly.
         */
    public Pants(ShoppingCart shoppingCart) {
        super(shoppingCart);
        System.out.println("Added Pants to the cart, the price is 60");
    }
    @Override
    public String getProducts() {
        return super.getProducts()+"Pants";
    }
    @Override
    public int getCost() {
        return super.getCost()+60;    }
}

Shirts


  public class Shirts extends ShoppingCartDecorator {
     /*
       Extends the decorator and modifying the prices and the products accordingly.
     */
    public Shirts(ShoppingCart shoppingCart) {
        super(shoppingCart);
        System.out.println("Added Shirt to the cart, the price is: 20");
    }
    @Override
    public String getProducts() {
        return super.getProducts()+"Shirt, ";
    }
    @Override
    public int getCost() {
        return super.getCost()+20;
    }
}

Main

   
  public class Main {
    public static void main(String[] args){
        /*
        We want to create a Shopping cart And set it as a empty shopping cart.
        After the shopping cart is empty , we want to getCost Shirts, pants and coats while the
        price and the product string of the shopping cart are updated.
         */
        ShoppingCart shoppingCart=new Pants(new Coats(new Shirts(new EmptyShoppingCart())));
        System.out.println();
        System.out.println("Items in the shopping cart:"+shoppingCart.getProducts());
        System.out.println("The price of the shopping cart is: "+shoppingCart.getCost());
    }
}

Running Example

Empty shopping cart is for free
Added Shirt to the cart, the price is: 20
Added Coat to the cart, the price is: 30
Added Pants to the cart, the price is 60

Items in the shopping cart:Shirt, Coat, Pants
The price of the shopping cart is: 110