Package rea.gameplay
Class GameplayFactory
java.lang.Object
rea.gameplay.GameplayFactory
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescription(package private) static final String
Default package name to lookup gameplay: the games subpackage of the gameplay package.(package private) static final ClassLoader
The class loader to find and load gameplays in theGAMEPLAY_PACKAGE
package. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a factory for gameplays.GameplayFactory
(String gameplayPackage) Create a factory for gameplays in a given package. -
Method Summary
Modifier and TypeMethodDescriptioncollectGameplayInPackage
(String gameplayPackage) Collect all gameplay in a package.Get the available games in this factory.(package private) String
getClassName
(Path path) Get the class name from a path.getGameplay
(String name) Get the gameplay for a game with a given name.(package private) Gameplay
getGameplayInstance
(String className) Get an instance of a class with the given name.
-
Field Details
-
LOADER
The class loader to find and load gameplays in theGAMEPLAY_PACKAGE
package.- Implementation Note:
- Use
GameplayFactory.class.getClassLoader()
(instead ofClassLoader.getSystemClassLoader()
) to avoid issues when calling this class from client side code in a web application.
-
GAMEPLAY_PACKAGE
Default package name to lookup gameplay: the games subpackage of the gameplay package.- See Also:
-
-
Constructor Details
-
GameplayFactory
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
Collect all gameplay in a package. All classes in the package implementingGameplay
. are instanced and returned as a map. The map uses the gameplay's name as the keyGameplay.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 ("/")., UseFiles.list(Path)
to get the files in the directory. You can convert a URL to a Path usingPath.of(url.toURI())
., Consider using streams to collect the gameplay, but its is not mandatory.
-
getClassName
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:
-
getGameplayInstance
Get an instance of a class with the given name. The class is expected to be a subclass ofGameplay
and have a default constructor (without arguments). Classes that do not meet these criteria and exceptions during instantiation will be ignored andnull
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 withclazz = LOADER.loadClass(className)
, then you can create an instance of the class withclazz.getDeclaredConstructor().newInstance()
-
getAvailableGameplays
Get the available games in this factory.- Specified by:
getAvailableGameplays
in interfaceAbstractGameplayFactory
- Returns:
- the available games as a set.
-
getGameplay
Get the gameplay for a game with a given name.- Specified by:
getGameplay
in interfaceAbstractGameplayFactory
- Parameters:
name
- of the game.- Returns:
- the gameplay for the given game,
or
null
if the game does not exist.
-