JAVA语言版抽象工厂模式
创建形状接口与颜色接口:
public interface Shape {
void draw();
}
public interface Color {
void fill();
}
创建抽象工厂接口:
public interface AbstractFactory {
Color getColor(String color);
Shape getShape(String shape);
}
创建颜色实体:
public class Blue implements Color {
@Override
public void fill() {
System.out.println("用蓝色涂满");
}
}
public class Green implements Color{
@Override
public void fill() {
System.out.println("用绿色涂满");
}
}
public class Red implements Color{
@Override
public void fill() {
System.out.println("用红色涂满");
}
}
创建形状实体:
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("画圆形");
}
}
public class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("画矩形");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("画正方形");
}
}
创建颜色工厂与形状工厂的实现类:
public class ColorFactory implements AbstractFactory {
@Override
public Color getColor(String color) {
if (color.equals("Blue")) {
return new Blue();
} else if (color.equals("Green")) {
return new Green();
} else if (color.equals("Red")) {
return new Red();
}
return null;
}
@Override
public Shape getShape(String shape) {
return null;
}
}
public class ShapeFactory implements AbstractFactory {
@Override
public Color getColor(String color) {
return null;
}
@Override
public Shape getShape(String shape) {
if (shape.equals("Circle")) {
return new Circle();
} else if (shape.equals("Rectangle")) {
return new Rectangle();
} else if (shape.equals("Square")) {
return new Square();
}
return null;
}
}
使用该工厂,通过传递类型信息来获取实体类的对象
public class FactorypatterDemo {
public static void main(String[] args) {
AbstractFactory shapeFactory = new ShapeFactory();
Shape circle = shapeFactory.getShape("Circle");
circle.draw();
Shape rectangle = shapeFactory.getShape("Rectangle");
rectangle.draw();
Shape square = shapeFactory.getShape("Square");
square.draw();
AbstractFactory colorFactory = new ColorFactory();
Color red = colorFactory.getColor("Red");
red.fill();
Color green = colorFactory.getColor("Green");
green.fill();
Color blue = colorFactory.getColor("Blue");
blue.fill();
}
}
输出结果:
画圆形
画矩形
画正方形
用红色涂满
用绿色涂满
用蓝色涂满
GO语言版抽象工厂模式
创建形状接口与颜色接口
type Color interface {
Fill()
}
type Shape interface {
Draw()
}
创建抽象工厂接口:
type AbstractFactory interface {
GetShape() Shape
GetColor() Color
}
创建颜色实体:
type Blue struct {
}
func (this *Blue) Fill() {
fmt.Println("用蓝色涂满")
}
type Green struct {
}
func (this *Green)Fill() {
fmt.Println("用绿色涂满")
}
type Red struct {
}
func (this *Red)Fill() {
fmt.Println("用红色涂满")
}
创建形状实体
type Circle struct {
}
func (this *Circle) Draw() {
fmt.Println("画圆形")
}
type Rectangle struct {
}
func (this *Rectangle)Draw() {
fmt.Println("画矩形")
}
type Square struct {
}
func (this *Square)Draw() {
fmt.Println("画正方形")
}
创建颜色工厂与形状工厂的实现类:
type ColorFactory struct {
}
func (this * ColorFactory)GetColorFactory(color string) Color {
if color=="Red" {
return &Red{}
}
if color=="Green" {
return &Green{}
}
if color =="Blue" {
return &Blue{}
}
return nil
}
func (this * ColorFactory)GetShapeFactory(shape string) Shape {
return nil
}
type ShapeFactory struct {
}
func (this * ShapeFactory)GetColorFactory(color string) Color {
return nil
}
func (this * ShapeFactory)GetShapeFactory(shape string) Shape {
if shape=="Circle" {
return &Circle{}
}
if shape =="Rectangle" {
return &Rectangle{}
}
if shape=="Square" {
return &Square{}
}
return nil
}
使用该工厂,通过传递类型信息来获取实体类的对象
func main() {
colorFactory := abstractfactory.ColorFactory{}
red := colorFactory.GetColorFactory("Red")
red.Fill()
green :=colorFactory.GetColorFactory("Green")
green.Fill()
blue :=colorFactory.GetColorFactory("Blue")
blue.Fill()
shapeFactory := abstractfactory.ShapeFactory{}
circle := shapeFactory.GetShapeFactory("Circle")
circle.Draw()
rectangle :=shapeFactory.GetShapeFactory("Rectangle")
rectangle.Draw()
square :=shapeFactory.GetShapeFactory("Square")
square.Draw()
}
输出结果:
用红色涂满
用绿色涂满
用蓝色涂满
画圆形
画矩形
画正方形