StateFactory.java

Overview

`StateFactory.java` is a core utility class within the Finite State Machine (FSM) Playback module of the media player system. Its primary responsibility is to create, manage, and cache singleton instances of `State` objects used by the FSM to represent various playback states (e.g., playing content, fetching ads, playing ads).

The factory supports:

This class is integral to the FSM playback system, providing a consistent and extensible mechanism to manage the lifecycle of playback states used by the `FsmPlayer`.


Class: StateFactory

Properties

Name

Type

Description

`stateInstance`

`Map`

Cache map storing singleton instances of each `State` subclass.

`customStateType`

`Map`

Map linking default state classes to custom override classes.


Methods

public void overrideStateCreation(@NonNull Class subClass)

Registers a custom subclass to override one of the known default FSM `State` classes.


@NonNull public State createState(@NonNull Class classType)

Creates or returns a cached singleton instance of the specified state class or its custom override.


Private Helper Methods


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Example

StateFactory stateFactory = new StateFactory();

// Override the MakingAdCallState with a custom implementation before creating any states
stateFactory.overrideStateCreation(MyCustomMakingAdCallState.class);

// Create a state instance (this will return the custom subclass instance)
State makingAdCallState = stateFactory.createState(MakingAdCallState.class);

// Use the state instance within the FSM player
fsmPlayer.setCurrentState(makingAdCallState);

Mermaid Class Diagram

classDiagram
    class StateFactory {
        - Map<Class, State> stateInstance
        - Map<Class, Class> customStateType
        + void overrideStateCreation(Class subClass)
        + State createState(Class classType)
        - State getCacheInstance(Class type)
        - void setCacheInstance(Class type, State instance)
        - Class convertToCustomClass(Class cla)
    }

The diagram highlights:


Summary

`StateFactory.java` is a pivotal utility within the FSM playback system that centralizes state instantiation, caching, and customization. By providing singleton instances of playback states and allowing custom subclass overrides, it enhances performance, modularity, and extensibility of the FSM player. Its design using synchronized caching and reflection-based instantiation makes it flexible and safe for concurrent use in a complex media playback environment.

This factory supports the core FSM playback workflow by ensuring that state objects are consistently managed and easily extendable, thus playing a foundational role in the media player's finite state machine architecture.