反射结合工厂设计模式的使用
2017-03-22 14:53
501 查看
package Hg
//反射结合工厂设计模式的使用
pbulic class FactoryDEMO
public static void void main(String[] args){
Fruit Fruit = Factory.getFruit("Hg.Apple");
fruit.eat();
}
}
//接口
intterface Fruit{
void eat();
}
//实现类
class Apple implements Fruit{
@Override
public void eat(){
System.out.println("吃苹果");
}
}
class pinappale implements Fruit{
@Override
public void eat(){
System.out.println("吃菠萝")
}
}
//工厂类
class
Factory{
public static Fruit getFruit(String className){
Fruit f = null;
try{
Class<?> cls = Class.forhome(className);
f = (Fruit)cls.newinstance();
}catch(ClassNotFoundException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}
//反射结合工厂设计模式的使用
pbulic class FactoryDEMO
public static void void main(String[] args){
Fruit Fruit = Factory.getFruit("Hg.Apple");
fruit.eat();
}
}
//接口
intterface Fruit{
void eat();
}
//实现类
class Apple implements Fruit{
@Override
public void eat(){
System.out.println("吃苹果");
}
}
class pinappale implements Fruit{
@Override
public void eat(){
System.out.println("吃菠萝")
}
}
//工厂类
class
Factory{
public static Fruit getFruit(String className){
Fruit f = null;
try{
Class<?> cls = Class.forhome(className);
f = (Fruit)cls.newinstance();
}catch(ClassNotFoundException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}
相关文章推荐
- 反射机制和工厂设计模式结合使用
- 扩展无极限:工厂设计模式与C#扩展方法的结合使用
- Java反射机制(五):使用反射增强简单工厂设计模式
- 【设计模式】工厂模式结合反射技术的简单应用
- 反射机制与配置文件结合的工厂设计模式
- 反射机制和工厂设计模式的使用
- java基础第九讲——反射、工厂设计模式、注解、线程、网络与线程的结合
- java 设计模式之工厂模式与反射的结合
- java 反射和 工厂模式结合使用
- Java反射机制(五):使用反射增强简单工厂设计模式
- 设计模式-----基于反射工厂模式
- 设计模式之简单工厂模式加反射
- 学习设计模式之结合简单工厂,策略,装饰的计算器
- 设计模式(1)-使用简单工厂优化代码
- 结合项目实例 回顾传统设计模式(四)工厂模式(简单工厂、普通工厂、抽象工厂)
- 设计模式(1)-使用简单工厂优化代码(转载)
- .Net反射工厂设计模式
- 使用了继承、多态还有工厂模式和反射,但是还是没有OO的感觉。[已经增加了实现的代码]
- 设计模式实战之--工厂模式结合适配器模式
- 增强可读性,易用性——设计模式之建造者模式(灵活使用继承和抽象类以及抽象函数和反射)