Factory https://refactoring.guru/es/design-patterns/abstract-factory
Introducción
Cómo aprender Go avanzado: concurrencia, patrones de diseño y net
Concurrencia
Race condition: el problema de depositar y retirar
Sync Mutex: Lock y Unlock
Mutex de lectura y escritura
Sistema de caché sin concurrencia
Sistema de caché con concurrencia
Reutilización de computación intensiva
Patrones de diseño
¿Qué son los patrones de diseño?
Factory
Singleton
Adapter
Observer
Strategy
Net
Escaneador de puertos sin concurrencia
Escaneador de puertos con concurrencia
Netcat
Servidor de chat en Go con net
Terminando el chat
Conclusión
Resumen del curso de Go avanzado
Aún no tienes acceso a esta clase
Crea una cuenta y continúa viendo este curso
Aportes 4
Preguntas 1
package main
import "errors"
// Patron Creacional
// Interface que define el comportamiento de un producto
type IProduct interface {
setStocked(stock int)
getStocked() int
getName() string
setName(name string)
}
// Implementacion de la interfaz IProduct para el producto de tipo "Computadora"
type Computer struct {
name string
stock int
}
// Implementando de forma implicita la interfaz IProduct
func (c *Computer) setStocked(stock int) {
c.stock = stock
}
func (c *Computer) getStocked() int {
return c.stock
}
func (c *Computer) getName() string {
return c.name
}
func (c *Computer) setName(name string) {
c.name = name
}
// Creando clase base de computadora, por composicion sobre herencia
type Laptop struct {
Computer
}
func NewLaptop() IProduct {
return &Laptop{Computer{"Laptop", 25}}
}
type Desktop struct {
Computer
}
func NewDesktop() IProduct {
return &Desktop{Computer{"Desktop", 35}}
}
// Creando fabrica de productos: Factory pattern
func GetComputerFactory(computerType string) (IProduct, error) {
switch computerType {
case "Laptop":
return NewLaptop(), nil
case "Desktop":
return NewDesktop(), nil
default:
return nil, errors.New("invalid computer type")
}
}
// Trying polymorphism
func PrintNameAndStock(product IProduct) {
println("Name:", product.getName(), "Stock:", product.getStocked())
}
func main() {
laptop, _ := GetComputerFactory("Laptop")
desktop, _ := GetComputerFactory("Desktop")
PrintNameAndStock(laptop)
PrintNameAndStock(desktop)
}
Factory
Es un patrón creacional, que nos permite crear una “fabrica” de objetos a partir de una clase base y a su vez va implementar comportamientos polimórficos que permite modificar el comportamiento de las clases heredadas
package main
import (
"fmt"
"strings"
)
type IProduct interface {
setStock(stock int)
getStock() int
setName(name string)
getName() string
}
type Computer struct {
name string
stock int
}
func (c *Computer) setStock(stock int) {
c.stock = stock
}
func (c *Computer) setName(name string) {
c.name = name
}
func (c *Computer) getName() string {
return c.name
}
func (c *Computer) getStock() int {
return c.stock
}
type Laptop struct {
Computer
}
func newLaptop() IProduct {
return &Laptop{
Computer: Computer{
name: "Laptop computer",
stock: 25,
},
}
}
type Desktop struct {
Computer
}
func newDesktop() IProduct {
return &Desktop{
Computer: Computer{
name: "Desktop computer",
stock: 35,
},
}
}
func GetComputerFactory(computerType string) (IProduct, error) {
if strings.ToLower(computerType) == "laptop" {
return newLaptop(), nil
}
if strings.ToLower(computerType) == "desktop" {
return newDesktop(), nil
}
return nil, fmt.Errorf("Invalid computer type")
}
func printNameAndStock(p IProduct) {
fmt.Printf("Product name: %s, with stock %d\n", p.getName(), p.getStock())
}
func main() {
laptop, _ := GetComputerFactory("laptop")
desktop, _ := GetComputerFactory("desktop")
printNameAndStock(laptop)
printNameAndStock(desktop)
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.