Top Banner
Partitioning and Layering Fundamentals
23

Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Dec 31, 2015

Download

Documents

Lorin Pitts
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Partitioning and Layering Fundamentals

Page 2: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

The Basic Problem

Change is a fact of lifeRequirementsTechnologiesBug Fixes

Software Must Adapt

Page 3: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Solution: Software Layers

Reduce software couplingMinimize the consequences of changeFocused Unit Tests to verify changeExample of Code Refactoring

Page 4: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Fundamental Concepts

Difference between a type and an objectComplex typeCompositionInterface

Page 5: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Type

class Employee{

string name;…void Pay()….

}

Data

Behavior

Simple Type

Complex Type

Page 6: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Object

Employee emp = new Employee();

Page 7: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Object Composition

class Auto{

Engine engine;Wheel[4] wheels;

void Drive() {…};}

Page 8: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

class Engine{

string manufacturer;int horsepower;

void Start(){…};void Stop(){…};

}

class Wheel{string manufacturer;float tirePressure;

void Turn(){…};}

Object composition introduces dependencies

Page 9: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Write your applications so that the dependencies of one type on another are eliminated or minimized.

Page 10: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Make types dependent on type's behavior, not its implementation.

Page 11: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Unit tests verify that a type's behavior is correct.

Page 12: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Interfaces

interface IEngine{

void Start();void Stop();

}

interface IWheel{

void Turn();}

Interfaces describe behavior, not implementation

Page 13: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Rewritten Engine, Wheel Classes

class Engine : IEngine{ string manufacturer;

int horsepower;

void Start () {…}void Stop() {…}

}

class Wheel : IWheel{

string manufacturerfloat tirePressure;

void Turn();}

Page 14: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Interface Composition

class Auto{

IEngine engine;IWheel[4] wheels;

void Drive() {…}}

Page 15: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Interfaces Hide Implementation

class Diesel: IEngine{ string manufacturer;

int horsepower;

void Start () {…}void Stop() {…}

}

class WankelEngine : IEngine{

string manufacturer;int rotationSpeed;

void Start () {…}void Stop() {…}

}

Page 16: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Coupling

Preserve Essential CouplingEssential Semantics

Remove Inessential CouplingProgramming Artifacts

Page 17: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Electrical Analogy

Wall socket interface removes the inessential coupling due to the physical shape of plugs and appliances

An interface cannot remove the essential behavioral coupling of voltage and amperage of standard current

Page 18: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Complexity vs. Flexibility

Interfaces add a level of indirectionPut interfaces along application fault linesHard to refactor out of a bad design

Page 19: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Interfaces vs. Inheritance

Favor interface over object compositionInterface Composition vs. Inheritance?

class RacingCar : HighPerformanceCar : Auto

Static DefinitionNeed to Understand Base Class Behavior

Page 20: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

"Inheritance Breaks Encapsulation"

class HighPerformanceCar{

virtual void Start(){

TurnIgnition();Press GasPedal();

}…}

class RacingCar : HighPerformanceCar

{…}

Page 21: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Interfaces

Avoid Inheriting ImplementationRestrict Inessential CouplingMake Interfaces Easy to Modify

Page 22: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Design Patterns

Minimize Dependencies in Implementation Use Design PatternsElectrical Analogy

Design to work with 110 or 220 volts?Use Transformer PatternFlexibility even with Essential Coupling

Page 23: Partitioning and Layering Fundamentals. The Basic Problem Change is a fact of life RequirementsTechnologies Bug Fixes Software Must Adapt.

Summary

Reduce coupling and dependencies of complex types

Use Interface Based DesignUse Composition rather than Implementation

InheritanceWrite unit tests to validate behavior