Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 259 & extra = Page % 3d2
Maybe you are already using the decorator mode, but like all other models, it is important to know why you use it and learn to use it to communicate with other developers. The decorator mode, that is, the wrapper mode. Its force control function is not to change a class. Code Or add a function for the class without damaging the class interface. When you want a class to have more functions, using the decorator Mode means that the new version does not have to use the extension class to reuse the existing functions. Assume that an interface called action has two methods: act1 () and Act2 (). This interface has a specific version of concreteaction. The decorator will be a class that implements the action, and its concreteaction has an action (usually concreteaction ). The Code is as follows: Public Class Actiondecorator implements action {
Private Action action;
Public Actiondecorator (action Action ){ This . Action = Action; }
Public Void Act1 (){ Action. act1 (); }
Public Void Act2 (){ // Do nothing }
} Using decorator produces fewer classes than using inheritance, so its code is relatively simple. However, it usually produces more objects, which increases the difficulty of debugging, especially because it increases flexibility, new errors may be introduced. |