请举一个工厂方法模式的例子 [英] Please give an example of factory method pattern

查看:109
本文介绍了请举一个工厂方法模式的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 factory pattern和 factory method pattern之间的区别。



工厂方法模式将对象的实例化延迟到子类。另外这是一个工厂方法,因为创建者的孩子负责实现创建方法。



哪个作为一个简单的工厂直接由想要创建对象的类调用。



但是工厂模式也可以通过反射来添加类注册,并为要创建的对象使用一个子类,并将对象实例化逻辑保持在该子类中,从而解除工厂类。



我没有得到符合上述工厂方法模式定义的工厂方法模式的示例。



<你可以给我一个工厂方法示例吗?

解决方案

假设我们要生产水果:

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

//具体水果Apple
公共类Apple实现水果{
@Override
public void plant(){
System。 out.println(苹果种植);
}
@Override
public void grow(){
System.out.println(Apple正在增长);
}
}

//具体水果香蕉
公共类香蕉实现水果{
@Override
public void plant(){。 ..}
@Override
public void grow(){...}
}

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



例如:

  //工厂类
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(对不起!%s不支持.\\\
,fruitName);
抛出新的异常();
}
}
/ *创建实例的另一种方式
public static Fruit createApple(){
return new Apple();
}
public static Fruit createBanana(){
return new Banana();
}
* /
}

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



示例: / p>

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

//具体工厂负责生产Apple
public class AppleGarden实现FruitGarden {
@Override
public Fruit createFruit(){
return new Apple();
}
}

//负责生产香蕉的具体工厂
public class BananaGarden实现FruitGarden {
@Override
public Fruit createFruit (){
return new Banana();
}
}


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?

解决方案

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();
    }
}

这篇关于请举一个工厂方法模式的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆