When should I use the factory?
|
RwD
![]()
Joined: 2005-01-09
Posts: 383 |
Posted: Thu, 2007-05-03 18:45
|
|
For a module I was thinking about making (if I find the time) I'm now first trying to find out more about the inner workings of G2 and how I'm supposed to be coding. Right now I am a bit unsure when I should use the facory to get a class for me and when not. So what I would like to know. When do I use the "newFactoryInstance" methods? It is for example used to get a urlGenerator which is usefull for the rewrite module but it isn't used for example for the translator module. so how do I know if I need to register a class with the factory?? Also I'm wondering when I would use which of the three "newFactoryInstance" functions. What is a "hint" exactly, can't really tell from the comments nor from the database contents... I hope my questions make sense ;) ______________________ |
|


Posts: 32509
no, not for the url generator.
we use the same instance everywhere because we don't want to initialize it again and again (slow).
it all depends on what you want to do.
the url generator is a nice example. the factory allows modules to register their own url generator which is then used everywhere.
so, what do you want to do?
--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage
Posts: 383
Well, I need pages with some text instead of images and I don't feel like using some cms and integrate gallery into that. But first I need to know a lot more about gallery, then I need the time :P
I am also kind of trying to figure out if I can build modules for completely images unrelated stuff like event calendars and signin systems and use gallery for what it wasn't initially made for but I have the feeling this possible so I want to investigate that.
But all I want to know is when would I decide to register a class with the factory. How do I decide if I should? I also see "MaintenanceTask", "GalleryToolkit" and "GalleryAuthPlugin" registered, so it is not only entities, but I can't seem to see the rule in this.
And I also still have question nr 2: What are hints and how are they used with the factory?
______________________
I made a theme for G2, try it
Posts: 32509
Use the factory if the feature you want to implement requires it.
Else there's usually no reason to use it.
Say you want to show items in your gallery that are just text (text / html / bbcode / ...).
What you'll need is...
- decide how such text items should be added. in g2, you usually add files. would you add text files? or would you offer a new "add item method" that let's you copy and paste some text in a web-form?
- how would the item be edited? probably with "edit item" in a new tab, e.g. "text" similar to the existing tabs "general", "photo", ...
- you'll need to create a new class which extends GalleryItem (or GalleryDataItem)
from the above features that you need to implement, you'll need to register a few factory implementations:
- adding items by entering text in a web-form: register an ItemAddPlugin instance
- editing the text of the item: register an ItemEditPlugin instance
- your new GalleryTextItem: register your entity (similiar to how GalleryPhotoItem, GalleryMovieItem ,... are registered)
@loading instances from the factory:
your module is probably not concerned about instantiating stuff via the factory. all you need to do is register your implementations such that the core module knows about them.
--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage