How decorator pattern works

Decorator is a structural pattern that adds additional responsibility to the object at run time. This pattern adds responsibility to a particular object not on the class. As a result different object of same type can have different responsibilities.

I would explain a real life example from my experience. I am a biker. Every morning I go to office riding on my bike. When I bought this bike the seller just sold the standard bike to me. They didn't provide any tail light, head light, mud guard and even the kickstand with the bike. Later I added or decorated my bike by adding a tail light, mud guard and with a kickstand. So, functionality of each of this component is different. I added them with my bike and they are responsible for additional functionalities. This can be thought of as a decorator pattern example. Here first I have the bike with standard installation. Then I decorated it with other component.

Participants in a decorator pattern are
  • Component interface
  • a Concrete implementation of Component
  • An abstract decorator
  • One or more concrete decorator that will extend abstract decorator
Basic class diagram is as follows. Later I will add the bike example decorator



Figure 1: Decorator pattern
Here ConcreteDecorator is the standard component that has to be decorated. In my example it is my bike. Decorator abstract class contains a reference of component. Each of the concrete decorator adds additional responsibility to the standard object. Now lets dive to the bike example. Participants for this example are

Bike as the Component
StandardBike as the ConcreteComponent
BikeDecorator as the abstract Decorator
TailLightDecorator as the ConcreteDecorator

So, the diagram for bike example is as follows
Figure 2: Diagram of bike decorator


The code for each of the component are given below

Bike interface is as follows

public interface Bike {
  public String ride();
}

public class StandardBike implements Bike{
 @Override
 public String ride(){
  return " This is a standard bike";
 }
}

public abstract class BikeDecorator implements Bike{

 protected Bike bike;
 
 public BikeDecorator(Bike pBike){
  this.bike = pBike;
 }
 
 @Override
 public String ride(){
  return bike.ride();
 }
}

public class TailLightDecorator extends BikeDecorator {

 public TailLightDecorator(Bike pBike) {
  super(pBike);
 }

 @Override
 public String ride() {
  return bike.ride() + turnOnLight();
 }

 public String turnOnLight() {
  return " + tail light";
 }
}

public class MudGuardDecorator extends BikeDecorator {

 public MudGuardDecorator(Bike pBike) {
  super(pBike);
 }

 @Override
 public String ride() {
  return bike.ride() + preventMud();
 }

 public String preventMud() {
  return "+ mud guard";
 }
}

public class Client {

 public static void main(String [] args){
  MudGuardDecorator md = new MudGuardDecorator(new TailLightDecorator(new StandardBike()));
  System.out.println(md.ride());
 }
 
}

Comments

Unknown said…
đồng tâm
game mu
cho thuê nhà trọ
cho thuê phòng trọ
nhac san cuc manh
số điện thoại tư vấn pháp luật miễn phí
văn phòng luật
tổng đài tư vấn pháp luật
dịch vụ thành lập công ty trọn gói

Con cá này, ước chừng có nặng trên dưới một trăm cân?

Ngay sau đó, cá lớn qua ở mặt nước, ra một đạo sông gợn, tiếp tục lôi kéo thuyền nhỏ trôi giạt đông tây.

“Oa! Quá đẹp trai!" kim thiếu nữ giống như mê trai hô lên, ở trên thuyền vui đến nhảy chân, thặng vỗ tay, ánh mặt rất mong đợi phán: “Dùng cá lớn kéo thuyên oa... oa..."

Càng ngày càng nhiều người phát hiện.

Nhất là một ít nữ tử lại hai mắt tỏa sáng nhìn tên điệu bộ này, Vừa là hâm mộ vừa là hướng về. Tiêu sái bực này, cũng thật không phải người bình thường có thể làm ra.

Thậm chí... loại trình độ phong cách này, so với Tiêu Tuyệt vừa rồi, không kém cỏi chút nào!

Sở ngự tòa mặt không chút thay đổi, dùng một loại sắc mặt cao ngạo, rất là cao thượng xuất trần ở khe hở giữa thuyền lớn thuyền nhỏ chui tới chui lui, thần sắc trên mặt không chút thay đôi....

Đây là một loại cảnh sắc khác.

Phiêu dạt như thể tiên sái như thế, muốn nổi bật như thế...

Con mắt mọi người đều nhìn thẳng rồi. Vừa rồi Tiêu Tuyệt dẫn lên oanh động trong phút chôc trở về vô hình.

Popular posts from this blog

Oracle Export, Import using sqlplus

Custom Request Processor in Struts

Preventing Duplicate Form Submission in Struts using TOKEN