Skip to content

Commit

Permalink
Preparation for lecture 3
Browse files Browse the repository at this point in the history
  • Loading branch information
qa-stefan-langer committed Oct 23, 2023
1 parent 79a155d commit f299b36
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 55 deletions.
46 changes: 21 additions & 25 deletions docs/03-Go-Programming-OOP.slide
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
Go Programming - OOP
Concepts of Programming Languages
24 Oct 2022
Tags: go, programming, master

Sebastian Macke
Sebastian Macke, Stefan Langer
Rosenheim Technical University
Sebastian.Macke@th-rosenheim.de
https://www.qaware.de


* Organisational matters
- Email from Sebastian
- Any other questions?

* Last lecture
- Types (string, int, bool, float64, ...)
- weak vs. strong typing
- Functions and Control Structures
- Unit Tests
- Arrays, Slices and Maps
- Pointer
- Unit Tests
- Preview into interfaces

* Last Exercise
- Let's look at an example implementation

: Ergebnisse der Übungen zeigen.

Expand Down Expand Up @@ -55,6 +63,7 @@ The "defer" statement lets us ensure that code runs before a function exits
* Exception Pro and Cons

- What do you think are the pros and cons of exceptions?
- [[https://miro.com/app/board/uXjVNekj0S8=/?share_link_id=355865373286]]

: * Exception Pro and Cons

Expand Down Expand Up @@ -220,10 +229,14 @@ The variable *r* is in both cases similar to the Java *this*, which reference to
: Object Oriented Programming - Custom Types
: Go has no classes but types and functions

* Syntax level OOP
.play ../src/oop/oop_vs_procedural/bool.go
* Composition VS. Inheritance?
- Composition: A design principle where objects are formed by combining multiple smaller objects
- Inheritance: A mechanism where a new class derives properties and behaviors from an existing class


- Which Pros and Cons exist?
- [[https://miro.com/app/board/uXjVNekj0S8=/?share_link_id=355865373286]]

- Go seems to convert the OOP style to procedural style. (OOP only on syntax level)

* Embedding
- Go does not support inheritance: Go supports embedding of other structs.
Expand Down Expand Up @@ -296,7 +309,7 @@ The print functions in the *fmt* package support the following interface
}

- Every type with a String() Method is detected by the print commands and the String function is executed
- Detection identical to the dynamic slide type
- Detection if object is type of interface

if tmp, ok := object.(Stringer); ok {
// The object implements stringer
Expand All @@ -306,29 +319,17 @@ The print functions in the *fmt* package support the following interface

.play ../src/oop/stringer/stringer.go

* Interfaces and Polymorphism
.play ../src/oop/polymorphism/polymorphism.go /func main/,/END2 OMIT/

* Recap: Go does support a dynamic type

.play ../src/basics/dynamic/main.go /START OMIT/,/END OMIT/

- The type _any_ is actually just an empty _interface{}_


* Example: Send Mail with Go: A minimal Interface
.code ../src/oop/mail/mail.go /Message/,/END OMIT/
- A example interface for a service-oriented component

* Example: A type implements an interface when providing the required methods
.code ../src/oop/mail/smtp/sender.go /Package/,/END OMIT/
.code ../src/oop/mail/smtp/sender.go /type/,/END OMIT/

* Example: The Go interface can be used as in Java
.code ../src/oop/mail/client/client.go /Package/,/EOF OMIT/

* Filesystem Mocking via Filesystem API

- Live Demo

* Summary
- Go does support Encapsulation via an OOP style syntax
Expand All @@ -340,11 +341,6 @@ The print functions in the *fmt* package support the following interface

: Inheritance can cause weak encapsulation, tight coupling and surprising bugs

* Video
.link https://youtu.be/Ng8m5VXsn8Q?t=414
: Start at 6:54 (Object orientation in Go)
: Stop at 32:06 (Concurrency)

: * Embedding
: .code ../src/oop/embedding/embedding.go /Introducer/,/EOE OMIT/

Expand Down
12 changes: 1 addition & 11 deletions docs/exercises/Exercise3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

If you do not finish during the lecture period, please finish it as homework.

## Stack 3.1 (Containers)

Write a generic LIFO container (stack) for all types by using `any` as dynamic type by using the OOP approach.
The stack should have at least two methods:

- Push(object)
- Pop()

Use the array append function for Push and the slices feature for Pop

## Exercise 3.2 - Interfaces, Polymorphism and Embedding
## Interfaces, Polymorphism and Embedding

The image shows a typical UML design with inheritance, aggregation and polymorph methods.

Expand Down
21 changes: 2 additions & 19 deletions src/oop/delegation/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,21 @@ package main

import "fmt"

type A struct {
}

type A struct {}
func (a A) Foo() {
fmt.Print("a.foo ")
a.Bar()
}

func (a A) Bar() {
fmt.Println("a.bar")
}

type B struct {
A
}

func (b B) Foo() {
fmt.Print("b.foo ")
b.Bar()
}

func (b B) Bar() {
fmt.Println("b.bar")
}

func main() {
/*
a := A{}
a.Foo()
*/

b := B{}
b.Foo() // "a.bar" or "b.bar"?

b.Foo() // What happens?
}
16 changes: 16 additions & 0 deletions src/oop/polymorphism/polymorphism_2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

type Point struct { x, y int }
func (p Point) String() string { return fmt.Sprintf("(%d, %d)", p.x, p.y) }

type ColorPoint struct { Point; color int }
func (cp ColorPoint) String() string { return fmt.Sprintf("(%d, %d, %d)", cp.x, cp.y, cp.color) }

func main() {
p := Point{1, 2}
cp := ColorPoint{Point{1, 2}, 3}
var s fmt.Stringer = p; fmt.Println(s) // prints "(1, 2)"
s = cp; fmt.Println(s) // prints "(1, 2, 3)"
}

0 comments on commit f299b36

Please sign in to comment.