Java modifier mode, java Decoration
Introduction to the Java modifier Mode
I. assume there is a Worker interface, which has a doSomething method. Both Plumber and Carpenter implement the Worker interface. The code and relationship are as follows:
1.Worker.javapackage decorator;public interface Worker { public void doSomething();}
2. Plumber. javapublic class Plumber implements Worker {@ Override public void doSomething () {System. out. println ("plumbing repair ");}}
3. Carpenterpublic class Carpenter implements Worker {public void doSomething () {System. out. println ("repair doors and windows ");}}
4. test code. javapublic class Test {public static void main (String [] args) {Worker worker = null; Carpenter carpenter = new Carpenter (); Plumber plumber = new Plumber (); worker = carpenter; worker. doSomething (); worker = plumber; worker. doSomething ();}}
The test results are as follows:
2. Currently, two companies A and B have Carpenter and Plumber, but they have different requirements for both types of work, company A requires employees to introduce "I am from company A" before doSomething; Company B requires employees to introduce "I am from Company B" before doSomething "; A direct but unscientific design method is to design their respective Carpenter and Plumber for each company. If the company increases to 100, the number of classes is increased to 200. If other types of work are added, more classes are added, and the relationships are as follows:
3. This situation is suitable for the decorator model. Each company designs a decoration class xWorker, which also implements the Worker interface, in addition, there is a constructor whose parameter type is Worker and a member variable whose type is Worker. In this way, xWorker can obtain the reference of the corresponding type worker. In xWorker's doSomething () methods to deal with the special requirements of their respective companies:
1. Added class AWorker. java 2 3 public class AWorker implements Worker {4 5 private Worker worker; 6 7 // through some constructor, AWorker can get reference of a specific job type, 8 // prepare for the special requirements for adding Company A to the following doSomething () method 9 public AWorker (Worker worker) {10 this. worker = worker; 11} 12 13 @ Override14 public void doSomething () {15 System. out. println ("Hello! I am an employee of Company A "); 16 worker. doSomething (); 17} 18 19}
1 // Add class BWorker. java 2 3 public class BWorker implements Worker {4 5 private Worker worker; 6 7 public BWorker (Worker worker) {8 this. worker = worker; 9} 10 11 @ Override12 public void doSomething () {13 System. out. println ("Hello! I am an employee of Company B. "); 14 worker. doSomething (); 15} 16 17}
1 // test code 2 3 public static void main (String [] args) {4 5 // Company A 6 Carpenter = new Carpenter (); 7 Plumber plumberA = new Plumber (); 8 AWorker aWorker1 = new AWorker (carpener); 9 AWorker aWorker2 = new AWorker (plumberA); 10 11 worker (); 12 worker (); 13 14 System. out. println ("================================== "); 15 16 // Company B 17 Carpenter carpenterB = new Carpenter (); 18 Plumber plumberB = new Plumber (); 19 BWorker bWorker1 = new BWorker (carpenterB ); 20 BWorker bWorker2 = new BWorker (plumberB); 21 22 bWorker1.doSomething (); 23 bWorker2.doSomething (); 24} 25}
Test results: