Factory method es un patrón de diseño creacional que resuelve el problema de crear objetos de producto sin especificar sus clases concretas.
El patrón Factory Method define un método que debe utilizarse para crear objetos, en lugar de una llamada directa al constructor (operador new). Las subclases pueden sobrescribir este método para cambiar las clases de los objetos que se crearán.
Si no sabes la diferencia entre varios patrones y conceptos de la fábrica, lee nuestra Comparación de fábricas.
This example illustrates how to organize a GUI framework into independent modules using dynamic dispatch:
The gui module defines interfaces for all the components.
It has no external dependencies.
The html_gui module provides HTML implementation of the base GUI.
Depends on gui.
The windows_gui module provides Windows implementation of the base GUI.
Depends on gui.
The app is a client application that can use several implementations of the GUI framework, depending on the current environment or configuration. However, most of the app code doesn’t depend on specific types of GUI elements. All client code works with GUI elements through abstract interfaces defined by the gui module.
The Abstract Factory example demonstrates an even greater separation of a factory interface and its implementations.
gui.rs: Product & Creator
html_gui.rs: Concrete creator
windows_gui.rs: Another concrete creator
init.rs: Initialization code
main.rs: Client code
Output
Maze Game
This example illustrates how to implement the Factory Method pattern using static dispatch (generics).