Factory, configuration files, and reflection learning in Java

Source: Internet
Author: User

The best way to learn the program is to make code to achieve it, here we assume a scene for a party, the party process is: 1. The party begins; 2. Singing; 3. Dance; 4. Sketch; 5 ends.

I. Traditional methods

1. New Java Project: party

2. New class Eveningparty in Party project

Package Com.gmail.lsgjzhuwei;public class Eveningparty {public static void main (string[] args) {}}
Implement the party process in the Eveningparty class, create a new process function and implement the entire party process in the process function, with the following code:

public void process () {//1. The party begins//2. Singing//3. Dance//4. Comedy//5. The party ends System.out.println ("Party Start");//singing singable singable = new Liudehua (); singable.sing ();//Dance dancleable dancleable = new Xiaohudui ();d ancleable.dance ();//sketch Essayable essayable = New Zhaobenshan (); Essayable.essay ();}

Here there are three classes singable, Dancleable, and Essayable, for the new interface, in order to extend the program, need to establish three interfacessingable, Dancleable, and essayable, respectively, for the realization of singing and dancing and sketches, the specific code for:

Package com.gmail.lsgjzhuwei;/** * @author Leo Chu *singable Interface for the realization of singing * 2014-8-24 */public interface singable {public void Sing ();}

Package com.gmail.lsgjzhuwei;/** * @author Leo Chu *dancleable Interface for the realization of dancing * 2014-8-24 */public interface Dancleable {public V OID dance ();}

Package com.gmail.lsgjzhuwei;/** * @author Leo Chu *essayable Interface for the implementation of skit * 2014-8-24 */public interface essayable {public voi D essay ();}

3. Define the above interface respectively for singers, dancers and sketch performers

3.1 Defining the Liudehua class to implement the Singable interface

Package Com.gmail.lsgjzhuwei;public class Liudehua implements singable {public void Sing () {//TODO auto-generated method StubSystem.out.println ("Andy Lau Sings Chinese");}}

3.2 Defining naying implementing the Sinable interface

Package Com.gmail.lsgjzhuwei;public class naying implements singable{public void Sing () {System.out.println ("Na ying singing: mirrors ");}}

3.3 Defining Xiaohudui Implementing the Dancleable interface
Package Com.gmail.lsgjzhuwei;public class Xiaohudui implements dancleable {public void Dance () {//TODO auto-generated met Hod stubSystem.out.println ("Little Tigers Dance: Love");}}

3.4 Defining Chenailian Implementing the Dancleable interface

Package Com.gmail.lsgjzhuwei;public class Chenailian implements dancleable {public void Dance () {//TODO auto-generated Me Thod stubSystem.out.println ("Ailian Dance: Swan Dance");}}

3.5 Defining Zhaobenshan Implementing the Essayable interface

Package Com.gmail.lsgjzhuwei;public class Zhaobenshan implements essayable {public void essay () {//TODO auto-generated Me Thod stubSystem.out.println ("Zhao Benshan Sketch: Maiguai");}}

3.6 Defining Liuqian Implementing the Essayable interface
Package Com.gmail.lsgjzhuwei;public class Liuqian implements essayable {public void essay () {//TODO auto-generated method StubSystem.out.println ("Liu Qian Magic: Air");}}
4. Modify the code in the main function

Package Com.gmail.lsgjzhuwei;public class Eveningparty {public static void main (string[] args) {new Eveningparty (). Process ();} public void process () {//1. The party begins//2. Singing//3. Dance//4. Comedy//5. The party ends System.out.println ("Party Start");//singing singable singable = new Liudehua (); singable.sing ();//Dance dancleable dancleable = new Xiaohudui ();d ancleable.dance ();//sketch//essayable Essayable = new Zhaobenshan (); Essayable.essay (); SYSTEM.OUT.PRINTLN ("Party End");}}

OK, the program can run normally.

But when the program says so, there is a problem: every time a better actor needs to change the code of the Party class, which brings inconvenience to the extension and maintenance of the program. So what should I do? Can you do it? These troublesome changes are outsourced to an agent to do, by the agent class to complete the modification of the actor, this is what we have to introduce a method: With the factory class agent responsible for code modification.

two/Use the factory class to put the parts that need to change into the factory class

1. Create a new factory class based on the above procedure,

Package Com.gmail.lsgjzhuwei;import Java.util.resourcebundle;public class Factory {public  singable getsingable () {return new Liudehua ();} Public  dancleable getdancleable () {return new Xiaohudui ();} public static essayable getessayable () {return new Zhaobenshan ();}}

2. Modify the party class code

Package Com.gmail.lsgjzhuwei;public class Eveningparty {public static void main (string[] args) {new Eveningparty (). Process ();} public void process () {//1. The party began to//2. Singing//3 dance//4. Comedy//5. The party ends System.out.println ("Party Start");////singing//singable singable = new Liudehua ();//singable.sing ();//////dance//dancleable dancleable = new Xiaohudui ();//dancleable.dance ();//////Sketch// Essayable essayable = new Zhaobenshan ();//essayable.essay (); Factory Factory = new Factory (); Factory.getsingable (). sing (); factory.getdancleable (). Dance (); Factory.getessayable ( ). essay (); SYSTEM.OUT.PRINTLN ("Party End");}}

OK, also can run

There is no need to modify the party class in this way, but to modify the code, another way is to write the actor's instance class to the configuration file

third, mapping method

1. On the basis of the above code, in the SRC directory under the new Party.properties class, the actor's instance class is written to the configuration file, the code is:

singable = com.gmail.lsgjzhuwei.LiudehuaDancleable = Com.gmail.lsgjzhuwei.XiaohuduiEssayable = Com.gmail.lsgjzhuwei.Zhaobenshan
2. Modify the code in the factory class to invoke the actor class:

Package Com.gmail.lsgjzhuwei;import Java.util.resourcebundle;public class Factory {public static singable getsingable ( {String className = resourcebundle.getbundle ("Party"). GetString ("singable"); try {Object obj = Class.forName (  ClassName). newinstance (); return (singable) obj; catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); throw new RuntimeException ("Object Incorrect");}} public static dancleable getdancleable () {String className = resourcebundle.getbundle ("Party"). GetString ("dancleable") try {Object obj = Class.forName (className). newinstance (); return (dancleable) obj;} catch (Exception e) {//TODO Auto-gen Erated catch Blocke.printstacktrace (); throw new RuntimeException ("object is incorrect");}} public static essayable getessayable () {String className = resourcebundle.getbundle ("Party"). GetString ("essayable"); try {Object obj = Class.forName (className). newinstance (); return (essayable) obj;} catch (Exception e) {//TODO Auto-genera Ted Catch Blocke.printstacktrace (); throw new Runtimeexception ("object is incorrect");}}} 

3. Do not modify the party class code, execute the program, OK correct.




Factory, configuration files, and reflection learning in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.