** Table of Contents ** A pattern that creates objects by pooling and cloning parts.
Clarify the type of object to be created using the prototype instance, and create a new object by copying it.
-Prototype Abstract class that duplicates -ConcretePrototype A concrete class that duplicates -Client Class that requests replication
We will implement the prototype factory required for mass production of cars.
First, mount the parts made by the prototype factory.
** Engine class **
Engine.kt
package prototype
class Engine(displacement: Int): Cloneable {
var displacement = displacement
fun show() {
print("【engine】"+ displacement +"cc ")
}
public override fun clone(): Any {
return super.clone()
}
}
** Tire class **
Tire.kt
package prototype
class Tire(num: Int): Cloneable {
var num = num
fun show() {
print("【tire】" + num + "Pieces")
}
public override fun clone(): Any {
return super.clone()
}
}
The above are the parts. Then mount the product car.
** Car class **
Car.kt
package prototype
class Car(engine: Engine, tire: Tire) {
val engine = engine
val tire = tire
fun show() {
print("[Product] Car")
engine.show()
tire.show()
print("\n")
}
}
We will implement a factory that can mass-produce the Engine and Tire that have been implemented so far.
Prototype interface
ProtoType.kt
package prototype
/**
*Prototype
* Prototype
*/
interface ProtoType {
fun createEngine(): Engine
fun createTire(): Tire
}
Prototype factory class
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
}
}
Finally, we will implement a factory that receives parts from the prototype factory and mass-produces the products.
Car manufacturing factory class
Factory.kt
package prototype
class Factory {
var carList:MutableList<Car> = mutableListOf()
var prototypeFactory = ProtoTypeFactory()
init {
//Displacement set to 1000cc
prototypeFactory.engine.displacement = 1000
//Mass-produce three cars with a displacement of 1000cc
massProduction(3)
//Displacement set to 2000cc
prototypeFactory.engine.displacement = 2000
//Mass-produce two cars with a displacement of 2000cc
massProduction(2)
//Displacement set to 12000cc
prototypeFactory.engine.displacement = 12000
//Set 8 tires
prototypeFactory.tire.num = 8
//A bus that mass-produces five cars with eight 12000cc tires?
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()))
}
}
}
With the above, we have implemented a factory that can mass-produce prototypes one after another. Unlike the [Abstract Factory](# abstract-factory) pattern, even if you want to mass-produce various models, you can change the members of the parts of the `ProtoTypeFactory
class without implementing a new factory. I'm done.
[output]
[Product] Car [Engine] 1000cc [Tire] 4 pieces
[Product] Car [Engine] 1000cc [Tire] 4 pieces
[Product] Car [Engine] 1000cc [Tire] 4 pieces
[Product] Car [Engine] 1000cc [Tire] 4 pieces
[Product] Car [Engine] 2000cc [Tire] 4 pieces
[Product] Car [Engine] 2000cc [Tire] 4 pieces
[Product] Car [Engine] 2000cc [Tire] 4 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
[Product] Car [Engine] 12000cc [Tire] 8 pieces
Recommended Posts