I have this program that uses Builder Pattern to build an ID Object. I am not sure if the implementation of Builder Pattern is correct. But, this program can create an ID Object. Once the Object is created, I would like to implement Decorator Pattern to add functionalities to my object.
Here is my ID class,
public class ID implements IID{
int width;
int height;
String name;
String idnumber;
boolean border = false;
String borderColor;
public int getWidth(){return this.width;}
public int getHeight(){return this.height;}
public String getName(){return this.name;}
public String getIDNumber(){return this.idnumber;}
public boolean isBordered(){return border;}
public String getBorderColor(){return borderColor;}
@Override
public void addBorder(){
this.border = true;
}
}
The boolean border, String borderColor, isBordered(), getBorderColor() and addBorder() is for Decorator Pattern.
Here is the Interface ID,
public interface IID {
void addBorder();
}
Here is the Decorator,
public abstract class IDDecorator implements IID{
protected ID decoratedID;
public IDDecorator(ID decoratedID){
this.decoratedID = decoratedID;
}
public void addBorder(){
decoratedID.addBorder();
}
}
Here is the Concrete Decorator,
public class GrayIDDecorator extends IDDecorator{
public GrayIDDecorator(ID decoratedID){
super(decoratedID);
}
@Override
public void addBorder(){
decoratedID.addBorder();
setGrayBorder(decoratedID);
}
private void setGrayBorder(ID decoratedID){
decoratedID.borderColor="Gray";
}
}
I also like to know what is the use of super() in this concrete decorator. What is super()?
Here is the main class,
...
public static void main(String[] args) {
...
IDBuilderDirector idd = new IDBuilderDirector();
...
ID id = idd.construct(roleD, idnumber, fname, lname);
...
IID grayID = new GrayIDDecorator(id);
id.addBorder();
if(id.isBordered()){
System.out.println("Border: "+id.getBorderColor());
}else{
System.out.println("Border: None");
}
}
The IDBuilderDirector and related methods is for Builder Pattern.
In the main class, I used GrayIDDecorator. After the execution of the program, I have now the ID with border. But why I can't change the value of borderColor of the ID? If I call id.getBorderColor(), it will return null.
Also, I would like to know if implementation of Decorator pattern here in my program is correct.
This is the link that I am following regarding to Decorator Pattern. http://ift.tt/1CXb1Gu
Aucun commentaire:
Enregistrer un commentaire