[GO] [Gang of Four] Design pattern learning --Factory Method

Factory Method-Factory method

** Table of Contents ** Abstract Factory I understand that only the concrete factory class part of the pattern corresponds to this pattern.

Purpose

Specify only the interface when creating an object, and let the subclass decide which class to actually instantiate. The Factory Method pattern leaves instantiation to subclasses.

Component

-Product Abstract class of generated parts ・ ConcreteProduct Generated parts ・ Creator Abstract class of parts manufacturing factory ・ ConcreteCreator parts manufacturing factory

Implementation

Implement a program that encourages professional athletes to swear by the organizer. The representative athletes who take the oath will be dispatched by their respective sports federations.

Product Abstract class of generated parts

Professional athlete abstract class

ProfessionalAthlete.kt


package factorymethod

abstract class ProfessionalAthlete(type: Type) {
    enum class Type(val value: String) {
        Soccer("Football"),
        BaseBall("baseball")
    }

    val myType = type

    abstract fun athletesOath()
}

ConcreteProduct Generated parts

Professional soccer player concrete class

ProfessionalSoccerAthlete.kt


package factorymethod

class ProfessionalSoccerAthlete(name: String): ProfessionalAthlete(Type.Soccer) {

    private val name = name

    override fun athletesOath() {
        println("oath! I" + name + "is. Professional【" + myType.value + "] Play openly with a soccer ball as a player!")
    }
}

Professional baseball player concrete class

ProfessionalBaseBallAthlete.kt


package factorymethod

class ProfessionalBaseBallAthlete(name: String): ProfessionalAthlete(Type.BaseBall) {

    private val name = name

    override fun athletesOath() {
        println("oath! I" + name + "is. Professional【" + myType.value + "] As a player, use a bat and a baseball ball to play openly!")
    }
}

With the above, the player (product) has been implemented. Next, we will implement a sports federation (factory) that dispatches (creates) athletes (products).

Creator Abstract class of parts manufacturing factory

Sports Federation Abstract Class

SportsFederation.kt


package factorymethod

abstract class SportsFederation {
    fun getAthlete(): ProfessionalAthlete {
        return dispatchRepresentativeAthlete()
    }

    protected abstract fun dispatchRepresentativeAthlete(): ProfessionalAthlete
}

ConcreteCreator Parts Manufacturing Factory

Soccer Federation Concrete Class

SoccerFederation.kt


package factorymethod

class SoccerFederation: SportsFederation() {
    override fun dispatchRepresentativeAthlete(): ProfessionalAthlete {
        return ProfessionalSoccerAthlete("Representative Taro Sakada")
    }
}

Baseball Federation concrete class

BaseBallFederation.kt


package factorymethod

class BaseBallFederation: SportsFederation() {
    override fun dispatchRepresentativeAthlete(): ProfessionalAthlete {
        return ProfessionalBaseBallAthlete("Baseball book captain man")
    }
}

Finally, we will implement an organizer who will request each sports federation (factory) for a representative player (product) and swear.

User

Organizer class

Organizer.kt


package factorymethod

class Organizer {
    init {
        var athleteList: MutableList<ProfessionalAthlete> = mutableListOf()
        //Have representative athletes dispatched from each sports federation
        //Acquired representative player from the soccer federation
        athleteList.add(SoccerFederation().getAthlete())
        //Obtained a representative player from the baseball federation
        athleteList.add(BaseBallFederation().getAthlete())

        //Get sworn
        for (athlete in athleteList) {
            athlete.athletesOath()
        }
    }
}

Now we have a pattern that can be used when the class of the object that must be created cannot be known in advance. I feel like.

Output result

[output]
oath! I'm Representative Taro Sakada. As a professional [soccer] player, play soccer balls in a straightforward manner!
oath! I'm a baseball book captain man. As a professional [baseball] player, you will play with a bat and a baseball ball.

Recommended Posts

[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Template Method
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Chain of Responsibility
Design Pattern #Factory Method
Learn the design pattern "Factory Method" in Python
Design Pattern #Template Method
Python Design Pattern --Template method
Introduction of data-driven controller design method
Gang of Four (GoF) Patterns in Python
Learn the design pattern "Template Method" in Python
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Clustering of clustering method
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
I wrote a design pattern in kotlin Factory edition
Learn the design pattern "Chain of Responsibility" in Python