Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- // Product interface
- type Product interface {
- Use() string
- }
- // ConcreteProductA is a type of Product
- type ConcreteProductA struct{}
- // Use implements the Product interface for ConcreteProductA
- func (c ConcreteProductA) Use() string {
- return "Using ConcreteProductA"
- }
- // ConcreteProductB is another type of Product
- type ConcreteProductB struct{}
- // Use implements the Product interface for ConcreteProductB
- func (c ConcreteProductB) Use() string {
- return "Using ConcreteProductB"
- }
- // Creator is the factory interface
- type Creator interface {
- CreateProduct() Product
- }
- // ConcreteCreatorA is a ConcreteCreator that creates ConcreteProductA
- type ConcreteCreatorA struct{}
- // CreateProduct implements the Creator interface for ConcreteCreatorA
- func (c ConcreteCreatorA) CreateProduct() Product {
- return ConcreteProductA{}
- }
- // ConcreteCreatorB is another ConcreteCreator that creates ConcreteProductB
- type ConcreteCreatorB struct{}
- // CreateProduct implements the Creator interface for ConcreteCreatorB
- func (c ConcreteCreatorB) CreateProduct() Product {
- return ConcreteProductB{}
- }
- func main() {
- creatorA := ConcreteCreatorA{}
- productA := creatorA.CreateProduct()
- fmt.Println(productA.Use())
- creatorB := ConcreteCreatorB{}
- productB := creatorB.CreateProduct()
- fmt.Println(productB.Use())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement