"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)模式
定义 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类.抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道或关心实际产出的具体产品是什么.这样一来,客户就能从具体的产 ...
随机推荐
- H.264学习笔记6——指数哥伦布编码
一.哥伦布码 哥伦布码就是将编码对象分能成等间隔的若干区间(Group),每个Group有一个索引值:Group Id. >对于Group Id采用二元码编码: >对于Group内的编码对 ...
- windows保存tomcat的控制台日志到文件
startup.bat修改:call "%EXECUTABLE%" start %CMD_LINE_ARGS%改为:call "%EXECUTABLE%" ru ...
- webpack3整理(第一节/满三节)
一.css文件打包到js中(loader的三种写法) //第一种写法:直接用use. module: { rules: [{ test: /\.css$/, use: ['style-loader', ...
- PHP会话控制考察点
为什么要使用会话控制技术 HTTP协议是无状态的,也就是说HTTP没有一个内建的机制来维护两个事务之间的状态.当一个用户完成一个请求发起第二个请求的时候,服务器无法知道这次请求是来自于上一次的客户.而 ...
- swift 待研备份
https://www.ctolib.com/topics-115290.html 但是还是用到了一个叫做 The Protocol Witness Table (PWT) 的函数表 https:// ...
- MySQL for Mac 终端操作说明
mysql for mac 终端操作说明MySQL服务开启Mac版mysql可以从设置里启动服务: 如果想要在终端(Terminal)中操作mysql,需要先添加mysql路径,在此以zsh为例: # ...
- laravel模块 目录设计
- sh NonUniqueObjectException
话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...
- CAD参数绘制多段线(网页版)
多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定 ...
- 拦截导弹问题(Noip1999)
1322:[例6.4]拦截导弹问题(Noip1999) 时间限制: 1000 ms 内存限制: 65536 KB提交数: 3843 通过数: 1373 [题目描述] 某国为了防 ...