工厂模式和抽象工厂模式之间的基本区别是什么?
当前回答
工厂模式: 工厂生产iproduct实现
工厂模式: 一个工厂-工厂生产IFactories, IFactories反过来生产IProducts:)
[根据评论更新] 我之前写的至少在维基百科上是不正确的。抽象工厂就是一个简单的工厂接口。有了它,您可以在运行时切换工厂,以允许在不同的上下文中使用不同的工厂。例如针对不同操作系统的不同工厂、SQL提供者、中间件驱动程序等等。
其他回答
延伸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.
}
}
点击这里查看:http://www.allapplabs.com/java_design_patterns/abstract_factory_pattern.htm 似乎Factory方法使用一个特定的类(不是抽象类)作为基类,而抽象工厂则使用一个抽象类。此外,如果使用接口而不是抽象类,结果将是抽象工厂模式的不同实现。
:D
根据定义,我们可以拖出两者的差异:
工厂:接口用于创建对象,但子类决定实例化哪个类。对象的创建是在需要时完成的。
抽象工厂:抽象工厂模式充当一个超级工厂,它可以创建其他工厂。在抽象工厂模式中,接口负责创建一组相关对象或依赖对象,而不指定它们的具体类。
所以,在上面的定义中,我们可以强调一个特定的区别。也就是说,工厂模式负责创建对象,抽象工厂负责创建一组相关的对象;显然都是通过一个接口。
工厂模式:
public interface IFactory{
void VehicleType(string n);
}
public class Scooter : IFactory{
public void VehicleType(string n){
Console.WriteLine("Vehicle type: " + n);
}
}
public class Bike : IFactory{
public void VehicleType(string n) {
Console.WriteLine("Vehicle type: " + n);
}
}
public interface IVehicleFactory{
IFactory GetVehicleType(string Vehicle);
}
public class ConcreteVehicleFactory : IVehicleFactory{
public IFactory GetVehicleType(string Vehicle){
switch (Vehicle){
case "Scooter":
return new Scooter();
case "Bike":
return new Bike();
default:
return new Scooter();
}
}
class Program{
static void Main(string[] args){
IVehicleFactory factory = new ConcreteVehicleFactory();
IFactory scooter = factory.GetVehicleType("Scooter");
scooter.VehicleType("Scooter");
IFactory bike = factory.GetVehicleType("Bike");
bike.VehicleType("Bike");
Console.ReadKey();
}
}
工厂模式:
interface IVehicleFactory{
IBike GetBike();
IScooter GetScooter();
}
class HondaFactory : IVehicleFactory{
public IBike GetBike(){
return new FZS();
}
public IScooter GetScooter(){
return new FZscooter();
}
}
class HeroFactory: IVehicleFactory{
public IBike GetBike(){
return new Pulsur();
}
public IScooter GetScooter(){
return new PulsurScooter();
}
}
interface IBike
{
string Name();
}
interface IScooter
{
string Name();
}
class FZS:IBike{
public string Name(){
return "FZS";
}
}
class Pulsur:IBike{
public string Name(){
return "Pulsur";
}
}
class FZscooter:IScooter {
public string Name(){
return "FZscooter";
}
}
class PulsurScooter:IScooter{
public string Name(){
return "PulsurScooter";
}
}
enum MANUFACTURERS
{
HONDA,
HERO
}
class VehicleTypeCheck{
IBike bike;
IScooter scooter;
IVehicleFactory factory;
MANUFACTURERS manu;
public VehicleTypeCheck(MANUFACTURERS m){
manu = m;
}
public void CheckProducts()
{
switch (manu){
case MANUFACTURERS.HONDA:
factory = new HondaFactory();
break;
case MANUFACTURERS.HERO:
factory = new HeroFactory();
break;
}
Console.WriteLine("Bike: " + factory.GetBike().Name() + "\nScooter: " + factory.GetScooter().Name());
}
}
class Program
{
static void Main(string[] args)
{
VehicleTypeCheck chk = new VehicleTypeCheck(MANUFACTURERS.HONDA);
chk.CheckProducts();
chk= new VehicleTypeCheck(MANUFACTURERS.HERO);
chk.CheckProducts();
Console.Read();
}
}
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.
工厂方法:您有一个工厂,它创建派生自特定基类的对象
抽象工厂:你有一个创建其他工厂的工厂,这些工厂反过来创建从基类派生的对象。这样做是因为您通常不只是想创建单个对象(与Factory方法一样)—相反,您想创建相关对象的集合。