在本例中,按钮和复选框将被作为产品。它们有两个变体:macOS 版和 Windows 版。
抽象工厂定义了用于创建按钮和复选框的接口。而两个具体工厂都会返回同一变体的两个产品。
客户端代码使用抽象接口与工厂和产品进行交互。同样的代码能与依赖于不同工厂对象类型的多种产品变体进行交互。
buttons: 第一个产品层次结构
buttons/Button.java
| package refactoring_guru.abstract_factory.example.buttons; |
| |
| |
| |
| |
| |
| |
| |
| |
| public interface Button { |
| void paint(); |
| } |
buttons/MacOSButton.java
| package refactoring_guru.abstract_factory.example.buttons; |
| |
| |
| |
| |
| |
| |
| public class MacOSButton implements Button { |
| |
| @Override |
| public void paint() { |
| System.out.println("You have created MacOSButton."); |
| } |
| } |
buttons/WindowsButton.java
| package refactoring_guru.abstract_factory.example.buttons; |
| |
| |
| |
| |
| |
| |
| public class WindowsButton implements Button { |
| |
| @Override |
| public void paint() { |
| System.out.println("You have created WindowsButton."); |
| } |
| } |
checkboxes: 第二个产品层次结构
checkboxes/Checkbox.java
| package refactoring_guru.abstract_factory.example.checkboxes; |
| |
| |
| |
| |
| public interface Checkbox { |
| void paint(); |
| } |
checkboxes/MacOSCheckbox.java
| package refactoring_guru.abstract_factory.example.checkboxes; |
| |
| |
| |
| |
| |
| |
| public class MacOSCheckbox implements Checkbox { |
| |
| @Override |
| public void paint() { |
| System.out.println("You have created MacOSCheckbox."); |
| } |
| } |
checkboxes/WindowsCheckbox.java
| package refactoring_guru.abstract_factory.example.checkboxes; |
| |
| |
| |
| |
| |
| |
| public class WindowsCheckbox implements Checkbox { |
| |
| @Override |
| public void paint() { |
| System.out.println("You have created WindowsCheckbox."); |
| } |
| } |
factories
factories/GUIFactory.java: 抽象工厂
| package refactoring_guru.abstract_factory.example.factories; |
| |
| import refactoring_guru.abstract_factory.example.buttons.Button; |
| import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; |
| |
| |
| |
| |
| public interface GUIFactory { |
| Button createButton(); |
| Checkbox createCheckbox(); |
| } |
factories/MacOSFactory.java: 具体工厂( macOS)
| package refactoring_guru.abstract_factory.example.factories; |
| |
| import refactoring_guru.abstract_factory.example.buttons.Button; |
| import refactoring_guru.abstract_factory.example.buttons.MacOSButton; |
| import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; |
| import refactoring_guru.abstract_factory.example.checkboxes.MacOSCheckbox; |
| |
| |
| |
| |
| |
| public class MacOSFactory implements GUIFactory { |
| |
| @Override |
| public Button createButton() { |
| return new MacOSButton(); |
| } |
| |
| @Override |
| public Checkbox createCheckbox() { |
| return new MacOSCheckbox(); |
| } |
| } |
factories/WindowsFactory.java: 具体工厂(Windows)
| package refactoring_guru.abstract_factory.example.factories; |
| |
| import refactoring_guru.abstract_factory.example.buttons.Button; |
| import refactoring_guru.abstract_factory.example.buttons.WindowsButton; |
| import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; |
| import refactoring_guru.abstract_factory.example.checkboxes.WindowsCheckbox; |
| |
| |
| |
| |
| |
| public class WindowsFactory implements GUIFactory { |
| |
| @Override |
| public Button createButton() { |
| return new WindowsButton(); |
| } |
| |
| @Override |
| public Checkbox createCheckbox() { |
| return new WindowsCheckbox(); |
| } |
| } |
app
app/Application.java: 客户端代码
| package refactoring_guru.abstract_factory.example.app; |
| |
| import refactoring_guru.abstract_factory.example.buttons.Button; |
| import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; |
| import refactoring_guru.abstract_factory.example.factories.GUIFactory; |
| |
| |
| |
| |
| |
| public class Application { |
| private Button button; |
| private Checkbox checkbox; |
| |
| public Application(GUIFactory factory) { |
| button = factory.createButton(); |
| checkbox = factory.createCheckbox(); |
| } |
| |
| public void paint() { |
| button.paint(); |
| checkbox.paint(); |
| } |
| } |
Demo.java: 程序配置
| package refactoring_guru.abstract_factory.example; |
| |
| import refactoring_guru.abstract_factory.example.app.Application; |
| import refactoring_guru.abstract_factory.example.factories.GUIFactory; |
| import refactoring_guru.abstract_factory.example.factories.MacOSFactory; |
| import refactoring_guru.abstract_factory.example.factories.WindowsFactory; |
| |
| |
| |
| |
| public class Demo { |
| |
| |
| |
| |
| |
| |
| private static Application configureApplication() { |
| Application app; |
| GUIFactory factory; |
| String osName = System.getProperty("os.name").toLowerCase(); |
| if (osName.contains("mac")) { |
| factory = new MacOSFactory(); |
| } else { |
| factory = new WindowsFactory(); |
| } |
| app = new Application(factory); |
| return app; |
| } |
| |
| public static void main(String[] args) { |
| Application app = configureApplication(); |
| app.paint(); |
| } |
| } |
OutputDemo.txt: 执行结果
You create WindowsButton.
You created WindowsCheckbox.