Iperson:
/*** @ Author com. Tiantian * @ version Creation Time: 3:43:04 */public interface iperson {public void cando ();}
Person:
/*** @ Author COM. tiantian * @ version Creation Time: 3:44:04 */public class person implements iperson {@ override public void cando () {system. out. println ("I Can Code ");}}
Decorator (parent class of all person decorators ):
/*** @ Author COM. tiantian * @ version Creation Time: 3:44:55 */public class decorator implements iperson {private iperson person; Public decorator (iperson person) {This. person = person ;}@ override public void cando () {person. cando ();}}
Decoratorswim (swim decoration -- add "swim" function for Peron ):
/*** @ Author COM. tiantian * @ version Creation Time: 3:48:54 */public class decoratorswim extends decorator {public decoratorswim (iperson person) {super (person) ;}@ override public void cando () {super. cando (); system. out. println ("I also can swim ");}}
Decoratordraw (draw decoration -- add the "Draw" function for Peron ):
/*** @ Author COM. tiantian * @ version Creation Time: 3:47:29 */public class decoratordraw extends decorator {public decoratordraw (iperson person) {super (person) ;}@ override public void cando () {super. cando (); system. out. println ("I also can draw ");}}
Test:
/*** @ Author COM. tiantian * @ version Creation Time: 3:49:35 */public class test {public static void main (string [] ARGs) {decorator = new decoratordraw (New decoratorswim (new person (); decorator. cando ();}}