工厂模式和抽象工厂模式之间的基本区别是什么?
当前回答
我的资源是:StackOverflow, tutorialspoint.com, programmers.stackexchange.com和CodeProject.com。
工厂方法(也称为工厂)用于解耦接口实现的客户端。例如,我们有一个具有圆和方两个实现的Shape接口。我们已经定义了一个工厂类,它带有一个工厂方法和一个确定参数,如Type和Shape接口的新的相关实现。
抽象工厂包含多个工厂方法或多个工厂实现的工厂接口。 对于上面的下一个示例,我们有一个带有两个红色和黄色实现的颜色接口。 我们已经用两个RedCircleFactory和YellowSquareFactory定义了一个ShapeColorFactory接口。下面的代码解释这个概念:
interface ShapeColorFactory
{
public Shape getShape();
public Color getColor();
}
class RedCircleFactory implements ShapeColorFactory
{
@Override
public Shape getShape() {
return new Circle();
}
@Override
public Color getColor() {
return new Red();
}
}
class YellowSquareFactory implements ShapeColorFactory
{
@Override
public Shape getShape() {
return new Square();
}
@Override
public Color getColor() {
return new Yellow();
}
}
这里是FactoryMethod和AbstractFactory的区别。工厂方法返回接口的具体类,而抽象工厂返回工厂的工厂。换句话说,抽象工厂返回不同组合的一系列界面。
我希望我的解释有用。
其他回答
延伸John Feminella的回答:
Apple, Banana, Cherry实现了FruitFactory,它有一个叫做Create的方法,它只负责创建Apple, Banana或Cherry。Factory方法就完成了。
现在,你想用你的水果创建一个特殊的沙拉,这就是你的抽象工厂。抽象工厂知道如何用苹果、香蕉和樱桃制作你的特殊沙拉。
public class Apple implements Fruit, FruitFactory {
public Fruit Create() {
// Apple creation logic goes here
}
}
public class Banana implements Fruit, FruitFactory {
public Fruit Create() {
// Banana creation logic goes here
}
}
public class Cherry implements Fruit, FruitFactory {
public Fruit Create() {
// Cherry creation logic goes here
}
}
public class SpecialSalad implements Salad, SaladFactory {
public static Salad Create(FruitFactory[] fruits) {
// loop through the factory and create the fruits.
// then you're ready to cut and slice your fruits
// to create your special salad.
}
}
使用Factory模式,您可以生成特定接口(例如,IFruit)的实现实例(Apple、Banana、Cherry等)。
使用抽象工厂模式,您可以为任何人提供他们自己的工厂提供一种方法。这使得您的仓库可以是IFruitFactory或IJuiceFactory,而不需要您的仓库了解任何关于水果或果汁的信息。
工厂模式: 工厂生产iproduct实现
工厂模式: 一个工厂-工厂生产IFactories, IFactories反过来生产IProducts:)
[根据评论更新] 我之前写的至少在维基百科上是不正确的。抽象工厂就是一个简单的工厂接口。有了它,您可以在运行时切换工厂,以允许在不同的上下文中使用不同的工厂。例如针对不同操作系统的不同工厂、SQL提供者、中间件驱动程序等等。
//Abstract factory - Provides interface to create factory of related products
interface PizzaIngredientsFactory{
public Dough createDough(); //Will return you family of Dough
public Clam createClam(); //Will return you family of Clam
public Sauce createSauce(); //Will return you family of Sauce
}
class NYPizzaIngredientsFactory implements PizzaIngredientsFactory{
@Override
public Dough createDough(){
//create the concrete dough instance that NY uses
return doughInstance;
}
//override other methods
}
课本上的定义已经由其他答案提供了。我想我也会提供一个例子。
因此这里pizzaingredient factory是一个抽象工厂,因为它提供了创建一系列相关产品的方法。
注意,抽象工厂中的每个方法本身都是一个工厂方法。就像createDough()本身是一个工厂方法,其具体实现将由nypizzafactorentsfactory等子类提供。因此,使用这个方法,每个不同的位置都可以创建属于其位置的具体成分实例。
工厂方法
提供具体实现的实例
在这个例子中: - createDough() -提供面团的具体实现。这是一个工厂方法
抽象工厂
提供创建相关对象族的接口
在这个例子中: pizzafactorentsfactory是一个抽象工厂,因为它允许创建一组相关的对象,如面团,蛤蜊,酱汁。为了创建每个对象族,它提供了一个工厂方法。
来自Head First设计模式的示例
Abstract Factory is template for creating different type of interfaces. Suppose you have project that requires you to parse different types of csv files containing quantity, price and item specific information like some contain data about fruits other about chocolates and then after parsing you need to update this information in their corresponding database so now you can have one abstract factory returning you parser and modifier factory and then this parser factory can return you Chocolate parser object,Fruit Parser Object etc. and similarly Modifier Factory can return Chocolate modifier object , Fruit Modifier object etc.