工厂模式--java

工厂模式细分有三种,分别为:
简单工厂模式
工厂方法模式
抽象工厂模式
现单个的讲,最后再讲这三个的区别,这篇文章主要通过一个农场的实例来讲解,这也是java与模式书中的例子,
只不过我对一些部分进行了简化,一些部分进行了扩充,以帮助理解例子如下:
有一个农场公司,专门向市场销售各类水果有如下水果:
葡萄(grape)
草莓(strawberry)
苹果(apple)

简单工厂模式:

这个比较简单,写一下源代码源代码中给出了必须的注释代码比书上的要
简单一些,排版也好看一些,只是为了让新手更好的理解
Fruit.java:
public interface Fruit
{
/**
* 水果与其它植物相比有一些专门的属性,以便与农场的
* 其它植物区分开这里的水果假设它必须具备的方法:
* 生长grow()收获harvest()种植plant()
*/
void grow();
void harvest();
void plant();
}


下面是Apple类的函数Apple.java:
public class Apple implements Fruit
{
/** 
* 苹果是水果类的一种,因此它必须实现水果接口的所有方法即
* grow()harvest()plant()三个函数另外,由于苹果是多年生植物,
* 所以多出一个treeAge性质,描述苹果的树龄
*/

private int treeAge;

public void grow() { //苹果的生长函数代码 }
public void harvest() { //苹果的收获函数代码 }
public void plant() { //苹果的种植函数代码 }

public int getTreeAge() { return treeAge; }
public void setTreeAge(int treeAge) { this.treeAge = treeAge; }
}


下面是Grape类的函数Grape.java:
public class Grape implements Fruit
{
/** 
* 葡萄是水果类的一种,因此它必须实现水果接口的所有方法即
* grow()harvest()plant()三个函数另外,由于葡萄分为有籽和无籽
* 两种,因此多出一个seedless性质,描述葡萄有籽还是无籽
*/
private boolean seedless;

public void grow() { //葡萄的生长函数代码 }
public void harvest() { //葡萄的收获函数代码 }
public void plant() { //葡萄的种植函数代码 }

public boolean getSeedless() { return seedless; }
public void setSeedless(boolean seedless) { this.seedless = seedless; }

}


下面是Strawberry类的函数Strawberry.java:
public class Strawberry implements Fruit
{
/** 
* 草莓是水果类的一种,因此它必须实现水果接口的所有方法即
* grow()harvest()plant()三个函数另外,这里假设草莓分为大棚草莓和一般
* 草莓(即没有棚的草莓)因此草莓比一般水果多出一个性质coteless,描述草莓
* 是大棚草莓还是没有大棚的草莓
*/

private boolean coteless;
public void grow() { //草莓的生长函数代码 }
public void harvest() { //草莓的收获函数代码 }
public void plant() { //草莓的种植函数代码 }
public boolean getCoteless() { return coteless; }
public void setCoteless(boolean coteless) { this. coteless = coteless; }

}


农场的园丁也是系统的一部分,自然要有一个合适的类来代表,我们用FruitGardener类
来表示FruitGardener类会根据客户端的要求,创建出不同的水果对象,比如苹果(apple),
葡萄(grape)或草莓(strawberry)的实例代码如下所示:
FruitGardener.java:
public class FruitGardener
{
/**
* 通过下面的表态工厂方法,可以根据客户的需要,创建出不同的水果对象
* 如果提供的参数是apple则通过return new Apple()创建出苹果实例
* 如果是提供的参数是grape则创建葡萄实例,这正是简单工厂方法之精髓
*/
public static Fruit factory(String which) throws BadFruitException
{
if (which.equalsIgnoreCase("apple")) { return new Apple(); }
else if (which.equalsIgnoreCase("strawberry")) { return new Strawberry(); }
else if (which.equalsIgnoreCase("grape")) { return new Grape(); }
else { throw new BadFruitException("Bad fruit request"); }
}
}


简单工厂方法的优点是当在系统中引入新产品时不必修改客户端,但需要个修改工厂
类,将必要的逻辑加入到工厂类中工厂方法模式就克服了以上缺点,下面谈谈工厂 方法模式

工厂方法模式:
由于水果接口以及grape类strawberry类apple类的代码都和上面的一样,所以下面相关的源码去掉了注释
Fruit.java:
public interface Fruit
{
void grow();
void harvest();
void plant();
}

Apple.java:
public class Apple implements Fruit
{
private int treeAge;
public void grow() { //苹果的生长函数代码 }
public void harvest() { //苹果的收获函数代码 }
public void plant() { //苹果的种植函数代码 }
public int getTreeAge() { return treeAge; }
public void setTreeAge(int treeAge) { this.treeAge = treeAge; }
}

Grape.java:
public class Grape implements Fruit
{
private boolean seedless;

public void grow() { //葡萄的生长函数代码 }
public void harvest() { //葡萄的收获函数代码 }
public void plant() { //葡萄的种植函数代码 }

public boolean getSeedless() { return seedless; }
public void setSeedless(boolean seedless) { this.seedless = seedless; }
}

Strawberry.java:
public class Strawberry implements Fruit
{
private boolean coteless;
public void grow() { //草莓的生长函数代码 }
public void harvest() { //草莓的收获函数代码 }
public void plant() { //草莓的种植函数代码 }
public boolean getCoteless() { return coteless; }
public void setCoteless(boolean coteless) { this. coteless = coteless; }

}



下面的源码就是工厂方法模式的重点了,在简单工厂模式中,将这里将FruitGardener定义为一个类,
即园丁要管理园里的所有水果,如果园丁哪天病了,水果都不能管理了在工厂方法模式中将FruitGardener定义为一个接口,
而将管理水果的角色划分得更细,比如有葡萄园丁草莓园丁苹果园丁等等具体角色实现FruitGardener接口的工厂方法源码如下所示:
接口FruitGardener的源码:
public interface FruitGardener
{
Fruit factory();
}



苹果园丁类AppleGardener.java的源码:
public class AppleGardener implements FruitGardener
{
public Fruit factory() { return new Apple(); }
}


葡萄园丁类GrapeGardener.java的源码:
public class GrapeGardener implements FruitGardener
{
public Fruit factory() { return new Grape(); }
}



草莓园丁类StrawberryGardener.java的源码:
public class StrawberryGardener implements FruitGardener
{
public Fruit factory() { return new Strawberry(); }
}

由以上源码可以看出,使用工厂方法模式保持了简单工厂模式的优点,克服了其缺点当在系统中引入新产品时,
既不必修改客户端,又不必修改具体工厂角色可以较好的对系统进行扩展

抽象工厂模式:
现在工厂再次大发展,要引进塑料大棚技术,在大棚里种植热带(Tropical)和亚热带的水果和蔬菜(Veggie)
其中水果分为TropicalFruit和NorthernFruit,蔬菜分为TropicalVeggie
和NorthernVeggie园丁包括TropicalGardener和NorthernGardener也就是说,
TropicalGardener专门管理TropicalFruit和TropicalGardener,NorthernGardener专门
管理NorthernFruit和NorthernVeggie抽象工厂模式在这个例子中的源码如下所示:
Fruit.java:
public interface Fruit { }

NorthernFruit.java:
public class NorthernFruit implements Fruit
{
private String name;
public NorthernFruit(String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

TropicalFruit.java:
public class TropicalFruit implements Fruit
{
private String name;
public TropicalFruit(String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

Veggie.java:
public interface Veggie { }

TropicalVeggie.java:
public class TropicalVeggie implements Veggie
{
private String name;
public TropicalVeggie(String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

NorthernVeggie.java:
public class NorthernVeggie implements Veggie
{
private String name;
public NorthernVeggie(String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

Gardener.java:
public interface Gardener
{
Fruit createFruit(String name);
Veggie createVeggie(String name);
}

TropicalGardener.java:
public class TropicalGardener implements Gardener
{
public Fruit createFruit(String name) { return new TropicalFruit(name); }
public Veggie createVeggie(String name) { return new TropicalVeggie(name); }
}

NorthernGardener.java:
public class NorthernGardener implements Gardener
{
public Fruit createFruit(String name) { return new NorthernFruit(name); }
public Veggie createVeggie(String name) { return new NorthernVeggie(name); }
}


为了简单起见,这里只讲一下增加新产品(族)时该系统如何扩展(关于产品族相关
知识,请看此书的相关章节,不过不懂产品族也没有关系,这里写得很简单,肯定能
看懂)比如现在要增加南方水果(SouthFruit)和南方蔬菜(SouthVeggie)那
么只要增加如下代码即可很容易的扩展:
SouthFruit.java:
public class SouthFruit implements Fruit
{
private String name;
public SouthFruit (String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

SouthVeggie.java:
public class SouthVeggie implements Veggie
{
private String name;
public SouthVeggie (String name) { }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

SouthGardener.java:
public class SouthGardener implements Gardener
{
public Fruit createFruit(String name) { return new SouthFruit(name); }
public Veggie createVeggie(String name) { return new SouthVeggie(name); }
}

五、小结

工厂方法模式仿佛已经很完美的对对象的创建进行了包装,使得客户程序中仅仅处理抽象产品角色提供的接口。那我们是否一定要在代码中遍布工厂呢?大可不必。也许在下面情况下你可以考虑使用工厂方法模式:

1)当客户程序不需要知道要使用对象的创建过程。

2)客户程序使用的对象存在变动的可能,或者根本就不知道使用哪一个具体的对象。

简单工厂模式与工厂方法模式真正的避免了代码的改动了?没有。在简单工厂模式中,新产品的加入要修改工厂角色中的判断语句;而在工厂方法模式中,要么将判断逻辑留在抽象工厂角色中,要么在客户程序中将具体工厂角色写死(就象上面的例子一样)。而且产品对象创建条件的改变必然会引起工厂角色的修改。
面对这种情况,Java的反射机制与配置文件的巧妙结合突破了限制——这在Spring中完美的体现了出来。
六、抽象工厂模式

先来认识下什么是产品族: 位于不同产品等级结构中,功能相关联的产品组成的家族。还是让我们用一个例子来形象地说明一下吧。
回到抽象工厂模式的话题上。
可以说,抽象工厂模式和工厂方法模式的区别就在于需要创建对象的复杂程度上。而且抽象工厂模式是三个里面最为抽象、最具一般性的。
抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象

而且使用抽象工厂模式还要满足一下条件:
1)系统中有多个产品族,而系统一次只可能消费其中一族产品。
2)同属于同一个产品族的产品以其使用。
来看看抽象工厂模式的各个角色(和工厂方法的如出一辙):
1)抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在java中它由抽象类或者接口来实现。

2)具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。在java中它由具体的类来实现。

3)抽象产品角色:它是具体产品继承的父类或者是实现的接口。在java中一般有抽象类或者接口来实现。

4)具体产品角色:具体工厂角色所创建的对象就是此角色的实例。在java中由具体的类来实现。
看过了前两个模式,对这个模式各个角色之间的协调情况应该心里有个数了,我就不举具体的例子了。只是一定要注意满足使用抽象工厂模式的条件哦。
//
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。

抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。
//
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计题目:农夫果园 一个农场,专门种植销售各类水果,在这个系统中需要描述下列水果: 葡萄:Grape 草莓:Strawberry 苹果:Apple 水果与其他的植物有很大的不同,水果最终是可以采摘食用的。那么一个自然的做法就是建立一个各种水果都适用的接口,以便与农场里的其他植物区分开。水果接口规定出所有的水果都必须实现的接口,包括任何水果必须具备的方法:种植plant(),生长grow(),收获harvest()。 Apple类是水果中的一种,因此它实现了水果接口所声明的所有方法。另外,由于苹果是多年生植物,因此多出一个treeAge性质,描述苹果树的树龄。 Grape类是水果类的一种,也实现Fruit接口中所声明的所有方法。但由于葡萄分为有籽和无籽的两种,因此比通常的水果多出一个seedless性质。 Strawberry类也是水果的一种,也实现了Fruit接口。 农场的园丁也是系统的一部分,自然要由一个合适的类来代表。这个类就是FruitGardener,它会根据农场老板的要求,使用factory()方法创建出不同的水果对象,比如苹果(Apple),葡萄(Grape)或草莓(Strawberry)的实例。而如果接到不合法的要求,会提示错误。 农场的市场调查员也是系统的一部分,也需要一个类代表,这个类是MarketInquirer,它通过inquire()调查今年市场上哪一种水果热销。 农场的老板也是系统的一部分,仍需要一个类来代表,这个类是FruitBoss,他会根据市场调查员的反馈信息,通知农场的园丁今年种植哪种水果。 要求:请你根据上述系统需求,用Java语言设计这个农场系统,发挥你的想象力吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值