Java设计模式-工厂方法模式
来源:互联网 发布:员工信息管理java 编辑:程序博客网 时间:2024/06/01 20:31
1:使用场景
在任何需要生产复杂对象的地方,都可以使用工厂方法模式,用new可以完成创建对象无需使用工厂方法模式
2:UML图
3:代码举例
public abstract class Product {public abstract void method(); }
public class ConcreteProductA extends Product{@Overridepublic void method(){System.out.println("我是具体产品A");}}
public class ConcreteProductB extends Product{@Overridepublic void method(){System.out.println("我是具体产品B");}}
public abstract class Factory {public abstract <T extends Product> T createProduct(Class<T> clz); }
public class ConcreteFactory extends Factory{@Overridepublic <T extends Product> T createProduct(Class<T> clz){Product p=null;try{p=(Product)Class.forName(clz.getName()).newInstance();}catch(Exception e){e.printStackTrace();}return (T)p;}}
public class Client {public static void main(String[] args){Factory factory =new ConcreteFactory();Product product=factory.createProduct(ConcreteProductA.class);product.method();}}
多工厂方法模式:
可以为每个产品都定义一个具体工厂,各司其职。
public class ConcreteFactoryA extends Factory{@Overridepublic Product createProduct(){return new ConcreteFactoryA();}}public class ConcreteFactoryB extends Factory{@Overridepublic Product createProduct(){return new ConcreteFactoryB();}}public class Client {public static void main(String[] args){Factory factoryA =new ConcreteFactoryA();Product productA=factoryA.createProduct();productA.method(); Factory factoryB =new ConcreteFactoryB();Product productB=factoryB.createProduct();productB.method();}}
简单工厂模式:
工厂类如果只有一个时,简化工厂方法模式,去掉抽象,将相应的工厂方法改为静态:
public class Factory {@Overridepublic static Product createProduct(){return new ConcreteFactoryA();}}
4:Android中的工厂方法模式
/** * Decode a file path into a bitmap. If the specified file name is null, * or cannot be decoded into a bitmap, the function returns null. * * @param pathName complete path name for the file to be decoded. * @param opts null-ok; Options that control downsampling and whether the * image should be completely decoded, or just is size returned. * @return The decoded bitmap, or null if the image data could not be * decoded, or, if opts is non-null, if opts requested only the * size be returned (in opts.outWidth and opts.outHeight) */ public static Bitmap decodeFile(String pathName, Options opts) { Bitmap bm = null; InputStream stream = null; try { stream = new FileInputStream(pathName); bm = decodeStream(stream, null, opts); } catch (Exception e) { /* do nothing. If the exception happened on open, bm will be null. */ Log.e("BitmapFactory", "Unable to decode stream: " + e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // do nothing here } } } return bm; }
阅读全文
0 0
- java设计模式--工厂方法设计模式
- Java工厂方法模式设计
- Java工厂方法模式设计
- java工厂方法设计模式
- java设计模式-工厂方法
- JAVA-工厂方法设计模式
- 【java】工厂方法设计模式
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- java设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- Java设计模式--工厂模式(简单工厂+工厂方法)
- Java设计模式-工厂模式(简单工厂+工厂方法)
- Struts+Spring+Mybitis整合Demo
- druid 项目的sql执行速度效率监控的配置
- MySQL之数据定义DDL篇
- FPGA时序约束中常用公式推导
- android 中vector drawable使用
- Java设计模式-工厂方法模式
- 动态任务
- GeoWave安装部署实践
- 【小小决斗】我看你不顺眼很久了,快来和我一决高下!
- ACM推荐网站
- 程序的机器级表示
- Myeclipse 2015 stable 1.0 完美破解方法
- 《庄子》阅读感想-逍遥游
- Solr整合Ansj中文分词器