Skip to content

FrontendFactoryConcept

cwerling edited this page Sep 16, 2014 · 3 revisions

FuzzEd's client-side JavaScript code (everything in FuzzEd/static/script) uses John Resig's Simple JavaScript Inheritance to simplify OOP and uses require.js for a modular design. Furthermore, it relies on a special class structure, which is enforced by a Factory class (see factory.js).

Every diagram type that FuzzEd implements starts off with a subclass of the abstract Editor (editor.js). The abstract Editor generally uses a Factory to create any instances of other classes (e.g. Graph, Node, ...) and so should everyone else.

this.graph = Factory.create('Graph', json);

This gives the Factory the ability to find the most concrete subclass of the given abstract class (first argument), load the particular module and call the concrete class constructor (any further arguments). It then returns the freshly built instance. If, for example, the Editor is in fact a FaulttreeEditor, the Factory would create a FaulttreeGraph, as it exists a constructor for it (in faulttree/graph.js).

Example Class Diagram


More generally spoken, the Factory works like this:

var myInstance = Factory.create('%Class%', constructor_arg1, constructor_arg2, ...);

Meta Class Diagram

Some side notes:

  • All classes are named in camel case, whereas their respective module file names are all lowercase and with underscores (e.g. NodeGroup is located in node_group.js)
  • In order to find the right modules and concrete classes, the Factory needs you to require all desired concrete class modules for a specific graph-kind in the {graph_kind}/editor.js (define(...))
  • The Factory looks for constructors in {graph_kind}/{abstract_class_name} (e.g. faulttree/graph) and then checks – in the following order – whether
  1. the module directly returns the constructor
  2. the module returns an object with a member called after the concrete class (e.g. FaulttreeGraph)
  3. the module returns object with a member called after the abstract class (e.g. Graph)
  • Inheritances that do not have origin in the abstract classes (which are located directly in the FuzzEd/static/script directory) have to be manually defined in factory.js::_baseKind()
  • Although Factory is a class, there is only one instance of it, which is handled by require.js. Loading the Factory via define(...) directly returns this instance.
  • The Factory is set up once in Fuzzed/static/templates/editor.html where its kind is set to the graph kind through a template variable
Clone this wiki locally