What is Abstract factory
To create an abstract factory ,provide an interface for creating families of related or dependent objects without specifying their concrete classes.Explanation
Abstract factory is the extension of basic Factory pattern. It provides Factory interfaces for creating a family of related classes.In an Abstract Factory class implementation you declare interfaces for Factories, which will in turn work in similar fashion as with Factories.public interface IFactory1 { IPeople GetPeople(); } public class Factory1 : IFactory1 { public IPeople GetPeople() { return new Villagers(); } } public interface IFactory2 { IProduct GetProduct(); } public class Factory2 : IFactory2 { public IProduct GetProduct() { return new IPhone(); } } public abstract class AbstractFactory12 { public abstract IFactory1 GetFactory1(); public abstract IFactory2 GetFactory2(); } public class ConcreteFactory : AbstractFactory12 { public override IFactory1 GetFactory1() { return new Factory1(); } public override IFactory2 GetFactory2() { return new Factory2(); } }
Where to Use
We should use the Abstract Factory design pattern when:
- The system needs to be independent from the way the products it works with are created.
- The system is or should be configured to work with multiple families of products.
- A family of products is designed to work only all together.T
- The creation of a library of products is needed, for which is relevant only the interface, not the implementation.
Conclusion
- The abstract factory class defines the abstract methods that have to be implemented by concrete factory classes. It serves as interface and contract definition.
- The methods return values are also defined by abstract classes, this allows a high flexibility and independence, leading to methods that must only be implemented once.
No comments:
Post a Comment