[GO] [Gang of Four] Design pattern learning --Prototype

Prototype-Prototype

** Table of Contents ** A pattern that creates objects by pooling and cloning parts.

Purpose

Clarify the type of object to be created using the prototype instance, and create a new object by copying it.

Component

-Prototype Abstract class that duplicates -ConcretePrototype A concrete class that duplicates -Client Class that requests replication

Implementation

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 Abstract class for duplication

Prototype interface

ProtoType.kt


package prototype

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

ConcretePrototype Concrete class that duplicates

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.

Client Class requesting replication

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 result

[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

[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning
[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 --Facade
[Gang of Four] Design pattern learning --Composite
[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 --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Gang of Four (GoF) Patterns in Python
Design Pattern #Builder
Design Pattern #Decorator
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
I wrote a design pattern in kotlin Prototype
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
Learn the design pattern "Chain of Responsibility" in Python
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
Prototype pattern in Java
Summary of Prototype patterns introductory design patterns learned in Java language
GoF design pattern is just an "introduction of abstraction layer"