Skip to content

Commit

Permalink
Session 10 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed committed Jul 23, 2022
1 parent b3d9ea5 commit fbdafe4
Show file tree
Hide file tree
Showing 25 changed files with 579 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 10-Interfaces/02-Diffs/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public interface IPerson
{
string Name { get; set; }
int Age { get; set; }
void Print()
{
Console.WriteLine($"{Name} {Age}");
}
}

public class Person : IPerson
{
public string Name { get; set; }
public int Age { get; set; }
public void Print()
{
Console.WriteLine("{0} - {1}", Name, Age);
}
}
27 changes: 27 additions & 0 deletions 10-Interfaces/02-Diffs/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "fmt"

type IPerson interface {
print()
}

type Person struct {
}

func main() {
var p IPerson
p = Person{}
p.print()
print2(p)
print2(12)
print2("ali")
}

func (person Person) print() {
println("Hello")
}

func print2(item interface{}) {
fmt.Printf("%v\n",item)
}
14 changes: 14 additions & 0 deletions 10-Interfaces/02-Diffs/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface IPerson {
name: string;
age: number;
print();

}

class Person implements IPerson {
name: string;
age: number;
print() {
console.log(this.name);
}
}
54 changes: 54 additions & 0 deletions 10-Interfaces/03-Interface/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import "fmt"

type Runner interface {
Run()
}

type Walker interface {
Walk()
}

type Shooter interface {
Shoot()
}

type ShooterRunner interface {
Runner
Shooter
}

type Player struct {
Name string
Age int
Height int
Weight int
Position string
}

func main() {

player1 := &Player{
Name: "Alireza",
Age: 30,
Height: 180,
Weight: 70,
Position: "Forward",
}

var runner ShooterRunner = player1

var shooter ShooterRunner = player1

runner.Run()

shooter.Shoot()
}

func (player *Player) Run() {
fmt.Printf("name: %s, position: %s , Player is running\n",player.Name,player.Position)
}
func (player *Player) Shoot() {
fmt.Printf("name: %s, position: %s , Player is shooting",player.Name,player.Position)
}
77 changes: 77 additions & 0 deletions 10-Interfaces/04-EmbeddedInterfaces/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import "fmt"

type Animal interface {
Eat()
Sleep()
Walk()
}

type Human interface {
Animal
Speak()
Think()
}

type Employee struct {
Human
Name string
Age int
}

type Cat struct {
Name string
}

func main() {
employee := &Employee{Name: "Ali", Age: 30}
cat := &Cat{Name: "Cat"}

var human Human = employee
var animal Animal = cat

human.Eat()
human.Sleep()
human.Walk()
human.Speak()
human.Think()

animal.Eat()
animal.Sleep()
animal.Walk()
}

func (cat *Cat) Eat() {
fmt.Println("Cat is eating")
}

func (cat *Cat) Sleep() {
fmt.Println("Cat is sleeping")
}

func (cat *Cat) Walk() {
fmt.Println("Cat is walking")
}

func (employee *Employee) Speak() {
fmt.Println("Employee is speaking")
}

func (employee *Employee) Think() {
fmt.Println("Employee is thinking")
}

func (employee *Employee) Eat() {
fmt.Println("Employee is eating")
}

func (employee *Employee) Sleep() {
fmt.Println("Employee is sleeping")
}

func (employee *Employee) Walk() {
fmt.Println("Employee is walking")
}


56 changes: 56 additions & 0 deletions 10-Interfaces/05-EmptyInterfaces/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import "fmt"

type Person struct {
Name string
}
func main() {
Print("Hello")
Print(123)
Print(true)
Print([]int{1, 2, 3})
Print(map[string]int{"a": 1, "b": 2})
Print(Person{"Ali"})
}

func AddOrder(order interface{}) interface{}{

if 1 == 2{
return 1
}

if 1 == 3{
return "Cancel"
}

if 1 == 4{
return true
}

if 1 == 4{
return false
}

return "OK"
}

func Print(input interface{}) {
switch input.(type) {
case string:
fmt.Println("String:", input)
case int:
fmt.Println("Integer:", input)
case bool:
fmt.Println("Boolean:", input)
case []int:
fmt.Println("Slice:", input)
case map[string]int:
fmt.Println("Map:", input)
case Person:
fmt.Println("Person:", input)
}
}


// object
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entities

type NotificationType string

const (
Email NotificationType = "Email"
Sms NotificationType = "Sms"
)
10 changes: 10 additions & 0 deletions 10-Interfaces/06-MiniProject/01-Notification/entities/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package entities

type Order struct {
ID int64
UserFullName string
UserId string
Price float64
Status bool
NotificationType NotificationType
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package externalServices

import (
"fmt"
"notification/entities"
)

type EmailService struct {
}

func (e *EmailService) SendMessage(order *entities.Order) {
fmt.Printf("Email sent: %v\n", order)
}

func (e *EmailService) SendNotify(receiver string, message string) {
fmt.Printf("Email sent: receiver: %s, message: %s\n", receiver, message)
}

func NewEmailService() *EmailService {
return &EmailService{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package externalServices

import "fmt"

type NilNotifyService struct {
}

func (e *NilNotifyService) SendNotify(receiver string, message string) {
fmt.Printf("nilNotifyService: receiver: %s, message: %s\n", receiver, message)
}

func NewNilNotifyService() *NilNotifyService {
return &NilNotifyService{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package externalServices

import "notification/entities"

type Notifier interface {
SendNotify(receiver string, message string)
}

func NewNotifier(notificationType entities.NotificationType) Notifier {
switch notificationType {
case entities.Sms:
return NewSmsService()
case entities.Email:
return NewEmailService()
default:
return NewNilNotifyService()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package externalServices

import (
"fmt"
"notification/entities"
)

type SmsService struct {
}

func (e *SmsService) SendMessage(order *entities.Order) {
fmt.Printf("sms sent: %v\n", order)
}

func (e *SmsService) SendNotify(receiver string, message string) {
fmt.Printf("sms sent: receiver: %s, message: %s\n", receiver, message)
}

func NewSmsService() *SmsService {
return &SmsService{}
}
5 changes: 5 additions & 0 deletions 10-Interfaces/06-MiniProject/01-Notification/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module notification

go 1.18

require github.com/naeemaei/golangTestMod v0.0.2 // indirect
2 changes: 2 additions & 0 deletions 10-Interfaces/06-MiniProject/01-Notification/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/naeemaei/golangTestMod v0.0.2 h1:92cnJZKdQGaQeNdxbl1z73tTY2lunCqFMQLTKrv5eDk=
github.com/naeemaei/golangTestMod v0.0.2/go.mod h1:nJJTUYiriqnqyOiC89eiyllcNpQ8pWf8dCiNeJQ4IUM=
Loading

0 comments on commit fbdafe4

Please sign in to comment.