LoginSignup
0

【Gang of Four】Prototype

Last updated at Posted at 2019-03-18

#Prototype
目次
部品をプールし、クローンすることでオブジェクトを生成するパターンです。

##目的
生成すべきオブジェクトの種類を原型となるインスタンスを使って明確にし、それをコピーすることで新たなオブジェクトの生成を行う。

##構成要素
・Prototype 複製を行う抽象クラス
・ConcretePrototype 複製を行う具象クラス
・Client 複製を依頼するクラス

##実装
車を量産するために必要な試作品工場を実装します。

まずは試作品工場が作る部品から実装します。

エンジンクラス

Engine.kt
package prototype

class Engine(displacement: Int): Cloneable {
    var displacement = displacement

    fun show() {
        print("【エンジン】"+ displacement +"cc ")
    }

    public override fun clone(): Any {
        return super.clone()
    }
}

タイヤクラス

Tire.kt
package prototype

class Tire(num: Int): Cloneable {
    var num = num

    fun show() {
        print("【タイヤ】" + num + "個 ")
    }

    public override fun clone(): Any {
        return super.clone()
    }
}

以上が部品です。続いて製品の車を実装します。

車クラス

Car.kt
package prototype

class Car(engine: Engine, tire: Tire) {
    val engine = engine
    val tire = tire

    fun show() {
        print("【製品】車 ")
        engine.show()
        tire.show()
        print("\n")
    }
}

今まで実装したEngineとTireを次々量産できる工場を実装します。

###Prototype 複製を行う抽象クラス
試作品インターフェース

ProtoType.kt
package prototype

/**
 * 試作品
 * Prototype
 */
interface ProtoType {
    fun createEngine(): Engine
    fun createTire(): Tire
}

###ConcretePrototype 複製を行う具象クラス
試作品工場クラス

ProtoTypeFactory.kt
package prototype

class ProtoTypeFactory: ProtoType {
    var engine = Engine(0)
    var tire = Tire(4)

    override fun createEngine(): Engine {
        return engine.clone() as Engine
    }

    override fun createTire(): Tire {
        return tire.clone() as Tire
    }
}

最後に試作品工場から部品を受け取り製品を量産する工場を実装します。
###Client 複製を依頼するクラス
車製造工場クラス

Factory.kt
package prototype

class Factory {
    var carList:MutableList<Car> = mutableListOf()
    var prototypeFactory = ProtoTypeFactory()
    init {

        // 排気量を1000ccに設定
        prototypeFactory.engine.displacement = 1000
        // 排気量1000ccの車を3台量産する
        massProduction(3)

        // 排気量を2000ccに設定
        prototypeFactory.engine.displacement = 2000
        // 排気量2000ccの車を2台量産する
        massProduction(2)

        // 排気量を12000ccに設定
        prototypeFactory.engine.displacement = 12000
        // タイヤを8本に設定
        prototypeFactory.tire.num = 8
        // 排気量12000cc タイヤ8本の車を5台量産する バス?
        massProduction(5)

        for (car in carList) {
            car.show()
        }
    }

    private fun massProduction(num: Int) {
        for (i in 0..num) {
            carList.add(Car(prototypeFactory.createEngine(), prototypeFactory.createTire()))
        }
    }
}

以上で試作品を次々量産できる工場が実装できました。Abstract Factoryパターンとは異なり、いろいろな車種を量産したくなってもProtoTypeFactoryクラスが持つ部品のメンバを変更すれば新たな工場を実装せずに済みます。

###出力結果

[output]
【製品】車 【エンジン】1000cc 【タイヤ】4個 
【製品】車 【エンジン】1000cc 【タイヤ】4個 
【製品】車 【エンジン】1000cc 【タイヤ】4個 
【製品】車 【エンジン】1000cc 【タイヤ】4個 
【製品】車 【エンジン】2000cc 【タイヤ】4個 
【製品】車 【エンジン】2000cc 【タイヤ】4個 
【製品】車 【エンジン】2000cc 【タイヤ】4個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 
【製品】車 【エンジン】12000cc 【タイヤ】8個 

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