Skip to content

Commit cbe79a6

Browse files
committed
facade
1 parent 041f878 commit cbe79a6

11 files changed

+242
-0
lines changed

Adapter/adapter.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Adapter
2+
3+
type Adapter interface {
4+
Mover()
5+
}
6+
7+
type AutomovilAdapter struct {
8+
Auto *Automovil
9+
}
10+
11+
func (a *AutomovilAdapter) Mover() {
12+
if !a.Auto.Encendido{
13+
a.Auto.Encender()
14+
}
15+
a.Auto.Acelerar()
16+
}
17+
18+
type BicicletaAdapter struct {
19+
Bici *Bicicleta
20+
}
21+
22+
func (b *BicicletaAdapter) Mover(){
23+
b.Bici.Avanzar()
24+
}

Adapter/adapter_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Adapter
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAutomovilAdapter_Mover(t *testing.T) {
8+
var s string
9+
//s="automovil"
10+
s="bicicleta"
11+
transport :=Factory(s)
12+
transport.Mover()
13+
}

Adapter/automovil.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Adapter
2+
3+
import "fmt"
4+
5+
type Automovil struct {
6+
Marca string
7+
Model uint8
8+
Encendido bool
9+
}
10+
11+
func (a *Automovil) Encender(){
12+
if a.Encendido{
13+
fmt.Println("Ya está encendido")
14+
return
15+
}
16+
fmt.Println("Encendido!")
17+
}
18+
19+
func (a *Automovil) Acelerar(){
20+
fmt.Println("Acelerando!")
21+
}

Adapter/bicileta.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Adapter
2+
3+
import "fmt"
4+
5+
type Bicicleta struct {
6+
Marca string
7+
Color string
8+
}
9+
10+
func (b *Bicicleta) Avanzar(){
11+
fmt.Println("Avanzando")
12+
}

Adapter/factory.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Adapter
2+
3+
func Factory(s string) Adapter{
4+
switch s {
5+
case "automovil":
6+
d:=Automovil{}
7+
return &AutomovilAdapter{&d}
8+
case "bicicleta":
9+
d:= Bicicleta{}
10+
return &BicicletaAdapter{&d}
11+
}
12+
return nil
13+
}

Facade/email.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Facade
2+
3+
import "fmt"
4+
5+
type Email struct {
6+
to string
7+
message string
8+
}
9+
10+
func NewEmail() Email{
11+
return Email{}
12+
}
13+
func (e *Email) Send(to,comment string) {
14+
e.to=to
15+
e.message=comment
16+
fmt.Printf("mail a: %s, message: %s\n",e.to,e.message)
17+
}

Facade/facade.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Facade
2+
3+
type Facade struct {
4+
to string
5+
comment string
6+
validatorToken ValidatorToken
7+
validatorPermission ValidatorPermission
8+
store Storage
9+
notificator Email
10+
}
11+
12+
func NewFacade(to, comment,token,user string) Facade{
13+
return Facade{
14+
to: to,
15+
comment: comment,
16+
validatorToken: NewValidatorToken(token),
17+
validatorPermission: NewValidatorPermission(user),
18+
store: NewStorage("mysql"),
19+
notificator: NewEmail(),
20+
}
21+
}
22+
23+
func (f *Facade) Comment() error{
24+
err := f.validatorToken.Validate()
25+
if err != nil {
26+
return err
27+
}
28+
err = f.validatorPermission.Validate()
29+
if err != nil {
30+
return err
31+
}
32+
f.store.Save(f.comment)
33+
f.notificator.Send(f.to,f.comment)
34+
return nil
35+
}
36+
37+
func (f *Facade) Notify() {
38+
f.notificator.Send(f.to,f.comment)
39+
}

Facade/facade_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Facade
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"testing"
7+
)
8+
9+
func TestStorage_Save(t *testing.T) {
10+
showInit()
11+
token:="token-validate"
12+
user := "user-blog"
13+
14+
comment := "video :)"
15+
16+
f:= NewFacade(to,comment,token,user)
17+
err := f.Comment()
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
f.Notify()
22+
showFinish()
23+
}
24+
25+
func showInit() {
26+
fmt.Println(`
27+
**************************
28+
* Bienvenido al programa *
29+
**************************
30+
`)
31+
}
32+
33+
func showFinish() {
34+
fmt.Println(`
35+
**************************
36+
* Gracias por utilizar *
37+
**************************
38+
`)
39+
}

Facade/storage.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Facade
2+
3+
import "fmt"
4+
5+
type Storage struct {
6+
engine string
7+
}
8+
9+
func NewStorage(e string) Storage {
10+
return Storage{engine:e}
11+
}
12+
13+
func (s *Storage) Save(comment string){
14+
fmt.Printf("register%s\n",comment)
15+
}

Facade/validate-permisson.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Facade
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var ErrPermissionNotValid = errors.New("no authorized")
9+
10+
type ValidatorPermission struct {
11+
userID string
12+
}
13+
14+
func NewValidatorPermission(id string) ValidatorPermission {
15+
return ValidatorPermission{userID:id}
16+
}
17+
18+
func (vp *ValidatorPermission) Validate() error {
19+
if vp.userID!="user-blog"{
20+
return ErrPermissionNotValid
21+
}
22+
fmt.Println("user authorized")
23+
return nil
24+
}

Facade/validate-token.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Facade
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var ErrTokenNotValid = errors.New("no login")
9+
10+
type ValidatorToken struct {
11+
token string
12+
}
13+
14+
func NewValidatorToken(t string) ValidatorToken{
15+
return ValidatorToken{token:t}
16+
}
17+
18+
func (vt *ValidatorToken) Validate() error{
19+
if vt.token!="token-validate"{
20+
return ErrTokenNotValid
21+
}
22+
23+
fmt.Println("token validate")
24+
return nil
25+
}

0 commit comments

Comments
 (0)