0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Go】簡単なFactoryパターンのサンプルコード

Posted at

概要

Goで簡単Factoryパターンを実装。

内容はポケモンをFactoryパターンを用いて生成するもの。

サンプルコード

package main

// IPokemon インターフェース: ポケモンの基本的な振る舞いを定義
type IPokemon interface {
	setName(name string)
	setAttack(attack string)
	getAttack() string
	getName() string
}

// Pokemon 構造体: ポケモンの共通のプロパティを保持
type Pokemon struct {
	name   string
	attack string
}

// setName: ポケモンの名前を設定
func (p *Pokemon) setName(name string) {
	p.name = name
}

// setAttack: ポケモンの攻撃技を設定
func (p *Pokemon) setAttack(attack string) {
	p.attack = attack
}

// getAttack: ポケモンの攻撃技を取得
func (p *Pokemon) getAttack() string {
	return p.attack
}

// getName: ポケモンの名前を取得
func (p *Pokemon) getName() string {
	return p.name
}

// Pikachu 構造体: ポケモンの一種 (ピカチュウ)
type Pikachu struct {
	Pokemon
}

// NewPikachu: ピカチュウを生成するファクトリーメソッド
func NewPikachu() IPokemon {
	return &Pikachu{
		Pokemon: Pokemon{
			name:   "ピカチュウ",
			attack: "電気ショック",
		},
	}
}

// Eevee 構造体: ポケモンの一種 (イーブイ)
type Eevee struct {
	Pokemon
}

// NewEevee: イーブイを生成するファクトリーメソッド
func NewEevee() IPokemon {
	return &Eevee{
		Pokemon: Pokemon{
			name:   "イーブイ",
			attack: "たいあたり",
		},
	}
}

// GetPokemon: ポケモンの名前を指定してインスタンスを取得
func GetPokemon(name string) IPokemon {
	switch name {
	case "ピカチュウ":
		return NewPikachu()
	case "イーブイ":
		return NewEevee()
	default:
		return nil
	}
}

func main() {
	pikachu := GetPokemon("ピカチュウ")
	eevee := GetPokemon("イーブイ")

	// 生成したポケモンの名前と技を出力
	println(pikachu.getName(), "の技: ", pikachu.getAttack())
	println(eevee.getName(), "の技: ", eevee.getAttack())
}

// 出力
// ピカチュウ の技:  電気ショック
// イーブイ の技:  たいあたり
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?