java 反射工厂模式_java反射机制+工厂模式+配置文件----->在谈到spring配置文件

下面的程序是从魔乐科技java教程里面抄来的,感觉这几段程序说明了一些问题,所以就抄过来了。

下面是一个简单的工厂模式

package org;

interface Fruit{

public void eat();

}

class Apple implements Fruit{

public void eat(){

System.out.println("吃苹果。");

}

}

class Orange implements Fruit{

public void eat(){

System.out.println("吃橘子");

}

}

class Factory{ //工厂类

public static Fruit getInstance(String className){

Fruit f=null;

if(className.equals("apple")){

f=new Apple();

}

if(className.endsWith("orange")){

f=new Orange();

}

return f;

}

}

public class FactoryDemo02 {

public static void main(String args[]){

Fruit f=Factory.getInstance("apple");

f.eat();

}

}package org;

interface Fruit{

public void eat();

}

class Apple implements Fruit{

public void eat(){

System.out.println("吃苹果。");

}

}

class Orange implements Fruit{

public void eat(){

System.out.println("吃橘子");

}

}

class Factory{ //工厂类

public static Fruit getInstance(String className){

Fruit f=null;

if(className.equals("apple")){

f=new Apple();

}

if(className.endsWith("orange")){

f=new Orange();

}

return f;

}

}

public class FactoryDemo02 {

public static void main(String args[]){

Fruit f=Factory.getInstance("apple");

f.eat();

}

}

但是工厂类如果这样写的话,就有一个问题,如果增加了水果,比如香蕉,那么在工厂类里面也要进行相关的修改了,这样不合理,而java的反射机制可以解决这个问题

package org1;

interface Fruit {

public void eat();

}

class Apple implements Fruit {

public void eat() {

System.out.println("吃苹果。");

}

}

class Orange implements Fruit {

public void eat() {

System.out.println("吃橘子");

}

}

class Factory {

public static Fruit getInstance(String className) {

Fruit f = null;

try {

f = (Fruit) Class.forName(className).newInstance();

} catch (Exception e) {

e.printStackTrace();

}

return f;

}

}

public class CopyOfFactoryDemo03 {

public static void main(String args[]) {

Fruit f = Factory.getInstance("org1.Apple");

f.eat();

}

}package org1;

interface Fruit {

public void eat();

}

class Apple implements Fruit {

public void eat() {

System.out.println("吃苹果。");

}

}

class Orange implements Fruit {

public void eat() {

System.out.println("吃橘子");

}

}

class Factory {

public static Fruit getInstance(String className) {

Fruit f = null;

try {

f = (Fruit) Class.forName(className).newInstance();

} catch (Exception e) {

e.printStackTrace();

}

return f;

}

}

public class CopyOfFactoryDemo03 {

public static void main(String args[]) {

Fruit f = Factory.getInstance("org1.Apple");

f.eat();

}

}

利用java的反射机制,就能动态的实例化各种类了。

但是这个程序还是存在一个问题,就是主函数这里需要填入一个完整的类名称,不够方便,所以要增加配置文件来简化

package org3;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

interface Fruit {

public void eat();

}

class Apple implements Fruit {

public void eat() {

System.out.println("吃苹果。");

}

}

class Orange implements Fruit {

public void eat() {

System.out.println("吃橘子");

}

}

class Factory {

public static Fruit getInstance(String className) {

Fruit f = null;

try {

f = (Fruit) Class.forName(className).newInstance();

} catch (Exception e) {

e.printStackTrace();

}

return f;

}

}

class PropertiesOperate{

private Properties pro=null;

private File file=new File("d:"+File.separator+"fruit.properties");

public PropertiesOperate(){

this.pro=new Properties();

if(file.exists()){

try {

pro.loadFromXML(new FileInputStream(file));

} catch (Exception e) {

e.printStackTrace();

}

}else{

this.save();

}

}

private void save(){

this.pro.setProperty("apple","org3.Apple");

this.pro.setProperty("orange", "org3.Orange");

try {

this.pro.storeToXML(new FileOutputStream(this.file),"Fruit");

} catch (Exception e) {

e.printStackTrace();

}

}

public Properties getProperties(){

return this.pro;

}

}

public class CopyOfFactoryDemo04 {

public static void main(String args[]) {

Properties pro=new PropertiesOperate().getProperties();

Fruit f= Factory.getInstance(pro.getProperty("apple"));

f.eat();

}

}package org3;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

interface Fruit {

public void eat();

}

class Apple implements Fruit {

public void eat() {

System.out.println("吃苹果。");

}

}

class Orange implements Fruit {

public void eat() {

System.out.println("吃橘子");

}

}

class Factory {

public static Fruit getInstance(String className) {

Fruit f = null;

try {

f = (Fruit) Class.forName(className).newInstance();

} catch (Exception e) {

e.printStackTrace();

}

return f;

}

}

class PropertiesOperate{

private Properties pro=null;

private File file=new File("d:"+File.separator+"fruit.properties");

public PropertiesOperate(){

this.pro=new Properties();

if(file.exists()){

try {

pro.loadFromXML(new FileInputStream(file));

} catch (Exception e) {

e.printStackTrace();

}

}else{

this.save();

}

}

private void save(){

this.pro.setProperty("apple","org3.Apple");

this.pro.setProperty("orange", "org3.Orange");

try {

this.pro.storeToXML(new FileOutputStream(this.file),"Fruit");

} catch (Exception e) {

e.printStackTrace();

}

}

public Properties getProperties(){

return this.pro;

}

}

public class CopyOfFactoryDemo04 {

public static void main(String args[]) {

Properties pro=new PropertiesOperate().getProperties();

Fruit f= Factory.getInstance(pro.getProperty("apple"));

f.eat();

}

}

加入配置文件问题就解决了,以后如果要增加新的水果类,都要在这个配置文件里面登记。这时我们可以说配置文件可以控制程序的执行,现在看起来有点像spring的ioc了。

其实最近在学spring

配置文件,一开始看一个好长的配置文件根本就不知道什么意思,是用来干什么的?而且看到好多的bean,百度了好多,说spring里面有ioc,还有aop(暂时还不理解aop是什么东西),可是要理解ioc就要先理解工厂模式,什么是接口,什么是java的反射。如果新手想要尽快的入门,可以尝试这个学习过程(当然我也是新手啦~~)。

下面我把我自己对spring的理解记下来:

其实我们如果理解了上面这个简单的三个程序就好办了,回顾上面的程序,该程序使用了工厂模式,把所有的类放在一个Factory里面,而为了动态的管理这些类(即使增加了新的Fruit类,这个工厂也不用变化),就用了java的反射机制。另外里面还设置了一个配置文件,使得某一长窜完整的类名称,比如org1.Apple可以用任意简短的名称来代替,比如apple。PS:如果这段话让读者感觉有点反胃,那就忘了这句废话,总之只要理解上面的程序,加上下面将要列举的小小的测试程序,我想就能理解spring配置文件的大致用途了。

简单的spring配置文件测试:

Person类

package test2;

public class Person {

private String name;

private int age;

private Grade grade;

public String getName() {

return name;

}

public Grade getGrade() {

return grade;

}

public void setGrade(Grade grade) {

this.grade = grade;

}

public void setName(String name) {

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

public int getAge() {

return age;

}

public int getTotleGrade() {

return grade.getEnglish()+grade.getMath();

}

}package test2;

public class Person {

private String name;

private int age;

private Grade grade;

public String getName() {

return name;

}

public Grade getGrade() {

return grade;

}

public void setGrade(Grade grade) {

this.grade = grade;

}

public void setName(String name) {

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

public int getAge() {

return age;

}

public int getTotleGrade() {

return grade.getEnglish()+grade.getMath();

}

}

Grade类

package test2;

public class Grade {

private int math;

private int english;

public int getMath() {

return math;

}

public void setMath(int math) {

this.math = math;

}

public int getEnglish() {

return english;

}

public void setEnglish(int english) {

this.english = english;

}

}package test2;

public class Grade {

private int math;

private int english;

public int getMath() {

return math;

}

public void setMath(int math) {

this.math = math;

}

public int getEnglish() {

return english;

}

public void setEnglish(int english) {

this.english = english;

}

}

Bean.xml配置文件(该文件只要放在test2包里面就好了)(堆积木工程开始咯~~)

/p>

"http://www.springframework.org/dtd/spring-beans.dtd">

//很多豆豆

//第一个豆豆,是一个Person类,id名字随便取,还要写上类的全名

//下面开始把这个类里面的所有属性列出来,并赋值,至于你说难道一定要赋值吗?我想可以,我刚学,不知道

小龙//这里的名字是通过程序里面的set来赋值的,不信你去掉程序里面相关的set,就出错了

23

//这里有点特别,这个grade变量是一个对象,和一般的变量要区别对待

//这里指向了本配置文件里面一个名字叫Grade(即id=Grade)的bean

//同上

99

59

/p>

"http://www.springframework.org/dtd/spring-beans.dtd">

//很多豆豆

//第一个豆豆,是一个Person类,id名字随便取,还要写上类的全名

//下面开始把这个类里面的所有属性列出来,并赋值,至于你说难道一定要赋值吗?我想可以,我刚学,不知道

小龙//这里的名字是通过程序里面的set来赋值的,不信你去掉程序里面相关的set,就出错了

23

//这里有点特别,这个grade变量是一个对象,和一般的变量要区别对待

//这里指向了本配置文件里面一个名字叫Grade(即id=Grade)的bean

//同上

99

59

最后是Test测试类

package test2;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

import test.ExampleBean;

public class Test {

public static void main(String args[]){

Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径

System.out.println("resource is:" + input);

BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说)

Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你

Grade grade=(Grade)factory.getBean("Grade");

System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样

System.out.println("年龄:"+person.getAge());

System.out.println("数学成绩:"+grade.getMath());

System.out.println("英语成绩:"+grade.getEnglish());

System.out.println("数学,英语总成绩:"+person.getTotleGrade());

}

}package test2;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

import test.ExampleBean;

public class Test {

public static void main(String args[]){

Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径

System.out.println("resource is:" + input);

BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说)

Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你

Grade grade=(Grade)factory.getBean("Grade");

System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样

System.out.println("年龄:"+person.getAge());

System.out.println("数学成绩:"+grade.getMath());

System.out.println("英语成绩:"+grade.getEnglish());

System.out.println("数学,英语总成绩:"+person.getTotleGrade());

}

}

如此看来,你在对比一开始的那个水果的程序,你会发现,spring配置文件,还是一个工厂,只不过换种形式一样,他管理所有的类,新建的类要到工厂里面去登记,不然就不能被主程序用,这就是为什么说ioc就是工厂模式的升级版。至于配置文件的书写,就跟堆积木一样,当然我现在学的还是表面东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值