Layering is the basic way for software to solve problems... but just one layering is even a design pattern...
Chestnut: ye bei catches animals on Olympus
1. Interfaces of creatures on Olympus
public interface God { public String power(); public String getName();}
2. creature on Mount Olympus
Public class Apollo implements God {Public String power () {return "messing up";} public void plague () {system. out. println ("Apollo started spreading plague");} Public String getname () {return "Apollo" ;}} public class Ares implements God {Public String power () {return "Tease";} public void war () {system. out. println ("arris started to fight");} Public String getname () {return "arris" ;}} public class Zeus implements God {Public String power () {return "lightning";} public void doomsday () {system. out. println ("Zeus triggered the end of the world");} Public String getname () {return "Zeus ";}}
3. baye and his skills
Public class Bill {public void executecatch () {int num = (INT) (math. random () * World. olympus. size (); God prey = World. olympus. get (Num); system. out. print ("Byte grabbed" + prey. getname (); system. out. print ("------------- ye has the power of his prisoners:" + prey. power ());}}
4. World/scenario
Public class world {// The God static final list in this world <God> Olympus = new arraylist <God> () {Add (new Zeus ()); add (New Apollo (); add (New ares () ;}}; static final bill = new Bill (); public static void main (string [] ARGs) throws interruptedexception {While (true) {bill.exe cutecatch (); thread. sleep (1000 );}}}
Scenario 1: normal capturing by baye
Ye bay captured the power of God: lord Bay grabbed Ares ------------- and Ye bay possessed the power of his prisoners: ye Bai captured the power of Zeus ------------- And ye bei possessed the power of his prisoners: ye lightning grabbed Zeus ------------- ye baye possessed the power of his prisoners: ye lightning grabbed Ares ------------- he possessed the power of his prisoners: ye pig captured the power of Zeus ------------- ye beiye possessed his prisoners: ye lightning grabbed Zeus ------------- ye beiye possessed the power of his prisoners: Lightning
Scenario 2: baye tries to communicate with his prisoners to obtain more information at a time.
At this time, Ye's skills are slightly modified.
Public class Bill {public void executecatch () {int num = (INT) (math. random () * World. olympus. size (); God prey = World. olympus. get (Num); system. out. print ("Byte grabbed" + prey. getname (); system. out. print ("------------- ye has the power of his prisoners:" + prey. power (); system. out. println ("++ knows the real body of his prey:" + prey. getclass ());}}
The result is:
Uncle Bay captured the power of Zeus ------------- Ye bay had his captive: ye lightning ++ knew the real body of his prey: class god. ye Zeus Bay captured the power of God: class god. god Apollo captured the power of God Bayes ------------- he possessed his prisoners: he learned the real body of his prey through the play ++: class god. ye Ares has seized the power of his prisoners: He knows the real body of his prey: class god. ye Ares Bay captured the power of Zeus ------------- Ye bay had his captive: ye lightning ++ knew the real body of his prey: class god. ye Zeus Bay captured the power of his prisoners: He knows the real body of his prey: class god. ye Ares has seized the power of his prisoners: He knows the real body of his prey: class god. ares
Scenario 3: as the real identity is obtained, ye beiye will certainly try to let his prisoner do more, so
Baye becomes:
Public void executecatch () {int num = (INT) (math. random () * World. olympus. size (); God prey = World. olympus. get (Num); system. out. print ("Byte grabbed" + prey. getname (); system. out. print ("------------- ye has the power of his prisoners:" + prey. power (); system. out. print ("++ know the real body of his prey:" + prey. getclass (); If (prey instanceof Zeus) {Zeus = (Zeus) prey; system. out. print ("////////// let ye let his prisoner" + prey. getname () + "release your secret:"); Zeus. doomsday ();} else if (prey instanceof Apollo) {Apollo = (Apollo) prey; system. out. print ("////////// let ye let his prisoner" + prey. getname () + "release your secret:"); Apollo. plague ();} else if (prey instanceof Ares) {Ares = (Ares) prey; system. out. print ("////////// let ye let his prisoner" + prey. getname () + "releases your secret:"); ARES. war ();} else {system. out. println ("ye needs to further analyze the prisoners ");}}}
The result is changed:
Baye captured the power of arris ------------- baye, who possessed his prisoners: he learned the real body of his prey through the play ++: class god. ares ////////// ye beiye releases his prisoner arris: arris started the war, and ye Bay grabbed Zeus ------------- Ye bay had the power of his prisoners: lightning ++ knows the real body of his prey: Class god. zeus ////////// ye Beye let his captive Zeus release his secret: zeus triggered the End of the World. God Bay captured the power of Zeus ------------- Ye bay: lightning ++ knows the real body of his prey: Class god. zeus ////////// ye Beye let his captive Zeus release his secret: zeus triggered the End of the World. God Bay captured the power of Zeus ------------- Ye bay: lightning ++ knows the real body of his prey: Class god. zeus ////////// ye Beye let his captive Zeus release his secret: zeus triggered the End of the World. God Bay captured the power of Zeus ------------- Ye bay: lightning ++ knows the real body of his prey: Class god. zeus ////////// ye Beye let his captive Zeus release his secret: zeus triggered the End of the World. baye captured the power of arris ----------- baye: the play ++ knows the real body of his prey: Class god. ares ////////// ye beiye releases his prisoner arris: arris started the war. baye grabbed Apollo ------------- baye and possessed the power of his prisoners: he knows the real body of his prey: Class god. apollo ////////// ye let his captive Apollo release his secret: Apollo began spreading plague
In this case, can you play happily? It just allows you to catch prey. Why do you get a big deal of prey?
To prevent Ye bay from having too many skills, we can only give the creature on the Olympus Mountains a replacement-"Facade
Introduce facade:
1. First, you must have Facade
public class GodFacade implements God{ private God god=null; public GodFacade(God god) { super(); this.god = god; } public String power() { return god.power(); } public String getName() { return god.getName(); }}
2. Start Using Facade
Public class world {// The God static final list in this world <God> Olympus = new arraylist <God> () {Add (New godfacade (new Zeus (); add (New godfacade (New Apollo ())); add (New godfacade (New ares () ;}}; static final bill = new Bill (); public static void main (string [] ARGs) throws interruptedexception {While (true) {bill.exe cutecatch (); thread. sleep (1000 );}}}
Final calculation result:
Uncle Bay captured the power of Zeus ------------- Ye bay had his captive: ye lightning ++ knew the real body of his prey: class god. godfacade ye needs to further analyze the captured Ye's grasp of Ares ------------- Ye's possession of his captive power: the play ++ knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Apollo ------------- Ye's possession of his captive power: he knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Zeus ------------- Ye's possession of his captive power: lightning ++ knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Ares ------------- Ye's possession of his captive power: the play ++ knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Apollo ------------- Ye's possession of his captive power: he knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Zeus ------------- Ye's possession of his captive power: lightning ++ knows the real body of his prey: Class god. godfacade ye needs to further analyze the captured Ye's grasp of Apollo ------------- Ye's possession of his captive power: he knows the real body of his prey: Class god. godfacade Bay needs to further analyze the prisoners
Well, the world is quiet, and ye Bay can no longer use the big moves of prey ....
Maybe we can simply find that facade does not make it easier to use, but it has to add a few more parts, and his final defense is actually baye (other programmers)
If you think so, let's just talk about it.
What are the basic features of object orientation? What makes inheritance, encapsulation, and polymorphism easier to use? How is the upper mode encapsulated?
Maybe we can think that we can strengthen or weaken inheritance, encapsulation, polymorphism, and better use of object-oriented methods, all of which are called design patterns.
Facade solves the problem of encapsulation + inheritance of open information. Maybe in a general project, this product has no meaning. After all, it is easier to know more about how to complete and call tasks, but what if it is used for other ape or frameworks? Unclear?
It's ape, no chestnuts, no understanding.
Chestnut: create a web project, create a servlet, and add system. Out. println (request. getclass () to the doget method. You will see
class org.apache.catalina.connector.RequestFacade
Even if ye catches Tom, he won't be able to take the lead...
Next, let's try out what the class interface provided in the framework of our project is, and there are benefits.