You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

931 B

package bridage

import "fmt"

type Draw interface {
	DrawCircle(radius, x, y int)
}

type RedCircle struct {
}

func (r *RedCircle) DrawCircle(radius, x, y int) {
	fmt.Printf("Red :: radius=%d, x=%d, y=%d\n", radius, x, y)
}

type YellowCircle struct {
}

func (r *YellowCircle) DrawCircle(radius, x, y int) {
	fmt.Printf("Yellow :: radius=%d, x=%d, y=%d\n", radius, x, y)
}

type Shape struct {
	draw Draw
}

func (s *Shape) Shape(d Draw) {
	s.draw = d
}

type Circle struct {
	shape  Shape
	x      int
	y      int
	radius int
}

func (c *Circle) Constructor(radius, x, y int, draw Draw) {
	c.radius = radius
	c.x = x
	c.y = y
	c.shape.Shape(draw)
}

func (c *Circle) Draw() {
	c.shape.draw.DrawCircle(c.radius, c.x, c.y)
}

  • 桥接模式

  • 定义接口并且实现接口,并定义统一结构体,其中一个属性是接口

  • 调用时,将需要调用对象作为参数传递进行,实现具体对象调用