- Hands-On Design Patterns with Kotlin
- Alexey Soshin
- 366字
- 2021-06-25 20:49:29
Making improvements
One may claim that having the VehicleFactory and Barracks classes is too cumbersome. They don't have any state, after all. Instead, we can replace them with objects.
Instead of the previous implementation of buildBarracks(), we can have the following:
fun buildBarracks(): Building<InfantryUnits, Infantry> {
val b = object : Building<InfantryUnits, Infantry> {
override fun build(type: InfantryUnits): Infantry {
return when (type) {
InfantryUnits.RIFLEMEN -> Rifleman()
InfantryUnits.ROCKET_SOLDIER -> RocketSoldier()
}
}
}
buildings.add(b)
return b
}
We've already seen two different usages of the object keyword: once in the Singleton design pattern, and another time in the Factory Method design pattern. Here is the third way we can use it: for creating anonymous classes on the fly. After all, Barracks is a building that, given InfantryUnitType, produces Infantry.
If our logic is straightforward, we can even shorten the declaration a bit more:
fun buildVehicleFactory(): Building<VehicleUnits, Vehicle> {
val vf = object : Building<VehicleUnits, Vehicle> {
override fun build(type: VehicleUnits) = when (type) {
VehicleUnits.APC -> APC()
VehicleUnits.TANK -> Tank()
}
}
buildings.add(vf)
return vf
}
Let's go to the beginning of this chapter. We said that Abstract Factory combines a number of related factories. So, what's common to all factories in our case? They're all buildings and they all produce units.
Having that principle in mind, you could apply it to many different cases. If you're familiar with strategy games, usually they have at least two different factions. Each may have different structures and units. To achieve that, you can repeat this pattern as many times as needed.
Let's assume we have two different factions now, cats and dogs, and Tanks and Rocket Infantry are only prerogatives of this faction. Dogs have Heavy Tanks and Grenadiers instead. What changes do we need to make in our system?
First, HQ becomes an interface:
interface HQ {
fun buildBarracks(): Building<InfantryUnits, Infantry>
fun buildVehicleFactory(): Building<VehicleUnits, Vehicle>
}
What was HQ previously now becomes CatHQ:
class CatHQ : HQ {
// Remember to add override to your methods
}
And DogHQ will have to repeat the same steps, but with a different construction logic.
This ability to accommodate big changes is what makes Abstract Factory so powerful in some use cases.
- Android Development with Kotlin
- 趣學(xué)Python算法100例
- Access 2010數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用項(xiàng)目式教程(第3版)
- 教孩子學(xué)編程:C++入門(mén)圖解
- Python算法從菜鳥(niǎo)到達(dá)人
- Java網(wǎng)絡(luò)編程核心技術(shù)詳解(視頻微課版)
- Service Mesh實(shí)戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實(shí)踐
- D3.js By Example
- Creating Data Stories with Tableau Public
- HTML5移動(dòng)前端開(kāi)發(fā)基礎(chǔ)與實(shí)戰(zhàn)(微課版)
- 程序員必會(huì)的40種算法
- Bitcoin Essentials
- PHP 7 Programming Blueprints
- Eclipse開(kāi)發(fā)(學(xué)習(xí)筆記)
- Visual C++ 開(kāi)發(fā)從入門(mén)到精通