Skip to content

Instantly share code, notes, and snippets.

@nvcnvn
Last active December 20, 2017 04:15
Show Gist options
  • Save nvcnvn/3271863ac703d5f103098fc03b9050bf to your computer and use it in GitHub Desktop.
Save nvcnvn/3271863ac703d5f103098fc03b9050bf to your computer and use it in GitHub Desktop.
func main() {
assasinService := &KindOfAssasinService{}
transporter := NewTransporter(assasinService)
hitman := NewHitMan(assasinService);
}
type KindOfAssasinService {}
func (ass *KindOfAssasinService) GetVehicle(t string) Vehicle {
if t == "car" {
return &Car{}
}
return &Tank{}
}
func (ass *KindOfAssasinService) GetWeapon(t string) Weapon {
if t == "gun" {
return &Gun{}
}
return &Dagger{}
}
type WeaponFactory interface {
GetWeapon(t string) Weapon
}
type Hitman struct {
weaponFactory WeaponFactory
}
func (h *Hitman) doHitmanStuff() {
h.weaponFactory.GetWeapon("gun").KickSomeAss()
}
type VehicleFactory interface {
GetVehicle(t string) Vehicle
}
type Transporter struct {
vehicle Vehicle
}
func (t *Transporter) makeOutWith(pretty Lady) {
// ...
}
func (t *) hitAllTheBadGuysAt(dest Desination) {
t.vehicle.Move(dest)
}
func NewTransporter(f VehicleFactory) *Transporter {
return &Transporter{
vehicle: f.Getvehicle("car"),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment