This topic is placed in the http://topic.csdn.net/u/20090915/12/a4424d66-0331-42b2-b08a-c08af684640a.html
Because the code is too long, go to the blog.
I have been teaching Java for a short time. I want to use Java to describe the following three stories, which requires high reusability and scalability. It allows students to understand the characteristics of object-oriented encapsulation, inheritance, and polymorphism. It is best to use the design model to enhance scalability and facilitate further learning.
The four Tang Monk traveled by plane. The plane crashed on the way, but there were only three parachutes.
As a result, Tang Miao said, everyone will answer questions and fail to answer questions.
Tang Seng: Wukong, how many suns are there in the sky?
Wukong: One.
Tang MIAO: OK. Give it to you.
Tang MIAO: How many moons are there in the sky?
Sha Seng: One.
Tang MIAO: Well, I 'd like to give it to you.
It's a simple question.
Tang MIAO: How many stars are there in the sky?
....
The eighth ring jumped down.
It wasn't long before the four of them traveled by plane again. They crashed again on the way, and there were only three parachutes.
They continue to answer questions.
Tang Seng: Wukong. When will the people's republic of china be established?
Wukong: October 11, 1949.
Tang MIAO: OK. Give it to you.
Tang MIAO: how many people have died in the war of liberation?
Sha Seng: 2.5 million people.
Tang MIAO: Well, I 'd like to give it to you.
Tang Seng: What is the name of the 2.5 million people?
... The eight rings had to jump on again.
The third time, the four of them traveled by plane again, and an accident occurred on the way.
At this time, BA Jie said: Master, you don't have to ask. I will do it myself.
Next, let's jump.
Tang Seng co-handed: Amitabha, there are four parachutes this time
Factory method mode
Command mode:
Observer mode:
After merging:
Itudiaction plays the role of observer
Ishifuaction plays the role of Subject
The command mode is embodied in the main function.
The Code is as follows:
// Main. Java
Public class main
{
Public static void sceneone ()
{
Ianimalfactory animalfactory1 = new stonemonkeyfactory ();
Animal Wukong = animalfactory1.createanimal ("Wukong"); // Sun Wukong
Ianimalfactory animalfactory2 = new stonemonkeyfactory ();
Animal Bajie = animalfactory2.createanimal ("Bajie"); // pig
Ipersonfactory personfactory1 = new shifumonkfactory ();
Person tangseng = personfactory1.createperson ("tangseng"); // Tang Seng
Ipersonfactory personfactory2 = new tudimonkfactory ();
Person shaseng = personfactory2.createperson ("shaseng"); // Sha Seng
(Shifumonk) tangseng). Attach (itudiaction) Wukong );
(Shifumonk) tangseng). Attach (itudiaction) shaseng );
(Shifumonk) tangseng). Attach (itudiaction) Bajie );
Command askcommand1 = new askcommand (itudiaction) Wukong );
Command askcommand2 = new askcommand (itudiaction) shaseng );
Command askcommand3 = new askcommand (itudiaction) Bajie );
(Shifumonk) tangseng). setorder (askcommand1, "How many suns are there in the sky? ");
String answer = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer, 0 );
(Shifumonk) tangseng). setorder (askcommand2, "How many moons are there in the sky? ");
String answer2 = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer2, 1 );
(Shifumonk) tangseng). setorder (askcommand3, "How many stars are there in the sky? ");
String answer3 = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer3, 2 );
}
Public static void scenetwo ()
{
Ianimalfactory animalfactory1 = new stonemonkeyfactory ();
Animal Wukong = animalfactory1.createanimal ("Wukong"); // Sun Wukong
Ianimalfactory animalfactory2 = new stonemonkeyfactory ();
Animal Bajie = animalfactory2.createanimal ("Bajie"); // pig
Ipersonfactory personfactory1 = new shifumonkfactory ();
Person tangseng = personfactory1.createperson ("tangseng"); // Tang Seng
Ipersonfactory personfactory2 = new tudimonkfactory ();
Person shaseng = personfactory2.createperson ("shaseng"); // Sha Seng
(Shifumonk) tangseng). Attach (itudiaction) Wukong );
(Shifumonk) tangseng). Attach (itudiaction) shaseng );
(Shifumonk) tangseng). Attach (itudiaction) Bajie );
Command askcommand1 = new askcommand (itudiaction) Wukong );
Command askcommand2 = new askcommand (itudiaction) shaseng );
Command askcommand3 = new askcommand (itudiaction) Bajie );
(Shifumonk) tangseng). setorder (askcommand1, "When will the people's republic of china be established? ");
String answer = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer, 0 );
(Shifumonk) tangseng). setorder (askcommand2, "How many people have died in the liberation war? ");
String answer2 = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer2, 1 );
(Shifumonk) tangseng). setorder (askcommand3, "what is the name of the 2.5 million person? ");
String answer3 = (shifumonk) tangseng). pleasereply ();
(Shifumonk) tangseng). Judge (answer3, 2 );
}
Public static void scenethree ()
{
Ianimalfactory animalfactory1 = new stonemonkeyfactory ();
Animal Wukong = animalfactory1.createanimal ("Wukong"); // Sun Wukong
Ianimalfactory animalfactory2 = new stonemonkeyfactory ();
Animal Bajie = animalfactory2.createanimal ("Bajie"); // pig
Ipersonfactory personfactory1 = new shifumonkfactory ();
Person tangseng = personfactory1.createperson ("tangseng"); // Tang Seng
Ipersonfactory personfactory2 = new tudimonkfactory ();
Person shaseng = personfactory2.createperson ("shaseng"); // Sha Seng
(Shifumonk) tangseng). Attach (itudiaction) Wukong );
(Shifumonk) tangseng). Attach (itudiaction) shaseng );
(Shifumonk) tangseng). Attach (itudiaction) Bajie );
System. Out. println ("Bajie jumped out! There are four parachutes! ");
}
Public static void main (string [] ARGs)
{
Main. sceneone ();
System. Out. println ("----------------");
Main. scenetwo ();
System. Out. println ("----------------");
Main. scenethree ();
}
}
// Animal. Java
Public abstract class animal
{
Protected string name = "";
Animal (string name)
{
This. Name = Name;
}
Animal ()
{
This. Name = "anonymous ";
}
Public String displayname ()
{
Return name;
}
}
// Monkey. Java
Public abstract class monkey extends animal
{
Monkey ()
{
Super ();
}
Monkey (string name)
{
Super (name );
}
}
// Stonemonkey. Java
Public class stonemonkey extends monkey implements itudiaction
{
Stonemonkey ()
{
Super ();
}
Stonemonkey (string name)
{
Super (name );
}
Public void jump ()
{
}
Public String replyquestion (string q)
{
Stonemonkeyknowlegedatabase smkdb = new stonemonkeyknowlegedatabase ();
Return smkdb. T. Get (Q );
}
}
Class stonemonkeyknowlegedatabase
{
Myhashtable <string, string> T = new myhashtable <string, string> ();
Stonemonkeyknowlegedatabase ()
{
T. Put ("How many suns are there in the sky? "," One ");
T. Put ("When will the people's republic of china be established? "," 1949 ");
}
}
// Pig. Java
Public abstract class pig extends animal
{
Pig ()
{
Super ();
}
Pig (string name)
{
Super (name );
}
}
// Pigman. Java
Public class pigman extends pig implements itudiaction
{
Pigman ()
{
Super ();
}
Pigman (string name)
{
Super (name );
}
Public void jump ()
{
}
Public String replyquestion (string question)
{
Return NULL;
}
}
Class pigmanknowlegedatabase
{
Pigmanknowlegedatabase ()
{
Myhashtable <string, string> T = new myhashtable <string, string> ();
T. Put ("How many stars are there in the sky? "," Don't know ");
T. Put ("what is the name of 2.5 million people? "," Don't know ");
}
}
// Person. Java
Public abstract class person
{
Protected string name = "";
Person (string name)
{
This. Name = Name;
}
Person ()
{
This. Name = "anonymous ";
}
Public String displayname ()
{
Return name;
}
}
// Monk. Java monk class
Public class monk extends person // monk
{
Monk ()
{
Super ();
}
Monk (string name)
{
Super (name );
}
}
// Shifumonk. Java
Import java. util. arraylist;
Import java. util. List;
Public class shifumonk extends monk implements ishifuaction
{
Private list <itudiaction> Observers = new arraylist <itudiaction> ();
Private string action;
String question;
Private command;
Public void setorder (command, string question)
{
This. Command = command;
This. Question = question;
}
Public String pleasereply ()
{
Return command. replyquestion (question );
}
Shifumonk ()
{
Super ();
}
Shifumonk (string name)
{
Super (name );
}
Public void attach (itudiaction observer)
{
Observers. Add (observer );
}
Public void detach (itudiaction observer)
{
Observers. Remove (observers );
}
Public String getshifustate ()
{
Return Action;
}
Public void setshifustate (string subjectstate)
{
Action = subjectstate;
}
Public void judge (string a, int I) // is the referee's question answered correctly?
{
Shifumonkknowlegedatabase smkdb = new shifumonkknowlegedatabase ();
If (smkdb. T. Get (this. Question). Equals ())
{
System. Out. println (itudiaction) observers. Get (I). displayname () + "give you a parachute! ");
}
Else
{
Orderjump (I );
}
}
Public void orderjump (int I)
{
System. Out. println (itudiaction) observers. Get (I). displayname () + "Skip ");
}
}
Class shifumonkknowlegedatabase
{
Myhashtable <string, string> T = new myhashtable <string, string> ();
Shifumonkknowlegedatabase ()
{
T. Put ("How many suns are there in the sky? "," One ");
T. Put ("When will the people's republic of china be established? "," 1949 ");
T. Put ("How many moons are there in the sky? "," One ");
T. Put ("how many people have died in the liberation war? "," 2.5 million people ");
T. Put ("How many stars are there in the sky? ","??? ");
T. Put ("what is the name of 2.5 million people? ","??? ");
}
}
// Tudimonk. Java apprentice monk
Public class tudimonk extends monk implements itudiaction
{
Tudimonk ()
{
Super ();
}
Tudimonk (string name)
{
Super (name );
}
Public void jump ()
{
}
Public String replyquestion (string q)
{
Tudimonkknowlegedatabase tmkdb = new tudimonkknowlegedatabase ();
Return tmkdb. T. Get (Q );
}
}
Class tudimonkknowlegedatabase
{
Myhashtable <string, string> T = new myhashtable <string, string> ();
Tudimonkknowlegedatabase ()
{
T. Put ("How many moons are there in the sky? "," One ");
T. Put ("how many people have died in the liberation war? "," 2.5 million people ");
}
}
// Tudimonkfactory. Java
Public class tudimonkfactory implements ipersonfactory
{
Public Person createperson (string name)
{
Return new tudimonk (name );
}
}
// Stonemonkeyfactory. Java
Public class stonemonkeyfactory implements ianimalfactory
{
Public animal createanimal (string name)
{
Return new stonemonkey (name );
}
}
// Shifumonkfactory. Java
Public class shifumonkfactory implements ipersonfactory
{
Public Person createperson (string name)
{
Return new shifumonk (name );
}
}
// Pigmanfactory. Java
Public class pigmanfactory implements ianimalfactory
{
Public animal createanimal (string name)
{
Return new pigman (name );
}
}
// Ipersonfactory. Java
Public interface ipersonfactory
{
Person createperson (string name );
}
// Ianimalfactory. Java
Public interface ianimalfactory
{
Animal createanimal (string name );
}
// Command. Java
Public abstract class command
{
Protected itudiaction TuDi;
Public command (itudiaction TuDi)
{
This. TuDi = TuDi;
}
Abstract Public String replyquestion (string Q );
Abstract Public void jump ();
}
// Askcommand. Java
Public class askcommand extends command
{
String question;
Askcommand (itudiaction TuDi)
{
Super (TuDi );
}
Public String replyquestion (string question)
{
Return TuDi. replyquestion (question );
}
Public void jump ()
{
}
}
// Jumpcommand. Java
Public class jumpcommand extends command
{
Jumpcommand (itudiaction TuDi)
{
Super (TuDi );
}
Public void jump ()
{
}
@ Override
Public String replyquestion (string q)
{
Return NULL;
}
}
// Myhashtable. Java
Import java. util. hashtable;
Public class myhashtable <K, V>
{
Public hashtable <K, V> H = new hashtable <K, V> ();
Public void put (K, V)
{
H. Put (K, V );
}
Public v get (K)
{
Return H. Get (k );
}
}
// Itudiaction. Java
Public interface itudiaction // equivalent to observer in observer Mode
{
// Equivalent to the update in the Observer mode, indicating a response, such as "Answer" and "Jump down"
String replyquestion (string Q );
Void jump ();
Public String displayname ();
}
// Ishifuaction. Java
Public interface ishifuaction // subject equivalent to the observer Mode
{
Void attach (itudiaction observer); // Add an observer
Void detach (itudiaction observer); // reduce the observer
Public String getshifustate ();
Public void setshifustate (string subjectstate );
// Set the command
Void setorder (command C, string Q); // Problem
Void orderjump (int I); // the I-th hop of the command
// Notification response
Public String pleasereply ();
// Confirm the decision
Void judge (string a, int I );
}
Running result:
Wukong gives you a parachute!
Shaseng gave you a parachute!
Bajie jump down
----------------
Wukong gives you a parachute!
Shaseng gave you a parachute!
Bajie jump down
----------------
Bajie jumped down! There are four parachutes!