KEMBAR78
Object Oriented Programming in Swift Ch0 - Encapsulation | PDF
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 Programming

by Josh Ko
Language
• Java
• Python
• Swift
How to build an
iPhone?
System -> Components
System -> Components
Divide and Conquer
System -> Components
Divide 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 time

No more disposable programs
Spaghetti Code
Spaghetti Code
Edible 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)
• Reuse
C 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) data

Assembly
Noodles -> Procedure -> Object
• NOP: spaghetti code + (possibly spaghetti) data

Assembly
• POP: organized code + (possibly organized) data

C, PASCAL

basic modules: procedures
Noodles -> Procedure -> Object
• NOP: spaghetti code + (possibly spaghetti) data

Assembly
• POP: organized code + (possibly organized) data

C, PASCAL

basic modules: procedures
• OOP: organized code + organized data (grouped)

Swift, Java, Python

basic 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 Programming

how 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 Programming

how 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 Programming

how to implement it
• class diagrams -> implementations
• OOP, this one
How to build an
iPhone?
Decomposition
Decomposition
System
Decomposition
System
object
objectobject
Decomposition
System
abstraction
object
objectobject
Decomposition
System
abstraction
object
objectobject
Decomposition
System
abstraction
object
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
Encapsulation
Access Control: public and private
Encapsulation
Access Control: public and private
Encapsulation
Access 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 State
private(set)
private(set)
Design by Contract
• Class Invariant
• Valid State
• 1 <= day <= 31

0 <= 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 oz
Think 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 & A

To be continued…
References
References
• Agile Software Development: Principles, Patterns, and Practice (
: )
References
• Agile Software Development: Principles, Patterns, and Practice (
: )
• Fundamental Object Oriented Programming
References
• Agile Software Development: Principles, Patterns, and Practice (
: )
• Fundamental Object Oriented Programming
•
References
• Agile Software Development: Principles, Patterns, and Practice (
: )
• Fundamental Object Oriented Programming
•
• Essential Object-Oriented Programming
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
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

Object Oriented Programming in Swift Ch0 - Encapsulation