Go design patterns --Bridge mode

Abstract part and a bridging mode to achieve some separation. Such that the two parts separate extension.

Bridge mode is similar strategy mode, except that a series of algorithms that package policy model algorithm can be replaced with each other.

Strategy mode achieve the abstract part and partial separation can be varied independently.

Bridge Package 

Import ( 
	"FMT" 
) 

type interface {// selecting a route Road 
	Way () String 
} 

type Trans {// selected transport interface 
	Transport () String 
} 

type Resault struct {// create a bridge, there can be various combinations 
	Road Way 
	Trans Trans 
} 

// bridging function 
FUNC HowtoSchool (Road Road, Trans Trans) * Resault { 
	return {& Resault 
		Way: Road, 
		Trans: Trans, 
	} 
} 

FUNC (the this Resault *) GotoSchool () { 
	fmt.Println ( "the Choose ", this.way.Way (), this.trans.Transport ()," to School ") 
} 


1 // instantiate the route 
of the type Way1 struct { 

} 

method // route 1, to realize how to go 
func (this * Way1 ) Way () string { 
	return" [way1] " 
} 

// select a route, a return route of Example 1 
FUNC ChooseWay1 () Road { 
	return & Way1 {} 
} 

// instantiate Scheme 2 
type Way2 struct { 

} 

The method of Scheme 2 // realize how to get 
FUNC (the this Way2 *) Way () String { 
	return "[Way2]" 
} 

// 2 selecting a route, a return route example 2 
FUNC ChooseWay2 () Road { 
	return & Way2 {} 
} 

// instantiate walking to school 
type walk struct { 

} 

// select walking to school, returns an instance walking 
FUNC ChooseWalk () Trans { 
	return & walk {} 
} 

// methods implemented walk 
FUNC (the this walk *) Transport () String { 
	return "[walk]" 
} 

// walking to school examples of the 
type Bike struct { 

} 

// choose to walk to school, returns an instance walking
ChooseBike FUNC () Trans { 
	return {} & Bike
} 

// methods to achieve walk 
FUNC (* the this Bike) Transport () String { 
	return "[Bike]" 
}

 

	how := bridge.HowtoSchool(bridge.ChooseWay1(),bridge.ChooseWalk())
	how.GotoSchool()

	how = bridge.HowtoSchool(bridge.ChooseWay2(),bridge.ChooseWalk())
	how.GotoSchool()

	how = bridge.HowtoSchool(bridge.ChooseWay1(),bridge.ChooseBike())
	how.GotoSchool()

	how = bridge.HowtoSchool(bridge.ChooseWay2(),bridge.ChooseBike())
	how.GotoSchool()

  

 

 

Guess you like

Origin www.cnblogs.com/flycc/p/12631768.html