23种设计模式之(三)抽象工厂模式(Abstract Factory)
本文主要介绍23种设计模式之抽象工厂模式,附详细python/c++示例代码。
- 概念
- 应用场景
- 注意事项
- 代码示例
- 总结
- 代码链接
抽象工厂模式(Abstract Factory)
概念
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的。抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象。
GoF对抽象工厂模式的定义是:抽闲工厂模式——提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
应用场景
一系列相互依赖的对象有不同的具体实现,提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合。
注意事项
抽象工厂模式与工厂模式的主要区别是:工厂模式只能生产一个产品(要么香蕉、要么苹果),抽象工厂可以一下生产一个产品族(里面有很多产品组成)。
代码示例
C++代码示例
/************************************************************************/
/* 设计模式专题
/*
/* 抽象工厂模式
/*
/* Author : zzl
/*
/* 编程环境: window10 vs2010
/*
/* Date : 20180914
/************************************************************************/
#include <iostream>
class Fruit
{
public:
virtual void show() = 0;
};
class AbstractFactory
{
public:
virtual Fruit* CreateBanana() = 0;
virtual Fruit* CreateApple() = 0;
};
class NorthBanana : public Fruit
{
public:
virtual void show()
{
printf("i am an north banana\n");
}
};
class NorthApple : public Fruit
{
public:
virtual void show()
{
printf("i am an north apple\n");
}
};
class SourthBanana : public Fruit
{
public:
virtual void show()
{
printf("i am an sourth banana\n");
}
};
class SourthApple : public Fruit
{
public:
virtual void show()
{
printf("i am an sourth apple\n");
}
};
class NorthFacorty : public AbstractFactory
{
virtual Fruit * CreateBanana()
{
return new NorthBanana;
}
virtual Fruit * CreateApple()
{
return new NorthApple;
}
};
class SourthFacorty : public AbstractFactory
{
virtual Fruit * CreateBanana()
{
return new SourthBanana;
}
virtual Fruit * CreateApple()
{
return new SourthApple;
}
};
void main()
{
Fruit *fruit = NULL;
AbstractFactory *af = NULL;
///Sourth
af = new SourthFacorty;
fruit = af->CreateApple();
fruit->show();
delete fruit;
fruit = af->CreateBanana();
fruit->show();
delete fruit;
///North
af = new NorthFacorty;
fruit = af->CreateApple();
fruit->show();
delete fruit;
fruit = af->CreateBanana();
fruit->show();
delete fruit;
delete af;
system("pause");
return ;
}
python代码示例
# -*- coding: utf-8 -*-
###################################################################
# 设计模式专题
#
# 抽象工厂模式
#
# Author : zzl
#
# 编程环境: window10 python2.7
#
# Date : 20180914
##################################################################
class Fruit(object):
def show(self):
pass
class AbstractFactory(object):
def create_banana(self):
pass
def create_apple(self):
pass
class NorthBanana(Fruit):
def show(self):
print("i am an north banana")
class NorthApple(Fruit):
def show(self):
print("i am an north apple")
class SourthBanana(Fruit):
def show(self):
print("i am an sourth banana")
class SourthApple(Fruit):
def show(self):
print("i am an sourth apple")
class NorthFacorty(AbstractFactory):
def create_banana(self):
return NorthBanana()
def create_apple(self):
return NorthApple()
class SourthFacorty(AbstractFactory):
def create_banana(self):
return SourthBanana()
def create_apple(self):
return SourthApple()
if __name__ == "__main__":
# Sourth
af = SourthFacorty()
fruit = af.create_apple()
fruit.show()
fruit = af.create_banana()
fruit.show()
# North
af = NorthFacorty()
fruit = af.create_apple()
fruit.show()
fruit = af.create_banana()
fruit.show()