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"的更多相关文章

  1. 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 ...

  2. 设计模式之美:Abstract Factory(抽象工厂)

    索引 别名 意图 结构 参与者 适用性 缺点 效果 相关模式 命名约定 实现 实现方式(一):使用 Factory Method 来实现 Abstract Factory. 实现方式(二):使用 Pr ...

  3. simple factory, factory method, abstract factory

    simple factory good:1 devide implementation and initialization2 use config file can make system more ...

  4. Design Pattern ——Factory Method&Abstract Factory

    今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...

  5. C++设计模式 之 “对象创建”模式:Factory Method、Abstract Factory、Prototype、Builder

    part 0 “对象创建”模式 通过“对象创建” 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式 Fact ...

  6. 抽象工厂(Abstract Factory),工厂方法(Factory Method),单例模式(Singleton Pattern)

    在谈工厂之前,先阐述一个观点:那就是在实际程序设计中,为了设计灵活的多态代码,代码中尽量不使用new去实例化一个对象,那么不使用new去实例化对象,剩下可用的方法就可以选择使用工厂方法,原型复制等去实 ...

  7. 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 ...

  8. 简单工厂,Factory Method(工厂方法)和Abstract Factory(抽象工厂)模式

    对于简单工厂来说,它的工厂只能是这个样子的 public class SimplyFactory {  /** * 静态工厂方法 */ public static Prouct factory(Str ...

  9. 面向对象设计——抽象工厂(Abstract Factory)模式

    定义 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类.抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道或关心实际产出的具体产品是什么.这样一来,客户就能从具体的产 ...

随机推荐

  1. java格式化sql

    在日志分析中,经常会对记录的sql进行分析,所以将一整行sql格式化,进行多行缩就显得很有必要,许多数据库客户端都提供sql的格式化功能,但复杂的多层嵌套sql往往格式化的l还不够友好,所以就自己造了 ...

  2. OpenGL VAO, VBO 使用简介

    参照代码样例: // This function takes in a vertex, color, index and type array // And does the initializati ...

  3. CentOS7 Install Consul

    Centos7 Install Consul 原文链接:http://www.cnblogs.com/caoguo/p/5959962.html 1) 环境 2) 安装 # yum install - ...

  4. IMX6核心板系列解决方案-工业级|商业级|四核|双核|Plus核心板

    i.MX 6Quad四核商业级和工业级系列的应用处理器将可扩展平台与广泛的集成和高能效处理功能相结合,尤其适合多媒体应用.i.MX6 Quad处理器的特性包括: 满足操作系统和游戏的MIPS需求,增强 ...

  5. STL中unique的使用

    作用 unique函数可以删除有序数组中的重复元素,即去重(并不是真正的删除,后面会讲) 定义在头文件<algorithm>中 函数原型 1.只有两个参数,且参数类型都是迭代器: iter ...

  6. ztree 展开一级节点 | ztree只显示到二级目录

    // 默认展开一级节点var nodes = tree.getNodesByParam("level", 0);for (var i = 0; i < nodes.lengt ...

  7. Windows文件自删除的两种方法

    可执行模块的自删除技术已经被讨论的很多, 有很多极富创意的思路和想法被提出, 但有些似是而非的方案往往使人误入歧途. 举个例子来说, 很多文章认为下面的一小段代码可以实现自删除:void main(v ...

  8. [WDS] Warnings while compiling. vue项目运行控制台输出太多警告信息

    vue项目运行控制台输出太多警告信息,我们需要vue 忽略警告,如Expected indentation of 0 spaces but found 2 这种警告都提示,很影响视觉体验!     解 ...

  9. RabbitMQ 基础概念介绍

    AMQP 消息模型 RabbitMQ 是基于 AMQP(高级消息队列协议)的一个开源实现,其内部实际也是 AMQP 的基本概念.

  10. P2347 砝码称重(动态规划递推,背包,洛谷)

    题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...