State mode : contains a reference to a parent class object or an interface in an object. The reference points to a subclass object, which changes the class of the subclass object through an internal method, changing the same reference call to the same method to achieve different effects.
Public classA {PrivateB b=NewB (); voidBUILDC () {b=NewC ();} voidBuildd () {b=NewD ();} voidBuilde () {b=NewE ();} Public Static voidMain (string[] args) {A a=NewA (); A.b.say (); A.BUILDC (); A.b.say (); A.BUILDD (); A.b.say (); A.builde (); A.b.say (); } //The results are as follows://sya:b//Sya:c//sya:d//sya:e }classb{ Public voidsay () {System.out.println ("Sya:b"); }}classCextendsB { Public voidsay () {System.out.println ("Sya:c"); }}classDextendsB { Public voidsay () {System.out.println ("Sya:d"); }}classEextendsB { Public voidsay () {System.out.println ("Sya:e"); }}
State mode (think in Java design mode)