Skip to content

Commit 8e71772

Browse files
authored
Merge pull request #4978 from hozlucas28/Solution-29-Go
#29 - Go
2 parents 1197186 + a34f33e commit 8e71772

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
/* -------------------------------------------------------------------------- */
8+
/* INTERFACES */
9+
/* -------------------------------------------------------------------------- */
10+
11+
type BadVehicle interface {
12+
Drive()
13+
Fly()
14+
}
15+
16+
type AirVehicle interface {
17+
Fly()
18+
}
19+
20+
type LandVehicle interface {
21+
Drive()
22+
}
23+
24+
type BlackAndWhitePrinter interface {
25+
Print()
26+
}
27+
28+
type ColorPrinter interface {
29+
Print()
30+
}
31+
32+
type MultifunctionalPrinter interface {
33+
Print()
34+
Scan()
35+
SendFax()
36+
}
37+
38+
/* -------------------------------------------------------------------------- */
39+
/* CLASSES */
40+
/* -------------------------------------------------------------------------- */
41+
42+
/* ------------------------------- BadAirplane ------------------------------ */
43+
44+
type badAirplane struct{}
45+
46+
func NewBadAirplane() BadVehicle {
47+
var airplane badAirplane = badAirplane{}
48+
return &airplane
49+
}
50+
51+
func (airplane *badAirplane) Drive() {
52+
panic("Airplanes do not drive")
53+
54+
}
55+
56+
func (airplane *badAirplane) Fly() {
57+
fmt.Println("The airplane is flying!")
58+
}
59+
60+
/* --------------------------------- BadCar --------------------------------- */
61+
62+
type badCar struct{}
63+
64+
func NewBadCar() BadVehicle {
65+
var car badCar = badCar{}
66+
return &car
67+
}
68+
69+
func (car *badCar) Drive() {
70+
fmt.Println("The car is driving!")
71+
}
72+
73+
func (car *badCar) Fly() {
74+
panic("Cars do not fly")
75+
}
76+
77+
/* ------------------------------ GoodAirplane ------------------------------ */
78+
79+
type goodAirplane struct{}
80+
81+
func NewGoodAirplane() AirVehicle {
82+
var airplane goodAirplane = goodAirplane{}
83+
return &airplane
84+
}
85+
86+
func (airplane *goodAirplane) Fly() {
87+
fmt.Println("The airplane is flying!")
88+
}
89+
90+
/* --------------------------------- GoodCar -------------------------------- */
91+
92+
type goodCar struct{}
93+
94+
func NewGoodCar() LandVehicle {
95+
var car goodCar = goodCar{}
96+
return &car
97+
}
98+
99+
func (car *goodCar) Drive() {
100+
fmt.Println("The car is driving!")
101+
}
102+
103+
/* -------------------------- BlackAndWhitePrinter -------------------------- */
104+
105+
type blackAndWhitePrinter struct{}
106+
107+
func NewBlackAndWhitePrinter() BlackAndWhitePrinter {
108+
var printer blackAndWhitePrinter = blackAndWhitePrinter{}
109+
return &printer
110+
}
111+
112+
func (printer *blackAndWhitePrinter) Print() {
113+
fmt.Println("Paper printed in black and white!")
114+
}
115+
116+
/* ------------------------------ ColorPrinter ------------------------------ */
117+
118+
type colorPrinter struct{}
119+
120+
func NewColorPrinter() ColorPrinter {
121+
var printer colorPrinter = colorPrinter{}
122+
return &printer
123+
}
124+
125+
func (printer *colorPrinter) Print() {
126+
fmt.Println("Paper printed in color!")
127+
}
128+
129+
/* ------------------------- MultifunctionalPrinter ------------------------- */
130+
131+
type multifunctionalPrinter struct{}
132+
133+
func NewMultifunctionalPrinter() MultifunctionalPrinter {
134+
var printer multifunctionalPrinter = multifunctionalPrinter{}
135+
return &printer
136+
}
137+
138+
func (printer *multifunctionalPrinter) Print() {
139+
fmt.Println("Paper printed!")
140+
}
141+
142+
func (printer *multifunctionalPrinter) Scan() {
143+
fmt.Println("Scan completed!")
144+
}
145+
146+
func (printer *multifunctionalPrinter) SendFax() {
147+
fmt.Println("Fax sent!")
148+
}
149+
150+
/* -------------------------------------------------------------------------- */
151+
/* MAIN */
152+
/* -------------------------------------------------------------------------- */
153+
154+
func main() {
155+
/*
156+
Interface Segregation Principle (ISP)...
157+
*/
158+
159+
fmt.Println("Interface Segregation Principle (ISP)...")
160+
161+
fmt.Println("\nBad implementation of Interface Segregation Principle (ISP)...")
162+
163+
fmt.Println("\n```\n" + `type BadVehicle interface {
164+
Drive()
165+
Fly()
166+
}
167+
168+
type badAirplane struct{}
169+
170+
func NewBadAirplane() BadVehicle {
171+
var airplane badAirplane = badAirplane{}
172+
return &airplane
173+
}
174+
175+
func (airplane *badAirplane) Drive() {
176+
panic("Airplanes do not drive")
177+
178+
}
179+
180+
func (airplane *badAirplane) Fly() {
181+
fmt.Println("The airplane is flying!")
182+
}
183+
184+
type badCar struct{}
185+
186+
func NewBadCar() BadVehicle {
187+
var car badCar = badCar{}
188+
return &car
189+
}
190+
191+
func (car *badCar) Drive() {
192+
fmt.Println("The car is driving!")
193+
}
194+
195+
func (car *badCar) Fly() {
196+
panic("Cars do not fly")
197+
}` + "\n```")
198+
199+
fmt.Println(
200+
"\nThis is a bad implementation of Interface Segregation Principle (ISP),\n" +
201+
"because the 'badAirplane' class and the 'badCar' class should not implement\n" +
202+
"an interface with methods that they are going to never use. So, the implemented\n" +
203+
"interface ('BadVehicle') is to general for both classes.",
204+
)
205+
206+
fmt.Println("\nGood implementation of Interface Segregation Principle (ISP)...")
207+
208+
fmt.Println("\n```\n" + `type AirVehicle interface {
209+
Fly()
210+
}
211+
212+
type LandVehicle interface {
213+
Drive()
214+
}
215+
216+
type goodAirplane struct{}
217+
218+
func NewGoodAirplane() AirVehicle {
219+
var airplane goodAirplane = goodAirplane{}
220+
return &airplane
221+
}
222+
223+
func (airplane *goodAirplane) Fly() {
224+
fmt.Println("The airplane is flying!")
225+
}
226+
227+
type goodCar struct{}
228+
229+
func NewGoodCar() LandVehicle {
230+
var car goodCar = goodCar{}
231+
return &car
232+
}
233+
234+
func (car *goodCar) Drive() {
235+
fmt.Println("The car is driving!")
236+
}` + "\n```")
237+
238+
fmt.Println(
239+
"\nThis is a good implementation of Interface Segregation Principle (ISP),\n" +
240+
"because the 'goodAirplane' class and the 'goodCar' class only implements\n" +
241+
"the necessary methods, without any extras.",
242+
)
243+
244+
fmt.Println(
245+
"\n# ---------------------------------------------------------------------------------- #",
246+
)
247+
248+
/*
249+
Additional challenge...
250+
*/
251+
252+
fmt.Println("\nAdditional challenge...")
253+
254+
var blackAndWhitePrinter BlackAndWhitePrinter = NewBlackAndWhitePrinter()
255+
var colorPrinter ColorPrinter = NewColorPrinter()
256+
var multifunctionalPrinter MultifunctionalPrinter = NewMultifunctionalPrinter()
257+
258+
fmt.Println()
259+
blackAndWhitePrinter.Print()
260+
261+
fmt.Println()
262+
colorPrinter.Print()
263+
264+
fmt.Println()
265+
multifunctionalPrinter.Print()
266+
267+
fmt.Println()
268+
multifunctionalPrinter.Scan()
269+
270+
fmt.Println()
271+
multifunctionalPrinter.SendFax()
272+
}

0 commit comments

Comments
 (0)