diff --git a/README.md b/README.md index 7d9f5f7..30ebd71 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ A curated collection of idiomatic design & application patterns for Go language. | Pattern | Description | Status | |:-------:|:----------- |:------:| -| [Abstract Factory](/creational/abstract_factory.md) | Provides an interface for creating families of releated objects | ✘ | +| [Abstract Factory](/creational/abstract_factory.md) | Provides an interface for creating families of releated objects | ✔ | | [Builder](/creational/builder.md) | Builds a complex object using simple objects | ✔ | | [Factory Method](/creational/factory.md) | Defers instantiation of an object to a specialized function for creating instances | ✔ | | [Object Pool](/creational/object-pool.md) | Instantiates and maintains a group of objects instances of the same type | ✔ | diff --git a/creational/abstract-factory.md b/creational/abstract-factory.md new file mode 100644 index 0000000..b2315a9 --- /dev/null +++ b/creational/abstract-factory.md @@ -0,0 +1,66 @@ +# Abstract Factory Pattern + +Abstract Factory creational design pattern provides an interface for +creating families of related or dependent objects without specifying +their concrete classes. + +## Implementation + +```go +package afactory + +import "fmt" + +// Coin hierarchy +type Coin interface { + name() string +} + +type Etherium struct { +} + +type Bitcoin struct { +} + +func (c Etherium) name() string { + return "Etherium" +} + +func (c Bitcoin) name() string { + return "Bitcoin" +} + +// Abstract Factory implementation +type AbstractCoinFactory interface { + createCoin() Coin +} + +type EtheriumFactory struct { +} + +type BitcoinFactory struct { +} + +func (c EtheriumFactory) createCoin() Coin { + return Etherium{} +} + +func (c BitcoinFactory) createCoin() Coin { + return Bitcoin{} +} + +``` + +## Usage + +```go + +func printNewCoinName (factory AbstractCoinFactory) { + coin := factory.createCoin() + fmt.Printf("%s\n", coin.name()) +} + +printNewCoinName(EtheriumFactory{}) +printNewCoinName(BitcoinFactory{}) + +``` \ No newline at end of file