设计模式,
架构设计实例,
使用到了工厂模式、单例模式、反射、泛型等
项目包结构如下图:
1.bean包
(1)Base.java父类
package test.bean;
public class Base {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
(2)Student.java学生类
package test.bean;
public class Student extends Base {
private String schoolbag;
public String getSchoolbag() {
return schoolbag;
}
public void setSchoolbag(String schoolbag) {
this.schoolbag = schoolbag;
}
}
(3)Teacher.java教师类
package test.bean;
public class Teacher extends Base {
private String bag;
public String getBag() {
return bag;
}
public void setBag(String bag) {
this.bag = bag;
}
}
2.factory工厂包
(1)factory.properties
1=test.factory.StudentServiceFactoryImpl
2=test.factory.TeacherServiceFactoryImpl
(2)FactoryUtil.java
package test.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import test.service.IService;
import test.factory.IServiceFactory;
public class FactoryUtil {
//装载配置
private Properties info = new Properties();
//保存各种渠道服务类列表,确保为每个机票服务类只生成一个对象
private Map service = new HashMap();
private volatile static FactoryUtil factoryUtil;
private FactoryUtil(){
init();
}
public void init(){
InputStream is = null;
try{
is=
FactoryUtil.class.getResourceAsStream("factory.properties");
info.load(is);
}catch(Exception e){
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static FactoryUtil getInstance(){
if(factoryUtil==null){
synchronized(FactoryUtil.class){
if(factoryUtil==null){
factoryUtil = new FactoryUtil();
}
}
}
return factoryUtil;
}
public IService getService(Integer patten){
String mode = String.valueOf(patten);
//如果该map已经里包含了该service就不需要再通过下面的反射再获取了
if(service.containsKey(mode)){
return service.get(mode);
}
String classPath = info.getProperty(mode);
try {
Object obj = Class.forName(classPath).newInstance();
IServiceFactory factory = (IServiceFactory)obj;
IService iservice = factory.createService();
service.put(mode, iservice);
return iservice;
} catch (ClassNotFoundException e) {
throw new RuntimeException("没有找到与“"+mode+"”对应的工厂实现类",e);
} catch (InstantiationException e) {
throw new RuntimeException("没有找到与“"+mode+"”对应的工厂实现类",e);
} catch (IllegalAccessException e) {
throw new RuntimeException("没有找到与“"+mode+"”对应的工厂实现类",e);
}
}
}
(3)IServiceFactory.java
package test.factory;
import test.service.IService;
public interface IServiceFactory {
public IService createService();
}
(4)StudentServiceFactoryImpl.java
package test.factory;
import test.service.IService;
import test.service.StudentServiceImpl;
public class StudentServiceFactoryImpl implements
IServiceFactory {
public IService createService() {
// TODO Auto-generated method stub
return new StudentServiceImpl();
}
}
(5)TeacherServiceFactoryImpl.java
package test.factory;
import test.service.IService;
import test.service.TeacherServiceImpl;;
public class TeacherServiceFactoryImpl implements
IServiceFactory {
public IService createService() {
// TODO Auto-generated method stub
return new TeacherServiceImpl();
}
}
3.service包
(1)IService.java
package test.service;
public interface IService {
public T doWork(String request);
public T getPerson(String request);
public void eat();
}
(2)StudentServiceImpl.java
package test.service;
import test.bean.Student;
public class StudentServiceImpl implements IService {
public String doWork(String request) {
// TODO Auto-generated method stub
return "工作:学生做"+request+"作业";
}
public Student getPerson(String request) {
// TODO Auto-generated method stub
Student student = new Student();
student.setName("小明");
student.setAge("8");
student.setSchoolbag("喜羊羊牌书包");
return student;
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃饭:学生吃饭");
}
}
(3)TeacherServiceImpl.java
package test.service;
import test.bean.Teacher;
public class TeacherServiceImpl implements IService {
public String doWork(String request) {
// TODO Auto-generated method stub
return "工作:老师批改"+request+"作业";
}
public Teacher getPerson(String request){
Teacher teacher = new Teacher();
teacher.setName("陈老师");
teacher.setAge("28");
teacher.setBag("LV男士皮包");
return teacher;
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃饭:老师吃饭");
}
}
4.Test测试包
Test.java
package test;
import test.bean.Student;
import test.factory.FactoryUtil;
import test.service.IService;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
IService iservice = FactoryUtil.getInstance().getService(1);//1 学生
2老师
//1.通过泛型获得对象Object然后强转为想要的类型
String returnStr = (String)iservice.doWork("数学");
System.out.println(returnStr);//打印内容:工作:学生做数学作业
//2.同上,通过泛型获得对象Object然后强转为想要的类型
Student student = (Student)iservice.getPerson("喜洋洋");
System.out.println(student.getName()+",年龄是"+student.getAge()+"岁,书包是"+student.getSchoolbag());//打印内容:小明,年龄是8,书包是喜羊羊牌书包
//3.直接调接口的eat()方法
iservice.eat();//打印内容:吃饭:学生吃饭
}
}
注:有问题希望大家指出,原创,转载请注明出处。