Design mode Decorator - home installation
Recently, I have been engaged in renovation, I have made a set of furniture, I need to brush the paint, so I will go to the market to find a paint master and paint apprentice.
The paint master mainly bought paint and torch paint, the paint apprentices mainly brush paint (team spirit? Haha, can not help but remind me of CS, you first rush, I pick up.).
1. Here, let's define this paint work into an interface class:
Public Interface Work
{
Public void brush (); // brush paint
}
2. Because the task of paint masters and paint apprentices are brush paints, they have to achieve the WORK interface:
A: Paint apprentice
The work of brush paint is mainly completed by the paint apprentices, so we define the paint and brothers into brusher (the paint master said: "The apprentice is to work." ).
Public Class Brusher IMPLEments WORK {
Public void brush () {
System.out.println ("brush paint");
}
}
B: Master Paint
We define the paint master into Decorator.
Public Class Decorator IMPLEments WORK {
PRIVATE WORK WORK;
/ / The work of the paint master is placed in this list
Private arraylist prework = new arraylist ();
/ / The default work of the paint master
Public Decorator (Work Work) {
THIS.WORK = WORK;
Prework.add ("Buy Paint");
Prework.add ("Paint");
}
Public void brush () {// brush paint, paint master also implements this method
NewWork (); // When the paint master is born, start a new job
}
//new job
Public void newwork () {
Prework (); // Paint master's pre-auxiliary work
Work.brush (); // Let the apprentice dry brush paint work
}
// Paint master's pre-auxiliary work
Public void prework () {
Listiterator ListIterator = prework.listiterator ();
While (Listiterator.hasnext ()) {
System.out.println ((String)) "Complete");
}
}
3, write test classes:
Public class test {
Public static void main (string args []) {
Work bursher = new brusher ();
Work Decorator = New Decorator (BURSHER);
Decorator.brush ();
/ / I will give the painting master, and the paint master will assign the actual brush paint to the paint.
}
4, explanation:
A: The code is only used to learn Decorator mode. If you want to run, you must do a change.
B: In this process, I only deal with the paint master, the specific brush paint is between the paint master and paint apprentices, I don't care.