Skip to main content

Go Example

package main

type counterTerroristDress struct {
color string
}

func (c *counterTerroristDress) getColor() string {
return c.color
}

func newCounterTerroristDress() *counterTerroristDress {
return &counterTerroristDress{color: "green"}
}
package main

type dress interface {
getColor() string
}
package main

import "fmt"

const (
//TerroristDressType terrorist dress type
TerroristDressType = "tDress"
//CounterTerrroristDressType terrorist dress type
CounterTerrroristDressType = "ctDress"
)

var (
dressFactorySingleInstance = &dressFactory{
dressMap: make(map[string]dress),
}
)

type dressFactory struct {
dressMap map[string]dress
}

func (d *dressFactory) getDressByType(dressType string) (dress, error) {
if d.dressMap[dressType] != nil {
return d.dressMap[dressType], nil
}

if dressType == TerroristDressType {
d.dressMap[dressType] = newTerroristDress()
return d.dressMap[dressType], nil
}
if dressType == CounterTerrroristDressType {
d.dressMap[dressType] = newCounterTerroristDress()
return d.dressMap[dressType], nil
}

return nil, fmt.Errorf("Wrong dress type passed")
}

func getDressFactorySingleInstance() *dressFactory {
return dressFactorySingleInstance
}
package main

type game struct {
terrorists []*player
counterTerrorists []*player
}

func newGame() *game {
return &game{
terrorists: make([]*player, 1),
counterTerrorists: make([]*player, 1),
}
}

func (c *game) addTerrorist(dressType string) {
player := newPlayer("T", dressType)
c.terrorists = append(c.terrorists, player)
return
}

func (c *game) addCounterTerrorist(dressType string) {
player := newPlayer("CT", dressType)
c.counterTerrorists = append(c.counterTerrorists, player)
return
}
package main

type player struct {
dress dress
playerType string
lat int
long int
}

func newPlayer(playerType, dressType string) *player {
dress, _ := getDressFactorySingleInstance().getDressByType(dressType)
return &player{
playerType: playerType,
dress: dress,
}
}

func (p *player) newLocation(lat, long int) {
p.lat = lat
p.long = long
}
package main

type terroristDress struct {
color string
}

func (t *terroristDress) getColor() string {
return t.color
}

func newTerroristDress() *terroristDress {
return &terroristDress{color: "red"}
}
package main

import "fmt"

func main() {
game := newGame()

//Add Terrorist
game.addTerrorist(TerroristDressType)
game.addTerrorist(TerroristDressType)
game.addTerrorist(TerroristDressType)
game.addTerrorist(TerroristDressType)

//Add CounterTerrorist
game.addCounterTerrorist(CounterTerrroristDressType)
game.addCounterTerrorist(CounterTerrroristDressType)
game.addCounterTerrorist(CounterTerrroristDressType)

dressFactoryInstance := getDressFactorySingleInstance()

for dressType, dress := range dressFactoryInstance.dressMap {
fmt.Printf("DressColorType: %s\nDressColor: %s\n", dressType, dress.getColor())
}
}