scala設計モデルの構築者モデルを実現

18391 ワード

package com.linewell.modeldesgin.builder

/**  *        * Created by ctao on 2015/8/8.  */  /**  *        */ trait ActorBuilder {
    /**  *     */  protected var actor: Actor

    /**  *       */  def buildRole(): Unit

    /**  *       */  def buildSex(): Unit

    /**  *       */  def buildFace(): Unit

    /**  *       */  def buildCostume(): Unit

    //     
    val BareHead = false   //    
    def buildHairStyle(): Unit

    //    
    def createActor(): Actor

    /**  *     ,          * @param ab      * @return        */  def construct(ab: ActorBuilder): Actor = {
        //     ,     ,     ,  ,            ,         
        ab.buildRole()
        ab.buildSex()
        ab.buildFace()
        ab.buildCostume()
        if (!ab.BareHead) {
            ab.buildHairStyle()
        }
        ab.createActor()
    }
}


/**  *      */ class Actor {
    //  
    var role: String = _
    //  
    var sex: String = _
    //  
    var face: String = _
    //  
    var costume: String = _
    //  
    var hairstyle: String = "  "

}

/**  *        */ object HeroBuilder extends ActorBuilder {

    override protected var actor: Actor = new Actor()

    override def buildRole() = actor.role = "  "

    override def buildSex() = actor.sex = " "

    override def buildFace() = actor.face = "  "

    override def buildCostume() = actor.costume = "  "

    override def buildHairStyle() = actor.hairstyle = "  "

    override def createActor() = actor }

object DevilBuilder extends ActorBuilder{
    override protected var actor: Actor = new Actor()

    override def buildRole() = actor.role = "  "

    override def buildSex() = actor.sex = " "

    override def buildFace() = actor.face = "  "

    override def buildCostume() = actor.costume = "  "


    override def buildHairStyle() = actor.hairstyle = "     "

    /**  *               buildHairStyle  */  override val BareHead = true  override def createActor() = actor }

object AngleBuilder extends ActorBuilder{
    override protected var actor: Actor = new Actor()

    override def buildRole() = actor.role = "  "

    override def buildSex() = actor.sex = " "

    override def buildFace() = actor.face = "  "

    override def buildCostume() = actor.costume = "  "

    override def buildHairStyle() = actor.hairstyle = "  "

    override def createActor() = actor }
package com.linewell.modeldesgin.builder

/**  *        * Created by ctao on 2015/8/8.  */ object Client extends App {
    /**  *        */  val ab1: ActorBuilder = HeroBuilder
    val actor1 = ab1.construct(ab1)
    println(actor1.role+"   :")
    println("  :"+actor1.sex)
    println("  :"+actor1.face)
    println("  :"+actor1.costume)
    println("  :"+actor1.hairstyle)

    /**  *        */  val ab2: ActorBuilder = AngleBuilder
    val actor2 = ab2.construct(ab2)
    println(actor2.role+"   :")
    println("  :"+actor2.sex)
    println("  :"+actor2.face)
    println("  :"+actor2.costume)
    println("  :"+actor2.hairstyle)

    /**  *        */  val ab3: ActorBuilder = DevilBuilder
    val actor3 = ab3.construct(ab3)
    println(actor3.role+"   :")
    println("  :"+actor3.sex)
    println("  :"+actor3.face)
    println("  :"+actor3.costume)
    println("  :"+actor3.hairstyle)
}