著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统能够说是随处可见。
由于工厂模式就相当于创建实例对象的new。我们经常要依据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时就要多个心眼,能否够考虑使用工厂模式,尽管这样做,可能多做一些工作,但会给你系统带来更大的可扩展性和尽量少的改动量。
在本例中,首先。我们须要将创建实例的工作与使用实例的工作分开, 也就是说,让创建实例所须要的大量初始化工作从Sample的构造函数中分离出去。
这时我们就须要Factory工厂模式来生成对象了。不能再用上面简单new Sample(參数)。还有,假设Sample有个继承如MySample, 依照面向接口编程,我们须要将Sample抽象成一个接口.ISample是接口,有两个子类MySample 和HisSample .我们要实例化他们时,例如以下:
ISample mysample=new MySample();
ISample hissample=new HisSample();
随着项目的深入,Sample可能还会"生出非常多儿子出来", 那么我们要对这些儿子一个个实例化,更糟糕的是,可能还要对曾经的代码进行改动:增加后来生出儿子的实例.这在传统程序中是无法避免的.
但假设你一開始就有意识使用了工厂模式,这些麻烦就没有了.
public class Factory{
public static ISample creator(int which){
if (which==1)
return new SampleA();
else if (which==2)
return new SampleB();
}
}那么在你的程序中,假设要创建ISample的实列时候能够使用假设我们创建对象的方法变得复杂了,如上面工厂方法中是创建一个对象Sample,假设我们还有新的产品接口Sample2.
这里如果:Sample有两个实体类SampleA和SampleB。而Sample2也有两个实体类Sample2A和Sample2B
那么。我们就将上例中Factory变成抽象类,将共同部分封装在抽象类中,不同部分使用子类实现。以下就是将上例中的Factory拓展成抽象工厂:
public abstract class Factory{
public abstract Sample creator();
public abstract Sample2 creator(String name);
}
public class SimpleFactory extends Factory{
public Sample creator(){
.........
return new SampleA
}
public Sample2 creator(String name){
.........
return new Sample2A
}
}
public class BombFactory extends Factory{
public Sample creator(){
......
return new SampleB
}
public Sample2 creator(String name){
......
return new Sample2B
}
}
People 类、PeopleA类、PeopleB类
public interface People {
}
public class PeopleA implements People{
public PeopleA() {
System.out.println("我是人类A亚当");
}
}
public class PeopleB implements People{
public PeopleB() {
System.out.println("我是人类B夏娃");
}
}God上帝类
public class God {
public People createPeople(int x){
switch (x) {
case 1:
return new PeopleA();
case 2:
return new PeopleB();
}
return null;
}
}主类
public class Client {
public static void main(String[] args) {
God god=new God();
PeopleA peopleA=(PeopleA) god.createPeople(1);
PeopleB peopleB=(PeopleB) god.createPeople(2);
}
}
God 能够依据传入的数字来创造人类,可要是人类种类变多了怎么办?又要写case 3 ,创造多一个PeopleC。。。显然是不符合开闭原则的(不清楚百度下哈)
所以就有了以下的工厂方法模式。God找来两个天使GodA和GodB来帮忙造人了。这样当须要新增人种时,God再找一个GodC来造人就好了,自己不用再做太多的杂事了。oh,God太聪明了。
请看以下工厂方法模式
输出结果为:我是人类A亚当
我是人类B夏娃
public interface People {
}
public class PeopleA implements People{
public PeopleA() {
System.out.println("我是人类A亚当");
}
}
public class PeopleB implements People{
public PeopleB() {
System.out.println("我是人类B夏娃");
}
}public interface God {
public People createPeople();
}
public class GodA implements God{
public People createPeople(){
return new PeopleA();
}
}
public class GodB implements God{
public People createPeople(){
return new PeopleB();
}
}public class Client {
public static void main(String[] args) {
God godA=new GodA();
People peopleA=godA.createPeople();
God godB=new GodB();
People peopleB=godB.createPeople();
}
}
输出结果为:
我是人类A亚当
我是人类B夏娃
有一天,天使A和天使B发现人们没东西遮羞哎。这时他们就開始造衣服裤子了,天使A開始造人类A(男人)的衣服裤子,天使B開始造人类B(女人)的衣服裤子。
(能够理解为天使A和天使B是两间不同的工厂,分别制造跟要造的对象(人类)相关的物品,这简直了,一站式服务,造车还顺便造这辆车须要的一些零件)。
public interface God {
public People createPeople();
public Yifu createYifu();
public Kuzi createKuzi();
}
public class GodA implements God{
public People createPeople(){
return new PeopleA();
}
@Override
public Yifu createYifu() {
return new ManYifu();
}
@Override
public Kuzi createKuzi() {
return new ManKuzi();
}
}
public class GodB implements God{
public People createPeople(){
return new PeopleB();
}
@Override
public Yifu createYifu() {
return new WomanYifu();
}
@Override
public Kuzi createKuzi() {
return new WomanKuzi();
}
}public interface Yifu {
public void getYifu();
}
public class ManYifu implements Yifu{
@Override
public void getYifu() {
System.out.println("我穿男人衣服");
}
}
public class WomanYifu implements Yifu{
@Override
public void getYifu() {
System.out.println("我穿女人衣服");
}
}public interface Kuzi {
public void getKuzi();
}
public class ManKuzi implements Kuzi{
@Override
public void getKuzi() {
System.out.println("我穿男人裤子");
}
}
public class WomanKuzi implements Kuzi{
@Override
public void getKuzi() {
System.out.println("我穿女人裤子");
}
}public class Client {
public static void main(String[] args) {
God godA=new GodA();
People peopleA=godA.createPeople();
Yifu manYifu=godA.createYifu();
Kuzi manKuzi=godA.createKuzi();
manYifu.getYifu();
manKuzi.getKuzi();
God godB=new GodB();
People peopleB=godB.createPeople();
Yifu womanYifu=godB.createYifu();
Kuzi womanKuzi=godB.createKuzi();
womanYifu.getYifu();
womanKuzi.getKuzi();
}
}
还不懂?没关系
看看以下的链接,你应该就几乎相同懂了,假设还不懂?来。敲我脑袋
http://blog.csdn.net/jason0539/article/details/23020989
http://www.cnblogs.com/devinzhang/archive/2011/12/19/2293160.html
反正你也敲不着。哈哈
原文:http://www.cnblogs.com/yxysuanfa/p/7096531.html