Package rea.gameplay

Class GameplayFactory

java.lang.Object
rea.gameplay.GameplayFactory
All Implemented Interfaces:
AbstractGameplayFactory

public class GameplayFactory extends Object implements AbstractGameplayFactory
A factory for creating Gameplay instances. This factory method creates instances of Gameplay using reflection. and selects the gameplay to use based on the name of the game. The available games are collected from a package, either GAMEPLAY_PACKAGE if the default constructor is used, or package name passed as parameter to the constructor.
Author:
José Paulo Leal jpleal@fc.up.pt
Implementation Note:
a concrete participant of the Factory method design pattern.
  • Field Details

    • LOADER

      static final ClassLoader LOADER
      The class loader to find and load gameplays in the GAMEPLAY_PACKAGE package.
      Implementation Note:
      Use GameplayFactory.class.getClassLoader() (instead of ClassLoader.getSystemClassLoader()) to avoid issues when calling this class from client side code in a web application.
    • GAMEPLAY_PACKAGE

      static final String GAMEPLAY_PACKAGE
      Default package name to lookup gameplay: the games subpackage of the gameplay package.
      See Also:
  • Constructor Details

    • GameplayFactory

      public GameplayFactory() throws ReaException
      Create a factory for gameplays.
      Throws:
      ReaException - if the package does not exist, or IO exception related to the package directory was raised.
    • GameplayFactory

      GameplayFactory(String gameplayPackage) throws ReaException
      Create a factory for gameplays in a given package.
      Parameters:
      gameplayPackage - the package to look for gameplays.
      Throws:
      ReaException - if the package does not exist, or IO exception related to the package directory was raised.
  • Method Details

    • collectGameplayInPackage

      Map<String,Gameplay> collectGameplayInPackage(String gameplayPackage) throws ReaException
      Collect all gameplay in a package. All classes in the package implementing Gameplay. are instanced and returned as a map. The map uses the gameplay's name as the key Gameplay.getName() and an instance of the gameplay as the value.
      Parameters:
      gameplayPackage - with the gameplay to collect.
      Returns:
      a map with the gameplay, indexed by their name.
      Throws:
      ReaException - if the package does not exist, or IO exception related to the package directory was raised.
      Implementation Note:
      Use ClassLoader.getResource(String) to get the URL of the package. Note that the package name should be in the form of a path, with dots (".") replaced by slashes ("/")., Use Files.list(Path) to get the files in the directory. You can convert a URL to a Path using Path.of(url.toURI())., Consider using streams to collect the gameplay, but its is not mandatory.
    • getClassName

      String getClassName(Path path)
      Get the class name from a path. The path is expected to be in the form of a package path. The class name is the last part of the path without the extension.
      Parameters:
      path - the path to get the class name from.
      Returns:
      the class name.
      See Also:
      • gameplayPackage
    • getGameplayInstance

      Gameplay getGameplayInstance(String className)
      Get an instance of a class with the given name. The class is expected to be a subclass of Gameplay and have a default constructor (without arguments). Classes that do not meet these criteria and exceptions during instantiation will be ignored and null will be returned,
      Parameters:
      className - the name of the class to get an instance of.
      Returns:
      an instance of the class or null.
      Implementation Note:
      Use ClassLoader.loadClass(String) to load the class. If you load a class with clazz = LOADER.loadClass(className), then you can create an instance of the class with clazz.getDeclaredConstructor().newInstance()
    • getAvailableGameplays

      public Set<String> getAvailableGameplays()
      Get the available games in this factory.
      Specified by:
      getAvailableGameplays in interface AbstractGameplayFactory
      Returns:
      the available games as a set.
    • getGameplay

      public Gameplay getGameplay(String name)
      Get the gameplay for a game with a given name.
      Specified by:
      getGameplay in interface AbstractGameplayFactory
      Parameters:
      name - of the game.
      Returns:
      the gameplay for the given game, or null if the game does not exist.