partitioning and layering fundamentals. the basic problem change is a fact of life...

23
Partitioning and Layering Fundamentals

Upload: lorin-pitts

Post on 31-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Partitioning and Layering Fundamentals

The Basic Problem

Change is a fact of lifeRequirementsTechnologiesBug Fixes

Software Must Adapt

Solution: Software Layers

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

Fundamental Concepts

Difference between a type and an objectComplex typeCompositionInterface

Type

class Employee{

string name;…void Pay()….

}

Data

Behavior

Simple Type

Complex Type

Object

Employee emp = new Employee();

Object Composition

class Auto{

Engine engine;Wheel[4] wheels;

void Drive() {…};}

class Engine{

string manufacturer;int horsepower;

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

}

class Wheel{string manufacturer;float tirePressure;

void Turn(){…};}

Object composition introduces dependencies

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

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

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

Interfaces

interface IEngine{

void Start();void Stop();

}

interface IWheel{

void Turn();}

Interfaces describe behavior, not implementation

Rewritten Engine, Wheel Classes

class Engine : IEngine{ string manufacturer;

int horsepower;

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

}

class Wheel : IWheel{

string manufacturerfloat tirePressure;

void Turn();}

Interface Composition

class Auto{

IEngine engine;IWheel[4] wheels;

void Drive() {…}}

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() {…}

}

Coupling

Preserve Essential CouplingEssential Semantics

Remove Inessential CouplingProgramming Artifacts

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

Complexity vs. Flexibility

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

Interfaces vs. Inheritance

Favor interface over object compositionInterface Composition vs. Inheritance?

class RacingCar : HighPerformanceCar : Auto

Static DefinitionNeed to Understand Base Class Behavior

"Inheritance Breaks Encapsulation"

class HighPerformanceCar{

virtual void Start(){

TurnIgnition();Press GasPedal();

}…}

class RacingCar : HighPerformanceCar

{…}

Interfaces

Avoid Inheriting ImplementationRestrict Inessential CouplingMake Interfaces Easy to Modify

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

Summary

Reduce coupling and dependencies of complex types

Use Interface Based DesignUse Composition rather than Implementation

InheritanceWrite unit tests to validate behavior