Upload
naren2k8
View
217
Download
0
Embed Size (px)
Citation preview
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
1/23
Partitioning and LayeringPartitioning and LayeringFundamentalsFundamentals
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
2/23
The Basic ProblemThe Basic Problem
Change is a factChange is a fact of lifeof life
RequirementsRequirements
TechnologiesTechnologiesBug FixesBug Fixes
Software Must AdaptSoftware Must Adapt
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
3/23
Solution: SoftwareSolution: Software LayersLayers
Reduce software couplingReduce software coupling
Minimize the consequences of changeMinimize the consequences of change
Focused Unit Tests to verify changeFocused Unit Tests to verify changeExample of Code RefactoringExample of Code Refactoring
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
4/23
Fundamental ConceptsFundamental Concepts
DDifference between a type and an objectifference between a type and an object
CComplex typeomplex type
CompositionCompositionInterfaceInterface
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
5/23
TypeType
class Employeeclass Employee
{{
string name;string name;
void Pay()void Pay()
..
}}
Data
Behavior
Simple Type
Complex
Type
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
6/23
ObjectObject
Employee emp = new Employee();Employee emp = new Employee();
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
7/23
Object CompositionObject Composition
class Auto
{
Engine engine;Wheel[4] wheels;
void Drive() {};
}
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
8/23
class Engine
{
string manufacturer;
int horsepower;
void Start(){};
void Stop(){};
}
class Wheel
{
string manufacturer;
float tirePressure;
void Turn(){};
}
Object composition introduces dependencies
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
9/23
Write yourWrite your applications so thatso that
the dependencies of one type onthe dependencies of one type onanother are eliminated oranother are eliminated orminimized.minimized.
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
10/23
MMake types dependent on type'sake types dependent on type'sbehavior, not its implementation.behavior, not its implementation.
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
11/23
Unit tests verify that a type'sbehavior is correct.
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
12/23
InterfacesInterfaces
interface IEngine
{
void Start();
void Stop();
}
interface IWheel
{
void Turn();
}
Interfaces describe behavior, not implementation
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
13/23
Rewritten Engine, WheelRewritten Engine, WheelClassesClasses
class Engine : IEngine
{
string manufacturer;
int horsepower;
void Start () {}
void Stop() {}
}
class Wheel : IWheel
{
string manufacturer
float tirePressure;
void Turn();
}
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
14/23
Interface CompositionInterface Composition
class Auto
{
IEngine engine;
IWheel[4] wheels;
void Drive() {}
}
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
15/23
Interfaces Hide ImplementationInterfaces Hide Implementation
class Diesel: IEngine
{
string manufacturer;
int horsepower;
void Start () {}
void Stop() {}
}
class WankelEngine : IEngine
{
string manufacturer;
int rotationSpeed;
void Start () {}
void Stop() {}
}
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
16/23
CouplingCoupling
Preserve Essential CouplingPreserve Essential Coupling
Essential SemanticsEssential Semantics
Remove Inessential CouplingRemove Inessential CouplingProgramming ArtifactsProgramming Artifacts
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
17/23
Electrical AnalogyElectrical Analogy
Wall socketWall socket interfaceinterface removes the inessentialremoves the inessentialcoupling due to the physical shape of plugscoupling due to the physical shape of plugsand appliancesand appliances
An interface cannot remove the essentialAn interface cannot remove the essentialbehavioral coupling of voltage and amperagebehavioral coupling of voltage and amperage
of standard currentof standard current
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
18/23
Complexity vs. FlexibilityComplexity vs. Flexibility
Interfaces add a level of indirectionInterfaces add a level of indirection
Put interfaces along application fault linesPut interfaces along application fault lines
Hard to refactor out of a bad designHard to refactor out of a bad design
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
19/23
Interfaces vs. InheritanceInterfaces vs. Inheritance
Favor interface over object compositionFavor interface over object composition
Interface Composition vs. Inheritance?Interface Composition vs. Inheritance?
class RacingCar : HighPerformanceCar : Autoclass RacingCar : HighPerformanceCar : Auto
Static DefinitionStatic Definition
Need to Understand Base Class BehaviorNeed to Understand Base Class Behavior
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
20/23
"Inheritance Breaks Encapsulation""Inheritance Breaks Encapsulation"
class HighPerformanceCarclass HighPerformanceCar
{{
virtual void Start()virtual void Start()
{{TurnIgnition();TurnIgnition();
Press GasPedal();Press GasPedal();
}}
}}
class RacingCar :class RacingCar :HighPerformanceCarHighPerformanceCar
{{
}}
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
21/23
InterfacesInterfaces
Avoid Inheriting ImplementationAvoid Inheriting Implementation
Restrict Inessential CouplingRestrict Inessential Coupling
Make Interfaces Easy to ModifyMake Interfaces Easy to Modify
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
22/23
Design PatternsDesign Patterns
Minimize Dependencies in ImplementationMinimize Dependencies in Implementation
Use Design PatternsUse Design Patterns
Electrical AnalogyElectrical AnalogyDesign to work with 110 or 220 volts?Design to work with 110 or 220 volts?
Use TransformerPatternUse TransformerPattern
Flexibility even with Essential CouplingFlexibility even with Essential Coupling
8/6/2019 01-PowerPoint Partitioning and Layering Fundamentals
23/23
SummarySummary
Reduce coupling and dependencies of complexReduce coupling and dependencies of complextypestypes
Use Interface Based DesignUse Interface Based Design
Use Composition rather than ImplementationUse Composition rather than ImplementationInheritanceInheritance
Write unit tests to validate behaviorWrite unit tests to validate behavior