找到你要的答案

Q:Please give an example of factory method pattern

Q:请举例说明工厂方法模式

I read the difference between factory pattern and factory method pattern.

Factory method pattern defers instantiation of an object to subclasses. Also it is a Factory Method because the children of “Creator” are responsible for implementing the “Create” method.

Where as a simple factory is called directly by the class which wants to create an object.

But for factory pattern also we can decouple the factory class by adding class registration through reflection and using a layer of subclasses for the objects to be created and keeping the object instantiation logic inside this subclasses.

I am not getting an example of the factory method pattern that conforms to the above definition of factory method pattern.

Can you please provide me a factory method example?

我看了工厂模式和工厂方法模式的区别。

工厂方法模式将一个对象实例化的类。这也是一种工厂方法,因为“创建者”的孩子们负责实施“创建”方法。

当一个简单的工厂被直接调用的类想创建一个对象。

但对于工厂模式也可以将工厂类,通过反射和使用层的子类被创建和保持对象实例化内部类对象添加逻辑这类注册。

我没有得到一个工厂方法模式的例子,它符合工厂方法模式的上述定义。

你能给我提供一个工厂方法的例子吗?

answer1: 回答1:

Suppose we want to produce fruits:

    public interface Fruit {
        public void plant();
        public void grow();
    }

    // concrete fruit Apple
    public class Apple implements Fruit {
        @Override
        public void plant() {
            System.out.println("Apple is planted.");
        }
        @Override
        public void grow() {
            System.out.println("Apple is growing.");
        }
    }

    // concrete fruit Banana
    public class Banana implements Fruit {
        @Override
        public void plant() { ... }
        @Override
        public void grow() { ... }
    }

In factory pattern, there is one factory class responsible for producing new instances. While the disadvantage is that if added one type of instance, have to change logic of the factory class.

Example:

// Factory class
public class FruitGarden {
    public static Fruit createFruit(String fruitName) throws Exception {
        if(fruitName.equals("Apple")) {
            return new Apple();
        } else if(fruitName.equals("Banana")) {
            return new Banana();
        } else {
            System.out.printf("Sorry! %s not supported.\n", fruitName);
            throw new Exception();
        }
    }
    /* another way to create instance
    public static Fruit createApple() {
        return new Apple();
    }
    public static Fruit createBanana() {
        return new Banana();
    }
    */
}

In factory method pattern, there is an abstract factory representing the creation of new instances. Different type of instances are created by different concrete factories. All these concrete factories implement the abstract factory interface or extends abstract factory class. If added one new type, just add one concrete factory, so it has more expandability.

Example:

// abstract factory interface
public interface FruitGarden {
    public Fruit createFruit();
}

// concrete factory which is responsible producing Apple
public class AppleGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Apple();
    }
}

// concrete factory which is responsible producing Banana
public class BananaGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Banana();
    }
}

假设我们想要生产水果:

    public interface Fruit {
        public void plant();
        public void grow();
    }

    // concrete fruit Apple
    public class Apple implements Fruit {
        @Override
        public void plant() {
            System.out.println("Apple is planted.");
        }
        @Override
        public void grow() {
            System.out.println("Apple is growing.");
        }
    }

    // concrete fruit Banana
    public class Banana implements Fruit {
        @Override
        public void plant() { ... }
        @Override
        public void grow() { ... }
    }

在工厂模式中,有一个工厂类负责生成新实例。缺点是如果添加了一种类型的实例,就必须更改工厂类的逻辑。

例子:

// Factory class
public class FruitGarden {
    public static Fruit createFruit(String fruitName) throws Exception {
        if(fruitName.equals("Apple")) {
            return new Apple();
        } else if(fruitName.equals("Banana")) {
            return new Banana();
        } else {
            System.out.printf("Sorry! %s not supported.\n", fruitName);
            throw new Exception();
        }
    }
    /* another way to create instance
    public static Fruit createApple() {
        return new Apple();
    }
    public static Fruit createBanana() {
        return new Banana();
    }
    */
}

在工厂方法模式中,有一个抽象工厂,代表创建新实例。不同类型的实例由不同的具体工厂创建。所有这些具体工厂都实现抽象工厂接口或扩展抽象工厂类。如果添加了一个新的类型,只是添加一个具体工厂,因此更具有可扩展性。

例子:

// abstract factory interface
public interface FruitGarden {
    public Fruit createFruit();
}

// concrete factory which is responsible producing Apple
public class AppleGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Apple();
    }
}

// concrete factory which is responsible producing Banana
public class BananaGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Banana();
    }
}
answer2: 回答2:

Java's Collection.iterator() is a factory method.

The concrete implementation of the collection determines what concrete iterator is returned by this method.

java的集合()是一个工厂方法。

集合的具体实现决定了该方法返回的具体迭代器。

design-patterns  factory