- 抽象工厂模式
- 1.介绍
- 概念
- 主要作用
- 解决的问题
- 使用场景
- 2.UML类图
- 3.实现
- java实现
- golang实现
- 其它设计模式
抽象工厂模式
1.介绍
概念
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
主要作用
提供一个创建一系列相关或者依赖对象的接口,而无需指定他们具体的类。
解决的问题
1.解决接口选择的问题。
2.如果同一家工厂需要生产不止一种产品,那么使用工厂模式会造成工厂过多的情况,使用抽象工厂模式可以使同一个工厂组成一个“产品族”,即可以生产多种产品。
使用场景
1、QQ 换皮肤,一整套一起换。 2、生成不同操作系统的程序。
2.UML类图
3.实现
java实现
1.创建鼠标接口
public interface Mouse {
void showMouse();
}
2.创建键盘接口
public interface KeyBoard {
void showKeyBoard();
}
3.创建戴尔鼠标类
public class DellMouse implements Mouse{
@Override
public void showMouse() {
System.out.println("This is a mouse produced by Dell.");
}
}
4.创建戴尔键盘类
public class DellKeyBoard implements KeyBoard{
@Override
public void showKeyBoard() {
System.out.println("This is a keyboard produced by Dell.");
}
}
5.创建华硕鼠标类
public class AsusMouse implements Mouse{
@Override
public void showMouse() {
System.out.println("This is a mouse produced by Asus.");
}
}
6.创建华硕键盘类
public class AsusKeyBoard implements KeyBoard{
@Override
public void showKeyBoard() {
System.out.println("This is a keyboard produced by Asus.");
}
}
7.创建抽象工厂类
public interface AbstractFactory {
Mouse produceMouse(String brand);
KeyBoard produceKeyBoard(String brand);
}
8.创建鼠标工厂类
public class MouseFactory implements AbstractFactory{
@Override
public Mouse produceMouse(String brand) {
if (brand == null) {
return null;
}
if (brand.equalsIgnoreCase("Dell")) {
return new DellMouse();
} else if (brand.equalsIgnoreCase("Asus")) {
return new AsusMouse();
}
return null;
}
@Override
public KeyBoard produceKeyBoard(String brand) {
return null;
}
}
9.创建键盘工厂类
public class KeyBoardFactory implements AbstractFactory{
@Override
public KeyBoard produceKeyBoard(String brand) {
if (brand == null) {
return null;
}
if (brand.equalsIgnoreCase("Dell")) {
return new DellKeyBoard();
} else if (brand.equalsIgnoreCase("Asus")) {
return new AsusKeyBoard();
}
return null;
}
@Override
public Mouse produceMouse(String brand) {
return null;
}
}
10.创建工厂构造器类
public class FactoryProducer {
public static AbstractFactory createFactory(String choice) {
if (choice == null) {
return null;
}
if (choice.equalsIgnoreCase("Mouse")) {
return new MouseFactory();
} else if (choice.equalsIgnoreCase("KeyBoard")) {
return new KeyBoardFactory();
}
return null;
}
}
11.创建测试类
public class AbstractFactoryDemo {
public static void main(String[] args) {
AbstractFactory mouse = FactoryProducer.createFactory("mouse");
Mouse dellMouse = mouse.produceMouse("dell");
dellMouse.showMouse();
System.out.println(".........");
AbstractFactory keyboard = FactoryProducer.createFactory("keyboard");
KeyBoard asusKeyBoard = keyboard.produceKeyBoard("asus");
asusKeyBoard.showKeyBoard();
}
}
12.运行结果
This is a mouse produced by Dell.
…
This is a keyboard produced by Asus.
golang实现
package AbstractFactoryPattern
import (
"fmt"
)
type Mouse interface {
showMouse()
}
type KeyBoard interface {
showKeyBoard()
}
type DellMouse struct {}
func (*DellMouse)showMouse() {
fmt.Println("This is a mouse produced by Dell.")
}
type DellKeyBoard struct {}
func (*DellKeyBoard)showKeyBoard() {
fmt.Println("This is a keyboard produced by Dell.")
}
type AsusMouse struct {}
func (*AsusMouse)showMouse() {
fmt.Println("This is a mouse produced by Asus.")
}
type AsusKeyBoard struct {}
func (*AsusKeyBoard)showKeyBoard() {
fmt.Println("This is a keyboard produced by Asus.")
}
type AbstractFactory interface {
produceMouse(brand string) Mouse
produceKeyBoard(brand string) KeyBoard
}
type MouseFactory struct {}
func (*MouseFactory)produceMouse(brand string) Mouse {
if brand == "" {
return nil
}
if brand == "Dell" {
return &DellMouse{}
} else if brand == "Asus" {
return &AsusMouse{}
}
return nil
}
func (*MouseFactory)produceKeyBoard(brand string) KeyBoard {
return nil
}
type KeyBoardFactory struct {}
func (*KeyBoardFactory) produceMouse(brand string) Mouse {
return nil
}
func (*KeyBoardFactory)produceKeyBoard(brand string) KeyBoard {
if brand == "" {
return nil
}
if brand == "Dell" {
return &DellKeyBoard{}
} else if brand == "Asus" {
return &AsusKeyBoard{}
}
return nil
}
type FactoryProducer struct {}
func (*FactoryProducer)createFactory(choice string) AbstractFactory {
if choice == "" {
return nil
}
if choice == "Mouse" {
return &MouseFactory{}
} else if choice == "KeyBoard" {
return &KeyBoardFactory{}
}
return nil
}
package AbstractFactoryPattern
import "testing"
func TestAbstractFactoryCreate(t *testing.T) {
factoryProducer := FactoryProducer{}
mouse := factoryProducer.createFactory("Mouse")
dellMouse := mouse.produceMouse("Dell")
dellMouse.showMouse()
}