"Simple Factory" vs "Factory Method" vs "Abstract Factory" vs "Reflect"
ref: http://www.cnblogs.com/zhangchenliang/p/3700820.html
1. "Simple Factory"
package torv.abc; public class HelloMain { public static void main(String[] args) {
Driver.createCar("audi").drive();
}
} //====================================================== product car abstract class Car {
private String brand; public abstract void drive(); public String getBrand() {
return brand;
} public void setBrand(String brandName) {
brand = brandName;
}
} //------------------------------------------------benz
class BenzCar extends Car {
public BenzCar() {
setBrand("Benz");
} @Override
public void drive() {
System.out.println(getBrand() + "--go-----");
}
} //------------------------------------------------bmw
class BmwCar extends Car {
public BmwCar() {
setBrand("Bmw");
} @Override
public void drive() {
System.out.println(getBrand() + "--go-----");
}
} //------------------------------------------------audi
class AudiCar extends Car {
public AudiCar() {
setBrand("Audi");
} @Override
public void drive() {
System.out.println(getBrand() + "--go-----");
}
} //====================================================== factory class Driver {
public static Car createCar(String carName) {
Car car = null; if (carName.equalsIgnoreCase("benz")) {
car = new BenzCar();
} else if (carName.equalsIgnoreCase("bmw")) {
car = new BmwCar();
} else if (carName.equalsIgnoreCase("audi")) {
car = new AudiCar();
} return car;
}
}
2. "Factory Method"
package torv.abc; public class HelloMain { public static void main(String[] args) {
Driver driver = new BmwDriver();
driver.createCar().drive();
}
} //====================================================== product car abstract class Car {
private String brand; public abstract void drive(); public String getBrand() {
return brand;
} public void setBrand(String brandName) {
brand = brandName;
}
} //------------------------------------------------benz
class BenzCar extends Car {
public BenzCar() {
setBrand("Benz");
} @Override
public void drive() {
System.out.println(getBrand()+"--go-----");
}
} //------------------------------------------------bmw
class BmwCar extends Car {
public BmwCar() {
setBrand("Bmw");
} @Override
public void drive() {
System.out.println(getBrand()+"--go-----");
}
} //------------------------------------------------audi
class AudiCar extends Car {
public AudiCar() {
setBrand("Audi");
} @Override
public void drive() {
System.out.println(getBrand()+"--go-----");
}
} //====================================================== factory abstract class Driver {
public abstract Car createCar();
} class BenzDriver extends Driver{ @Override
public Car createCar() {
return new BenzCar();
} } class BmwDriver extends Driver{ @Override
public Car createCar() {
return new BmwCar();
} } class AudiDriver extends Driver{ @Override
public Car createCar() {
return new AudiCar();
} }
3. "Abstract Factory"
package torv.abc; public class HelloMain { public static void main(String[] args) {
Driver driver = new SportDriver();
driver.createAudiCar().drive();
}
} //====================================================== product car abstract class Car {
private String brand;
private String kind; public abstract void drive(); public String getBrand() {
return brand;
} public void setBrand(String brandName) {
brand = brandName;
} public String getKind() {
return kind;
} public void setKind(String kindName) {
kind = kindName;
}
} //------------------------------------------------benz
abstract class BenzCar extends Car {
public BenzCar() {
setBrand("Benz");
}
} class BenzSportCar extends BenzCar { public BenzSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class BenzBizCar extends BenzCar { public BenzBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //------------------------------------------------bmw
abstract class BmwCar extends Car {
public BmwCar() {
setBrand("Bmw");
}
} class BmwSportCar extends BmwCar { public BmwSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class BmwBizCar extends BmwCar { public BmwBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //------------------------------------------------audi
abstract class AudiCar extends Car {
public AudiCar() {
setBrand("Audi");
}
} class AudiSportCar extends AudiCar { public AudiSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class AudiBizCar extends AudiCar { public AudiBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //====================================================== factory abstract class Driver {
public abstract Car createBenzCar(); public abstract Car createBmwCar(); public abstract Car createAudiCar();
} class SportDriver extends Driver { @Override
public Car createBenzCar() {
return new BenzSportCar();
} @Override
public Car createBmwCar() {
return new BmwSportCar();
} @Override
public Car createAudiCar() {
return new AudiSportCar();
}
} class BizDriver extends Driver { @Override
public Car createBenzCar() {
return new BenzBizCar();
} @Override
public Car createBmwCar() {
return new BmwBizCar();
} @Override
public Car createAudiCar() {
return new AudiBizCar();
} }
3. "Simple Factory" + Reflect
package torv.abc; public class HelloMain { public static void main(String[] args) {
try {
Driver.createCar("torv.abc.BenzSportCar").drive();
} catch (Exception e) {
e.printStackTrace();
}
}
} //====================================================== product car abstract class Car {
private String brand;
private String kind; public abstract void drive(); public String getBrand() {
return brand;
} public void setBrand(String brandName) {
brand = brandName;
} public String getKind() {
return kind;
} public void setKind(String kindName) {
kind = kindName;
}
} //------------------------------------------------benz
abstract class BenzCar extends Car {
public BenzCar() {
setBrand("Benz");
}
} class BenzSportCar extends BenzCar { public BenzSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class BenzBizCar extends BenzCar { public BenzBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //------------------------------------------------bmw
abstract class BmwCar extends Car {
public BmwCar() {
setBrand("Bmw");
}
} class BmwSportCar extends BmwCar { public BmwSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class BmwBizCar extends BmwCar { public BmwBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //------------------------------------------------audi
abstract class AudiCar extends Car {
public AudiCar() {
setBrand("Audi");
}
} class AudiSportCar extends AudiCar { public AudiSportCar() {
setKind("Sport");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } class AudiBizCar extends AudiCar { public AudiBizCar() {
setKind("Biz");
} @Override
public void drive() {
System.out.println(getBrand() + "--" + getKind() + "--go-------");
} } //====================================================== factory abstract class Driver {
public static Car createCar(String name) throws Exception{
return (Car) Class.forName(name).newInstance();
}
}
"Simple Factory" vs "Factory Method" vs "Abstract Factory" vs "Reflect"的更多相关文章
- Simple Factory vs. Factory Method vs. Abstract Factory【简单工厂,工厂方法以及抽象工厂的比较】
I ran into a question on stackoverflow the other day that sort of shocked me. It was a piece of code ...
- 设计模式之美:Abstract Factory(抽象工厂)
索引 别名 意图 结构 参与者 适用性 缺点 效果 相关模式 命名约定 实现 实现方式(一):使用 Factory Method 来实现 Abstract Factory. 实现方式(二):使用 Pr ...
- simple factory, factory method, abstract factory
simple factory good:1 devide implementation and initialization2 use config file can make system more ...
- Design Pattern ——Factory Method&Abstract Factory
今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...
- C++设计模式 之 “对象创建”模式:Factory Method、Abstract Factory、Prototype、Builder
part 0 “对象创建”模式 通过“对象创建” 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式 Fact ...
- 抽象工厂(Abstract Factory),工厂方法(Factory Method),单例模式(Singleton Pattern)
在谈工厂之前,先阐述一个观点:那就是在实际程序设计中,为了设计灵活的多态代码,代码中尽量不使用new去实例化一个对象,那么不使用new去实例化对象,剩下可用的方法就可以选择使用工厂方法,原型复制等去实 ...
- spring3+structs2整合hibernate4时报org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void sy.dao.impl.UserDaoImpl.setSessionFactory(org.hibernate.SessionFactory);
今天在spring3+structs2整合hibernate4时报如下错误,一直找不到原因: org.springframework.beans.factory.BeanCreationExcepti ...
- 简单工厂,Factory Method(工厂方法)和Abstract Factory(抽象工厂)模式
对于简单工厂来说,它的工厂只能是这个样子的 public class SimplyFactory { /** * 静态工厂方法 */ public static Prouct factory(Str ...
- 面向对象设计——抽象工厂(Abstract Factory)模式
定义 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类.抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道或关心实际产出的具体产品是什么.这样一来,客户就能从具体的产 ...
随机推荐
- ORM-PetaPoco
PetaPoco有以下特色:--------------------------20170715姜彦 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的Poco类, ...
- PHP正则表达式考察点
正则表达式的作用 分隔.查找.匹配.替换字符串 正则表达式的组成部分 分隔符 "/" . "#" . "~" 通用原子 \d : 十进制的0 ...
- 未来IT行业的掌控者
(题外话,我发现很多高手都喜欢讲代码实现,喜欢贴代码贴图,我个人不大喜欢这种方式,我觉得最重要的是思想,是想法,具体的实现代码实现步骤由读者自己去实现.这纯属我个人喜好,望各大内高手勿喷,可能是本人水 ...
- Android(java)学习笔记203:JNI之NDK开发步骤
1. NDK开发步骤(回忆一下HelloWorld案例): (1)创建工程 (2)定义native方法 (3)创建jni文件夹 (4)创建c源文件放到jni文件夹 (5)拷贝jni.h头文件到jni目 ...
- scrapy 请求传参
class MovieSpider(scrapy.Spider): name = 'movie' allowed_domains = ['www.id97.com'] start_urls = ['h ...
- 拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))
由于我添加了一个一般处理程序,再运行就出现报错. 解决方法是: 运行dcomcnfg 点组件服务->服务->电脑->我的电脑->DCOM 配置 找到“Windows M ...
- 解决vue项目eslint校验 Do not use 'new' for side effects 的两种方法
import Vue from 'vue' import App from './App.vue' import router from './router' new Vue({ el: '#app' ...
- java混淆工具Jocky和Proguard
java混淆工具有很多种,这里介绍Jocky和Proguard 一:Jocky是金蝶中间件技术领袖袁红岗先生的个人作品(旧有名称JOC).原本是方便Apusic 应用服务器的开发,现在开放出来,供大家 ...
- 解决WCF接口无法传递object参数的问题
在某些场合中,我们需要提供以object为参数的方法.不过在WCF中,由于需要序列化与反序列化,因此它要求所有WCF传递的参数类型都是已知的,无法传递object这种未知类型.即使用了KnownTyp ...
- 新引入thinkphp报错“应用目录[./Application/]不可写,目录无法自动生成! 请手动生成项目目录~”
新引入thinkphp报错“应用目录[./Application/]不可写,目录无法自动生成! 请手动生成项目目录~”, 其主要原因是文件夹的权限问题,手动将项目文件夹权限更改为可读可写就OK,具体操 ...