WidgetData

The WidgetData class is a Java record that encapsulates the data needed to create a widget. It uses Java’s Supplier functional interface to provide a factory method for creating widgets.

This class is mostly used for loading widgets. This provides an automatic way of producing widget (also known as widgetFactory).

Key Components

  • name: A String that represents the name of the widget.

  • description: A String that provides a description of the widget.

  • widgetFactory: A Supplier<T> where T extends Widget. This is a factory method for creating instances of the widget.

Methods

  • name(): This method returns the name of the widget.

  • description(): This method returns the description of the widget.

  • createWidget(): This method uses the widgetFactory to create a new instance of the widget.

Usage

Here’s an example of how you might use the WidgetData class:

WidgetData<MyWidget> myWidgetData = new WidgetData<>(
    "MyWidget", // Name of the widget
    "This is my custom widget.", // Description of the widget
    () -> new MyWidget() // Factory method for creating the widget
);

// Create a new instance of MyWidget (Used in WidgetManager to load from a save file)
Widget myWidget = myWidgetData.createWidget();

Last updated

Was this helpful?