几个工厂模式初识

简单工厂模式、工厂方法模式及抽象工厂模式
一、实现方法
简单工厂:
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");
        }
    }
}


重要的开-闭原则 因为在引入新的产品需要修改工厂所有引入工厂方法模式

工厂方法:
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();
    }
}

如果说现在又要新增一个产品族与Fruit等级的产品此时会新增n多的工厂于是引入抽象工厂
抽象工厂
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; */
}

二、三者的联系

以实例图为:
几个工厂模式初识
几个工厂模式初识




你可能感兴趣的:(apple)