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..330fdd9 --- /dev/null +++ b/creational/abstract-factory.md @@ -0,0 +1,55 @@ +# Abstract Factory Method Pattern + +Abstract Factory method creational design pattern allows providing interface that creating objects without having to specify the exact type of the object that will be created. + +## Implementation + +The example implementation shows how to make a phone from different copmanies. + +### Types + +```go +package company + +type iCompnayFactory interface { + makePhone() iPhone +} +``` + +### Different Implementations + +```go +package company + +type CompnayType int + +const ( + SAMSNUG CompnayType = iota + APPLE +) + +func getCompnayFactory(compnay CompnayType) (iCompnayFactory, error) { + switch compnay { + case SAMSNUG: + return &samsungFactory{}, nil + case APPLE: + return &appleFactory{}, nil + default: + return nil, fmt.Errorf(/* .. */) + } +} +``` + +## Usage + +With the abstract factory method, the user can provide an interface for creating families of releated objects. + +```go +appleFactory, _ := getCompnayFactory(APPLE) +applePhone := appleFactory.makePhone(); +applePhone.makeCall(/*...*/); + +samsungFactory, _ := getCompnayFactory(SAMSNUG) +samsungPhone := samsungFactory.makePhone(); +samsungPhone.makeCall(/*...*/); +```