(a) A brief introduction to the mode of entrust
Entrust mode is one of the main design patterns. Entrusted. That is, let another object help you do things.
More models, such as state mode, strategy mode, and interview mode, are essentially used in a more special situation.
The commit mode allows us to use aggregations instead of inheritance. java-combination is better than inheritance.
The simplest Java commit mode
Class Realprinter { void print () { System.out.println ("Real Printer");} } Class Printer { realprinter realprinter = new Realprinter (); public void print () { realprinter.print (); }}
/** * Simple Entrust Mode * * @author Peter_wang * @create-time 2014-5-19 pm 5:39:42 */public class Delegationdemo { /** * @ param args * /public static void main (string[] args) { Printer Printer = new Printer (); Printer.print (); }}
(ii) the trust mode in Android
The Listerner event in Android is a commit mode, for example, a button click event.
Let's simulate how the entire click event is using the commit model.
/** * Analog Basic View * * @author Peter_wang * @create-time 2014-5-19 pm 5:03:55 */public Class View { private onclicklist Ener Monclicklistener; /** * Analog Click event */public void Clickevent () { if (monclicklistener! = null) { Monclicklistener.onclick (this); } } public void Setonclicklistener (Onclicklistener onclicklistener) { this.monclicklistener = Onclicklistener; } /** * Click event interface * * @author peter_wang * @create-time 2014-5-19 pm 5:05:45 */Public Interface Onclicklistener {public void OnClick (View v); }}
/** * Analog button * * @author Peter_wang * @create-time 2014-5-19 pm 5:17:57 */public class Button extends View {}
/** * Simulated basic Activity class * * @author Peter_wang * @create-time 2014-5-19 pm 5:20:38 */public class Activity {public STA tic void Main (string[] args) { activity activity = new activity (); Activity.oncreate (); } /** * Analog OnCreate method * /protected void OnCreate () { }}
/** * Entrust Simulation page * * @author Peter_wang * @create-time 2014-5-19 pm 5:19:22 */public class delegationactivity extends A Ctivity implements Onclicklistener { private Button Mbutton; @Override protected void OnCreate () { super.oncreate (); Mbutton = new Button (); Mbutton.setonclicklistener (this); Analog Click event mbutton.clickevent (); } @Override public void OnClick (View v) { if (v = = Mbutton) { System.out.println ("OnClick () is callback!"); } }}
Android in-depth design mode (a) Entrust mode