抽象工厂模式代码举例(java语言版与go语言版)

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()
}

输出结果:

用红色涂满
用绿色涂满
用蓝色涂满
画圆形
画矩形
画正方形

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值