O Factory method é um padrão de projeto criacional, que resolve o problema de criar objetos de produtos sem especificar suas classes concretas.
O Factory Method define um método, que deve ser usado para criar objetos em vez da chamada direta ao construtor (operador new). As subclasses podem substituir esse método para alterar a classe de objetos que serão criados.
Se você não conseguir descobrir a diferença entre os padrões Factory, Factory Method e Abstract Factory, leia nossa Comparação Factory.
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).