SOLID 2000 , - . - , , , , . SOLID - .
, - , , , , . SOLID .
, SOLID .
…
. . , . , .
, SOLID, .
, . - .
class Calculate {
fun add (a, b) = a + b
fun sub (a, b) = a - b
fun mul (a, b) = a * b
fun div (a, b) = a / b
}
, - .
- : «! 4 ! , , ! »
? , .
Calculate
, . .
, - Calculate
. !
. , .
/
« ... , ».
- . , , , , .
:
interface Operation {
fun compute(v1: Int, v2: Int): Int
}
class Add:Operation {
override fun compute(v1: Int, v2: Int) = v1 + v2
}
class Sub:Operation {
override fun compute(v1: Int, v2: Int) = v1 - v2
}
class Calculator {
fun calculate(op: Operation, v1: Int, v2: Int): Int {
return op.compute(v1, v2)
}
}
Calculator
, Operation . Mul
Div
Calculator
.
class Mul:Operation {
override fun compute(v1: Int, v2: Int) = v1 * v2
}
class Div:Operation {
override fun compute(v1: Int, v2: Int) = v1 / v2
}
, /!
. C, — Inverse. , X, 1/X.
, ? ompute
, 2 . , 1 .
Calculator
? , , .
. , , :)
«, , , ».
, . .
interface Animal {
fun move()
}
class Mammal: Animal {
override move() = "walk"
}
class Bird: Animal {
override move() = "fly"
}
class Fish: Animal {
override move() = "swim"
}
fun howItMove(animal: Animal) {
animal.move()
}
.
, . , , . , :
class WalkingAnimal: Animal {
override move() = "walk"
}
class FlyingAnimal: Animal {
override move() = "fly"
}
class SwimmingAnimal: Animal {
override move() = "swim"
}
, - , Animal
:
fun howItMove(animal: Animal) {
animal.move()
}
- . , . Sessile. :
interface Animal
interface MovingAnimal: Animal {
move()
}
class Sessile: Animal {}
.
fun howItMove(animal: Animal) {
animal.move()
}
, howItMove
. , , . , , .
. - . .
« , ».
. Animal
, .
interface Animal {
fun move()
fun eat()
fun grow()
fun reproduction()
}
, , , , Sessile
. move
.
interface Animal {
fun eat()
fun grow()
fun reproduction()
}
interface MovingObject {
fun move()
}
class Sessile : Animal {
//...
}
class NonSessile : Animal, MovingObject {
//...
}
Plant
. , grow
reproduction
:
interface LivingObject {
fun grow()
fun reproduction()
}
interface Plant: LivingObject {
fun makeFood()
}
interface Animal: LivingObject {
fun eat()
}
interface MovingObject {
fun move()
}
class Sessile : Animal {
//...
}
class NonSessile : Animal, MovingObject {
//...
}
, . .
, - : «! , , LivingObject!».
, reproduction
LivingObject
.
, ! , , .
. , . , , , - .
« , - ».
, .
. .
interface Operation {
fun compute (v1: Int, v2: Int): Int
fun name (): String
}
class Add: Operation {
override fun compute (v1: Int, v2: Int) = v1 + v2
override fun name () = "Add"
}
class Sub: Operation {
override fun compute (v1: Int, v2: Int) = v1 - v2
override fun name () = "Subtract"
}
class Calculator {
fun calculate (op: Operation, v1: Int, v2: Int): Int {
println ("Running $ {op.name ()}")
return op.compute (v1, v2)
}
}
Calculator
Add
Sub
. Add
Sub
, Operation
. .
, - Android , . println
Android. Lod.d
.
, Calculator
println
. Printer
:
interface Printer {
fun print(msg: String)
}
class AndroidPrinter: Printer {
override fun print(msg: String) = Log.d("TAG", msg)
}
class NormalPrinter: Printer {
override fun print(msg: String) = println(msg)
}
class Calculator(val printer: Printer) {
fun calculate(op: Operation, v1: Int, v2: Int): Int {
printer.print("Running ${op.name()}")
return op.compute(v1, v2)
}
}
.
Android Calculator
, , , YAGNI.
TL; DR;
, SOLID - . , , , , , .
, . , , SOLID.
. . , SOLID.
SOLID . SOLID - , .