1

This is a very basic question that doesn't seem to be asked or answered anywhere.

Sometimes we define a method and name the parameter as "factory". Example: https://github.com/AirtestProject/Airtest/blob/c29d0462fe29db5c04cda31de1c05bcae5991061/airtest/report/js/lazyload.js

(function (root, factory) {
    if (typeof exports === "object") {
        module.exports = factory(root);
    } else if (typeof define === "function" && define.amd) {
        define([], factory(root));
    } else {
        root.LazyLoad = factory(root);
    }

Sometimes we name the class as SomeFactory. Example: https://github.com/SeleniumHQ/selenium/blob/d4dba1c07a1d8601f80b9e71be7853317ab0d49d/java/src/org/openqa/selenium/remote/server/ActiveSessionFactory.java

public class ActiveSessionFactory implements SessionFactory {

Sometimes methods are named as someFactoryMethod. Example: https://github.com/SeleniumHQ/selenium/blob/d4dba1c07a1d8601f80b9e71be7853317ab0d49d/java/src/org/openqa/selenium/remote/server/ActiveSessionFactory.java

public synchronized ActiveSessionFactory bind(
  Predicate<Capabilities> onThis,
  SessionFactory useThis) {
Require.nonNull("Predicate", onThis);
Require.nonNull("SessionFactory", useThis);

Sometimes variables are also named as varFactory

private List<SessionFactory> factories;

What is the expectation around the word "factory" in these contexts? When should I name something as "factory"? Isn't every class essentially a blueprint or a "factory" in some way?

Not sure if this last question can be answered but are there other similar common names that classes/methods are usually named besides "factory"?

Mugen
  • 1,417
  • 5
  • 22
  • 40
  • 2
    A factory instance creates instances of some other class. They're different from a simple constructor (or static factory) in that the factory implementation can vary at runtime. – shmosel Apr 14 '22 at 19:14
  • @shmosel It would be great if you could explain this with an example. When I hear about a class being created dynamically I'm thinking about the generic class T being used while defining the class using angular brackets. – Mugen Apr 14 '22 at 19:20
  • 1
    You should be able to Google for "factory design pattern" and get tons of results. Or see https://stackoverflow.com/q/13029261/217324 – Nathan Hughes Apr 14 '22 at 19:23
  • Take the JDK's `ThreadFactory` for example. If a library wants to use threads constructed according to your specification, it would have to either duplicate all of `Thread`'s configuration options (which would make the API cumbersome and perishable) or accept thread instances (which would limit its ability to allocate threads on demand). By creating a factory, you get to define _how_ threads are created, while leaving it to the library to actually create and use them as needed. – shmosel Apr 14 '22 at 19:30
  • In another scenario, you might have multiple factory implementations that are selected based on some external configuration. – shmosel Apr 14 '22 at 19:32

1 Answers1

1

Mugen. The factory is for the factory design pattern. The Design Pattern

Code example:

public abstract class TvMediaFactory {
public abstract Episode createEpisode(int number, String title);
public abstract Film createFilm(String title);
public abstract TvSeries createSeries(String title);
}

For the concrete classes, we can implements different behaviours.

 public class AnimatedFactory extends TvMediaFactory {
    @Override
    public Episode createEpisode(int number, String title) {
    return new AnimatedEpisode(number, title);
    }
    @Override
    public Film createFilm(String title) {
    return new AnimatedFilm(title);
    }
    @Override
    public TvSeries createSeries(String title) {
    return new AnimatedSeries(title);
    }
    }
Marina D.
  • 11
  • 2
  • Thanks Marina! I think I'm understanding that factory class can produce entirely different classes even without using general class T. – Mugen Apr 16 '22 at 02:07