factory(工厂) 模式简单示例
来源:互联网 发布:淘宝pc端链接转换 编辑:程序博客网 时间:2024/06/11 06:42
//Printer接口
interface IPrinter {
//接口printByString方法声明
public void printByString(String str);
}
//MyPrinter实现Printer接口
class MyPrinter implements IPrinter {
public void printByString(String str) {
System.out.println(str);
}
}
//IPrinter对象工厂,用于创建实现接口的类对象
class PrinterFactory {
String str = "MyPrinter";//通过字符串寻找实现接口的类,字符串可从文件中读取获得,从而实现IOC模式
//工厂方法,返回IPrinter接口实例
public static IPrinter createPrinter() throws InstantiationException,
ClassNotFoundException, IllegalAccessException {
return (IPrinter) Class.forName(str).newInstance();//返回IF_Printer接口实例
}
}
测试方法
public static void main(String[] args) {
try {
PrinterFactory.createPrinter().printByString("Hello World~!");
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
- factory(工厂) 模式简单示例
- 简单工厂模式(Factory)
- 简单工厂模式(factory)
- 简单工厂(Factory)模式
- 简单工厂模式--Factory
- 简单工厂模式(Simple Factory)
- 简单工厂模式(Simple Factory)
- 简单工厂模式(Simple Factory)
- Simple Factory 简单工厂模式
- 简单工厂模式(Simple Factory)
- 简单工厂模式(simple factory)
- 简单工厂模式/Simple Factory
- Simple Factory (简单工厂模式)
- 简单工厂模式(Simple Factory)
- Simple Factory 简单工厂模式
- 简单工厂( Simple Factory )模式
- 简单工厂模式(Simple Factory)
- 简单工厂模式(Simple Factory)
- 浅谈c++中的拷贝构造函数
- 服务程序C#编写系统服务,注册表读写问题
- 关于Session重大的BUG?
- 试用commons-jexl
- 我的笔记
- factory(工厂) 模式简单示例
- Import Emails from Outlook 2007 into Dynamics AX 2009
- 分页显示数据库东西代码
- C++转向C#的疑惑:难道C#中没有拷贝构造函数 ?
- Firebug快速入门(二)
- 刻录光驱不能访问出现函数不正确问题解决
- Flex 的一些网络学习资料
- telnet 黑老师的 电脑
- BMP 全解