java软件设计的三层结构

时间:2024-06-30 20:04:29

接口:

package dao;

public interface Dao {
String getData();
}
package biz;

import dao.Dao;

public interface Biz {
void setDao(Dao dao);
String dealData();
}
package view;

import biz.Biz;

public interface View {
void setBiz(Biz biz);
void showData();
}

实现类:

package dao;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class FileDaoImpl implements Dao { @Override
public String getData() {
String data= null;
BufferedReader br= null;
try {
br= new BufferedReader(new FileReader("test.txt"));
data= br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (br!=null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return data;
} }
package biz;

import dao.Dao;

public class UpperCaseBizImpl implements Biz {
private Dao dao;
@Override
public void setDao(Dao dao) {
this.dao= dao;
} @Override
public String dealData() {
String data= dao.getData();
if (data != null) {
data.toUpperCase();
}
return data;
} }
package view;

import biz.Biz;

public class TextViewImpl implements View {
private Biz biz;
@Override
public void setBiz(Biz biz) {
this.biz= biz;
} @Override
public void showData() {
String data= biz.dealData();
System.out.println(data);
} }

Object Factory:

package factory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import biz.Biz; import view.View; import dao.Dao; public class SimpleFactory {
private Properties properties;
public SimpleFactory() {
properties= new Properties();
InputStream is= null;
try {
is= new FileInputStream("conf.properties");
properties.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} public Dao createDao(){
String className = properties.getProperty("dao");
return (Dao)createObject(className);
}
public Biz createBiz(){
String className= properties.getProperty("biz");
return (Biz) createObject(className);
}
public View createView(){
String className= properties.getProperty("view");
return (View) createObject(className);
}
private Object createObject(String name){
Object result= null;
try {
Class clazz = Class.forName(name);
result = clazz.newInstance();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}

配置文件:conf.properties

view=view.TextViewImpl
biz=Biz.UpperCaseBizImpl
dao=Dao.FileDaoImpl

文本文件:test.txt

HELLO WORLD

测试类:

package test;

import view.View;
import biz.Biz;
import dao.Dao;
import factory.SimpleFactory; public class TestMain { /**
* @param args
*/
public static void main(String[] args) {
SimpleFactory simpleFactory= new SimpleFactory();
Dao dao= simpleFactory.createDao();
Biz biz= simpleFactory.createBiz();
biz.setDao(dao);
View view = simpleFactory.createView();
view.setBiz(biz);
view.showData();
} }

output:

HELLO WORLD