105
Object Oriented Programming in Swift Ch 0 Sai Li @ Yowoo Tech. 2016/11/18

Object Oriented Programming in Swift Ch0 - Encapsulation

Embed Size (px)

Citation preview

Object Oriented Programming in Swift

Ch 0

Sai Li @ Yowoo Tech. 2016/11/18

References

References • Agile Software Development: Principles, Patterns,

and Practice ( : ) by Robert C. Martin

References • Agile Software Development: Principles, Patterns,

and Practice ( : ) by Robert C. Martin

• Fundamental Object Oriented Programming by Hsuan-Tien Lin

References • Agile Software Development: Principles, Patterns,

and Practice ( : ) by Robert C. Martin

• Fundamental Object Oriented Programming by Hsuan-Tien Lin

• by Teddy Chen

References • Agile Software Development: Principles, Patterns,

and Practice ( : ) by Robert C. Martin

• Fundamental Object Oriented Programming by Hsuan-Tien Lin

• by Teddy Chen

• Essential Object-Oriented Programmingby Josh Ko

Language

• Java

• Python

• Swift

How to build an iPhone?

System -> Components

System -> ComponentsDivide and Conquer

System -> ComponentsDivide and Conquer

Modularization

Good Modules make good System

Good Modules make good System

• Easy to debug

Good Modules make good System

• Easy to debug

• Easy to maintain/ manage

Good Modules make good System

• Easy to debug

• Easy to maintain/ manage

• Easy to modify/ update

Good Modules make good System

• Easy to debug

• Easy to maintain/ manage

• Easy to modify/ update

• Easy to reuse

Good Modules make good System

• Easy to debug

• Easy to maintain/ manage

• Easy to modify/ update

• Easy to reuse

Good Modules make good System

• Easy to debug

• Easy to maintain/ manage

• Easy to modify/ update

• Easy to reuse

Modularization: Save time in the future

Save money, save timeNo more disposable programs

Spaghetti Code

Spaghetti CodeEdible is enough

Spaghetti Code

Spaghetti Code

Noodle Oriented Programming

Noodle -> Procedure

• More organized than spaghetti code

• Identify the purpose

• Isolate (modularize)

• Reuse

Noodle -> Procedure

• More organized than spaghetti code

• Identify the purpose

• Isolate (modularize)

• ReuseC language

OOP• Grouped related data

OOP• Use Struct/ Class to generate objects

OOP• Don’t do something on the object

• Let object takes actions

OOP• Don’t do something on the object

• Let object takes actions

OOP• Don’t do something on the object

• Let object takes actions

Noodles -> Procedure -> Object

• NOP: spaghetti code + (possibly spaghetti) dataAssembly

Noodles -> Procedure -> Object

• NOP: spaghetti code + (possibly spaghetti) dataAssembly

• POP: organized code + (possibly organized) data C, PASCALbasic modules: procedures

Noodles -> Procedure -> Object

• NOP: spaghetti code + (possibly spaghetti) dataAssembly

• POP: organized code + (possibly organized) data C, PASCALbasic modules: procedures

• OOP: organized code + organized data (grouped) Swift, Java, Pythonbasic modules: Class / Struct

Noodles -> Procedure -> Object

OO Levels

OO Levels• OO Analysis:

what the system does

OO Levels• OO Analysis:

what the system does• needs -> models

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it• model -> class diagrams

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it• model -> class diagrams• Design Pattern

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it• model -> class diagrams• Design Pattern

• OO Programminghow to implement it

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it• model -> class diagrams• Design Pattern

• OO Programminghow to implement it• class diagrams -> implementations

OO Levels• OO Analysis:

what the system does• needs -> models• Software Engineering

• OO Design:how the system dose it• model -> class diagrams• Design Pattern

• OO Programminghow to implement it• class diagrams -> implementations• OOP, this one

How to build an iPhone?

Decomposition

Decomposition

System

Decomposition

Systemobject

objectobject

Decomposition

System

abstractionobject

objectobject

Decomposition

System

abstractionobject

objectobject

Decomposition

System

abstractionobject

Modeling

objectobject

Data Abstraction

Object

Data Abstraction

Object

method

Data Abstraction

Object

method

Data Abstraction

Object

method

Data Abstraction

Object

method

Interface

Data Abstraction

Object

method

Interface

Implementation

Data Abstraction

Abstraction is selective ignorance. by Andrew Koenig

Data Abstraction

Abstraction is selective ignorance. by Andrew Koenig

Others care about the interface and ignore the implementation

EncapsulationAccess Control: public and private

EncapsulationAccess Control: public and private

EncapsulationAccess Control: public and private

Design by Contract

Design by Contract• Class Invariant

Design by Contract• Class Invariant

• Valid State

Design by Contract• Class Invariant

• Valid State

Design by Contract• Class Invariant

• Valid Stateprivate(set)private(set)

Design by Contract• Class Invariant

• Valid State

• 1 <= day <= 310 <= hour <= 24

private(set)private(set)

Design by Contract• Preconditions, postconditions

Design by Contract• Preconditions, postconditions

First class invariant ?

Design by Contract• Preconditions, postconditions

First class invariant ?Constructor

Encapsulation

• Separate implementation and use

Encapsulation

• Separate implementation and use

• Don’t trust other people including you(3 months after)

Encapsulation

• Separate implementation and use

• Don’t trust other people including you(3 months after)

• Hide unnecessary details

Encapsulation

• Separate implementation and use

• Don’t trust other people including you(3 months after)

• Hide unnecessary details

• One more oz

Encapsulation

• Separate implementation and use

• Don’t trust other people including you(3 months after)

• Hide unnecessary details

• One more oz

Encapsulation

• Separate implementation and use

• Don’t trust other people including you(3 months after)

• Hide unnecessary details

• One more ozThink about incorrect use about your class

Encapsulation

As a designer, you should avoid giving the users of your code too much freedom to do bad and/or make bugs

accessor (getter)

accessor (getter)

accessor (getter)

mutator (setter)

OOP

• Encapsulation: Security

• Inheritance: Reusability

• Polymorphism: Flexibility

Q & ATo be continued…

References

References • Agile Software Development: Principles, Patterns, and Practice (

: )

• Fundamental Object Oriented Programming

• Essential Object-Oriented Programming

• images.techtimes.com/data/images/full/11234/tangled-earbuds.jpg

• spaghetticrunch.com/wp-content/uploads/2014/12/spaghetti-bolognaise-1.png

References • Agile Software Development: Principles, Patterns, and Practice (

: )

• Fundamental Object Oriented Programming

• Essential Object-Oriented Programming

• images.techtimes.com/data/images/full/11234/tangled-earbuds.jpg

• spaghetticrunch.com/wp-content/uploads/2014/12/spaghetti-bolognaise-1.png

• upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Design_by_contract.svg/220px-Design_by_contract.svg.png

References • Agile Software Development: Principles, Patterns, and Practice (

: )

• Fundamental Object Oriented Programming

• Essential Object-Oriented Programming

• images.techtimes.com/data/images/full/11234/tangled-earbuds.jpg

• spaghetticrunch.com/wp-content/uploads/2014/12/spaghetti-bolognaise-1.png

• upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Design_by_contract.svg/220px-Design_by_contract.svg.png

• www.linuxdigest.org/wp-content/uploads/2012/06/09fig091.gif