-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_oriented_programming_in_go.slide
173 lines (110 loc) · 4.4 KB
/
object_oriented_programming_in_go.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
Object-Oriented Programming in Go
Go Seoul Meetup
24 Jan 2015
장재휴
Developer, Purpleworks
http://www.slideshare.net/JaehueJang/object-oriented-programming-in-go
* Who am I
- Elandsystems ➜ purpleworks
- Ruby, C#, Go
* Agenda
- OOP in Go
* What is "Object Oriented" (wikipedia)
- A language is usually considered *object-based* if it *includes* *the* *basic* *capabilities* *for* *an* *object*: identity, properties, and attributes
- A language is considered *object-oriented* if it *is* *object-based* and also *has* *the* *capability* *of* *polymorphism* *and* *inheritance*
* Is Go Object-based Language?
* What is an "Object" (wikipedia)
- An object is an abstract data type that has *state(data)* and *behavior(code)*
* Object in Go (via Custom Type & Method)
.play object_oriented_programming_in_go/object1.go
* Custom Type of built-in Type
.play object_oriented_programming_in_go/object2.go /START OMIT/,/END OMIT/
* Go is Object-based
- Via custom type and methods
* Is Go Object-oriented Language?
* What is "Inheritance" (wikipedia)
- Provides reuse of existing objects
- Classes are created in hierarchies
- Inheritance passes *knowledge* down!
* Inheritance is not good
In Java user group meeting,
James Gosling (Java’s inventor) says:
*You*should*avoid*implementation*inheritance*whenever*possible*.
* Go's approach
- Go avoided inheritance
- Go strictly follows the *Composition* *over* *inheritance* principle
# or Composite Reuse Principle
* What is Composition
- Provides reuse of Objects
- One object is declared by containing other objects
- Composition pulls *knowledge* into another
* Composition in Go
.play object_oriented_programming_in_go/composition1.go /START OMIT/,/END OMIT/
* Call Method of Embedded Type
.play object_oriented_programming_in_go/composition2.go /START OMIT/,/END OMIT/
* Method Overriding
.play object_oriented_programming_in_go/composition3.go /START OMIT/,/END OMIT/
* Composition in Go 2
.play object_oriented_programming_in_go/composition4.go /START OMIT/,/END OMIT/
* Composition in Go 3
.play object_oriented_programming_in_go/composition5.go /START OMIT/,/END OMIT/
* Composition in Go 4
.play object_oriented_programming_in_go/composition6.go /START OMIT/,/END OMIT/
* What is "Polymorphism" (wikipedia)
- The provision of a single interface to entities of different types
- Via *Generics*, *Overloading* and/or *Subtyping*
* Go’s approach
- Go avoided subtyping & overloading
- Go does not provide Generics
- Polymorphism via interfaces
* Interfaces in Go
- Interfaces are just sets of methods
- Interfaces define behavior (duck typing)
- "If something can do this, then it can be used here”
* Interfaces in Go
.code object_oriented_programming_in_go/interface1.go /START1 OMIT/,/END1 OMIT/
* Interfaces in Go
.play object_oriented_programming_in_go/interface1.go /START2 OMIT/,/END2 OMIT/
* Interfaces in Go 2
.play object_oriented_programming_in_go/interface.go /START OMIT/,/END OMIT/
* Satisfying Interface of Other Package
- `fmt.Println`
func Println(a ...interface{}) (n int, err error) {
return Fprintln(os.Stdout, a...)
}
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
...
// print using Stringer interface
...
return
}
- `fmt.Stringer`
type Stringer interface {
String() string
}
* Satisfying Interface of Other Package
.code object_oriented_programming_in_go/interface4.go /START OMIT/,/END OMIT/
* Satisfying Interface of Other Package
.play object_oriented_programming_in_go/interface4.go /START1 OMIT/,/END1 OMIT/
* Deep into Go's Standard Library
- `io.Writer` interface
// http://godoc.org/io#Writer
type Writer interface {
Write(p []byte) (n int, err os.Error)
}
- `fmt.Fprintln` function
func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
* The Power of Interfaces
In handle function, just write to *io.Writer* object
func handle(w io.Writer, msg string) {
fmt.Fprintln(w, msg)
}
The *os.Stdout* can be used for *io.Writer*.
.play object_oriented_programming_in_go/interface2.go /START OMIT/,/END OMIT/
* The Power of Interfaces
The *http.ResponseWriter* can be used for *io.Writer*.
.play object_oriented_programming_in_go/interface3.go /START OMIT/,/END OMIT/
.link http://localhost:4000/hello-world localhost:4000/hello-world
.link http://localhost:4000/this-is-an-example-of-io.Writer localhost:4000/this-is-an-example-of-io.Writer
* Go is Object-Oriented