** Table of Contents ** Abstract Factory I understand that only the concrete factory class part of the pattern corresponds to this pattern.
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.
-Product Abstract class of generated parts ・ ConcreteProduct Generated parts ・ Creator Abstract class of parts manufacturing factory ・ ConcreteCreator parts manufacturing factory
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.
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()
}
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).
Sports Federation Abstract Class
SportsFederation.kt
package factorymethod
abstract class SportsFederation {
fun getAthlete(): ProfessionalAthlete {
return dispatchRepresentativeAthlete()
}
protected abstract fun dispatchRepresentativeAthlete(): ProfessionalAthlete
}
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.
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]
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