Applying Object Composition to Build Rich Domain Models

Preview:

Citation preview

Applying Object Composition to Build Rich Domain Models

Zoran HorvatCEO & Principal Consultant, Coding Helmet Consultancy zh@sysexpand.com http://codinghelmet.com zoranh75

https://pluralsight.com/authors/zoran-horvat

zh@sysexpand.com http://codinghelmet.com zoranh75

Animal obj = ...

Animal obj = ...if (obj is Mammal){}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}else if (obj is WaterMammal){ WaterMammal mammal = (WaterMammal)obj; // e.g. a whale // Pull mammal’s fins using all your strength // Swim as fast as you can}

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}else if (obj is WaterMammal){ WaterMammal mammal = (WaterMammal)obj; // e.g. a whale // Pull mammal’s fins using all your strength // Swim as fast as you can}

Logicduplication,but no true

code duplication!

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}else if (obj is WaterMammal){ WaterMammal mammal = (WaterMammal)obj; // e.g. a whale // Pull mammal’s fins using all your strength // Swim as fast as you can}

.

Logicduplication,but no true

code duplication!

Animal obj = ...if (obj is Mammal){ Mammal mammal = (Mammal)obj; // e.g. a tiger // Pull mammal’s tail using all your strength // Run as fast as you can}else if (obj is WaterMammal){ WaterMammal mammal = (WaterMammal)obj; // e.g. a whale // Pull mammal’s fins using all your strength // Swim as fast as you can}

.

Animal Classification Environment

Abilities

Animal Classification Environment

Abilities

Mammal Ground Walk, Run

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground Walk

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)Mammal SaltWater Surface, Dive,

UnderwaterMammal SaltWater Surface, Dive,

Underwater

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)Mammal SaltWater Surface, Dive,

UnderwaterMammal SaltWater Surface, Dive,

UnderwaterBonyFish SaltWater Underwater, GlideBonyFish FreshWater Underwater

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)Mammal SaltWater Surface, Dive,

UnderwaterMammal SaltWater Surface, Dive,

UnderwaterBonyFish SaltWater Underwater, GlideBonyFish FreshWater Underwater

Bird Air, Ground Fly, WalkBird Air, Ground Fly

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)Mammal SaltWater Surface, Dive,

UnderwaterMammal SaltWater Surface, Dive,

UnderwaterBonyFish SaltWater Underwater, GlideBonyFish FreshWater Underwater

Bird Air, Ground Fly, WalkBird Air, Ground Fly

Mammal Ground Walk, Run, Glide

Animal Classification Environment

Abilities

Mammal Ground Walk, RunMammal Ground Walk, Run

Bird Ground Walk, RunBird Ground Walk, Run

Reptile Ground WalkGastropod Ground Walk (?)Mammal SaltWater Surface, Dive,

UnderwaterMammal SaltWater Surface, Dive,

UnderwaterBonyFish SaltWater Underwater, GlideBonyFish FreshWater Underwater

Bird Air, Ground Fly, WalkBird Air, Ground Fly

Mammal Ground Walk, Run, GlideMammal Air, Ground Fly

Doubledispatch

ability.Accept(visitor);

void Run.Accept(AbilityVisitor visitor){ visitor.Accept(this); // Accept(Run)}

Questions?

Zoran HorvatCEO & Principal Consultant, Coding Helmet Consultancy zh@sysexpand.com http://codinghelmet.com zoranh75

Recommended