[b][size=x-large]简单工厂模式、工厂方法模式及抽象工厂模式[/size][/b]
[size=large][b]一、实现方法[/b][/size]
[size=medium]简单工厂:[/size]
[color=red]重要的开-闭原则 因为在引入新的产品需要修改工厂所有引入工厂方法模式[/color]
[size=medium]工厂方法:[/size]
[color=red]如果说现在又要新增一个产品族与Fruit等级的产品此时会新增n多的工厂于是引入抽象工厂[/color]
[size=medium]抽象工厂[/size]
[size=large][b]二、三者的联系[/b][/size]
以实例图为:
[img]http://dl.iteye.com/upload/attachment/264783/b6edcebb-6cf5-3752-aebc-5fc6c626e2d2.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/264785/26b73fd5-d601-3e46-9655-144a0e56bc2b.jpg[/img]
[size=large][b]一、实现方法[/b][/size]
[size=medium]简单工厂:[/size]
public interface Product
{
}
public interface Fruit
{
void grow();
void harvest();
void plant();
}
package com.javapatterns.simplefactory;
public class Grape implements Fruit
{
public void grow()
{
System.out.println("Grape is growing...");
}
public void harvest()
{
System.out.println("Grape has been harvested.");
}
public void plant()
{
System.out.println("Grape has been planted.");
}
public boolean getSeedless()
{
return seedless;
}
public void setSeedless(boolean seedless)
{
this.seedless = seedless;
}
private boolean seedless;
}
package com.javapatterns.simplefactory;
public class Strawberry implements Fruit
{
public void grow()
{
System.out.println("Strawberry is growing...");
}
public void harvest()
{
System.out.println("Strawberry has been harvested.");
}
public void plant()
{
System.out.println("Strawberry has been planted.");
}
}
package com.javapatterns.simplefactory;
public class Apple implements Fruit
{
public void grow()
{
System.out.println("Apple is growing...");
}
public void harvest()
{
System.out.println("Apple has been harvested.");
}
public void plant()
{
System.out.println("Apple has been planted.");
}
public int getTreeAge(){ return treeAge; }
public void setTreeAge(int treeAge){ this.treeAge = treeAge; }
private int treeAge;
}
package com.javapatterns.simplefactory;
public class FruitGardener
{
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");
}
}
}
[color=red]重要的开-闭原则 因为在引入新的产品需要修改工厂所有引入工厂方法模式[/color]
[size=medium]工厂方法:[/size]
package com.javapatterns.factorymethod.farm;
public interface Fruit
{
void grow();
void harvest();
void plant();
}
package com.javapatterns.factorymethod.farm;
public class Grape implements Fruit
{
public void grow()
{
System.out.println("Grape is growing...");
}
public void harvest()
{
System.out.println("Grape has been harvested.");
}
public void plant()
{
System.out.println("Grape has been planted.");
}
public boolean getSeedless()
{
return seedless;
}
public void setSeedless(boolean seedless)
{
this.seedless = seedless;
}
private boolean seedless;
}
package com.javapatterns.factorymethod.farm;
public class Strawberry implements Fruit
{
public void grow()
{
System.out.println("Strawberry is growing...");
}
public void harvest()
{
System.out.println("Strawberry has been harvested.");
}
public void plant()
{
System.out.println("Strawberry has been planted.");
}
}
package com.javapatterns.factorymethod.farm;
public class Apple implements Fruit
{
private int treeAge;
public void grow()
{
System.out.println("Apple is growing...");
}
public void harvest()
{
System.out.println("Apple has been harvested.");
}
public void plant()
{
System.out.println("Apple has been planted.");
}
public int getTreeAge()
{
return treeAge;
}
public void setTreeAge(int treeAge)
{
this.treeAge = treeAge;
}
}
package com.javapatterns.factorymethod.farm;
public interface FruitGardener
{
public Fruit factory();
/** @link dependency
* @label Creates*/
/*# Fruit lnkFruit; */
}
package com.javapatterns.factorymethod.farm;
public class StrawberryGardener implements FruitGardener
{
public Fruit factory()
{
return new Apple();
}
}
package com.javapatterns.factorymethod.farm;
public class GrapeGardener implements FruitGardener
{
public Fruit factory()
{
return new Apple();
}
}
package com.javapatterns.factorymethod.farm;
public class AppleGardener implements FruitGardener
{
public Fruit factory()
{
return new Apple();
}
}
[color=red]如果说现在又要新增一个产品族与Fruit等级的产品此时会新增n多的工厂于是引入抽象工厂[/color]
[size=medium]抽象工厂[/size]
package com.javapatterns.abstractfactory;
public interface ProductA
{
}
package com.javapatterns.abstractfactory;
public class ProductA1 implements ProductA
{
public ProductA1()
{
}
}
package com.javapatterns.abstractfactory;
public class ProductA2 implements ProductA
{
public ProductA2()
{
}
}
package com.javapatterns.abstractfactory;
public interface ProductB
{
}
package com.javapatterns.abstractfactory;
public class ProductB1 implements ProductB
{
public ProductB1()
{
}
}
package com.javapatterns.abstractfactory;
public class ProductB2 implements ProductB
{
public ProductB2()
{
}
}
package com.javapatterns.abstractfactory;
public interface Creator
{
public ProductA factoryA();
public ProductB factoryB();
}
package com.javapatterns.abstractfactory;
public class ConcreteCreator1 implements Creator
{
public ProductA factoryA()
{
return new ProductA1();
}
public ProductB factoryB()
{
return new ProductB1();
}
/** @link dependency */
/*# ProductA1 lnkProductA1; */
/** @link dependency */
/*# ProductB1 lnkProductB1; */
}
package com.javapatterns.abstractfactory;
public class ConcreteCreator2 implements Creator
{
public ProductA factoryA()
{
return new ProductA1();
}
public ProductB factoryB()
{
return new ProductB1();
}
/** @link dependency */
/*# ProductA2 lnkProductA2; */
/** @link dependency */
/*# ProductB2 lnkProductB2; */
}
[size=large][b]二、三者的联系[/b][/size]
以实例图为:
[img]http://dl.iteye.com/upload/attachment/264783/b6edcebb-6cf5-3752-aebc-5fc6c626e2d2.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/264785/26b73fd5-d601-3e46-9655-144a0e56bc2b.jpg[/img]