📄 ozoneobjectfactory.java
字号:
/*Normally a class like this would be generated by OPP. Unfortunately this cannot(yet?) be the case for OzoneObject, as that class does not implement OzoneRemote.*/package org.ozoneDB;import org.ozoneDB.tools.OPP.OPP;/** * <p>Factory pattern class for creating ozone objects.</p> * <p> You can use this class for retrieving objects from the database where * the type is not known beforehand.</p> * A factory has a bit of a schizophrenic nature: on the client-side it 'links' * to an ExternalDatabase, while on the server-side it does so to the Database * that holds the instances that the factory creates. Note however that a * factory running inside an Ozone server can also be linked to an External * database outside that server (userclient -> server A -> server B). In that * case such a factory would be 'linked' to an ExternalDatabase.</p> * <p>The idea behind factories is threefold:<ul> * <li>provide an abstraction from Ozone specific object creation</li> * <li>facilitate creating objects with non-default constructors</li> * <li>provide (almost) the same interface on the server side and the client * side on object creation</li></ul> * The differences in client side and server side operation are:<ul> * <li>on the server side <code>getDefault()</code> returns a factory that is * creates its objects inside that same server database and on the client side * it returns a factory that creates its objects in the default database</li> * <li>In order to use <code>getDefault()<code> on the client side, you need to * call <code>setDefaultDatabaseUrl(String)</code> to specify the default * database.</li> * In a typical real-world scenario where you connect to only one ozone database * you would call <code>setDefaultDatabaseUrl(String)</code> on the client only * once, and for the rest of the programs lifespan call * <code>CarTypeImplFactory.getDefault().create()</code> methods both on the server and * client sides to create objects.</p> * <p>Note: if you do not have a clue what factories are and how they work, you * should probably brush up on your knowledge of * <a href="http://www.google.com/search?q=java+design+patterns+GoF">design patterns</a></p>. * @author <a href="mailto://ozone-db.orgATmekenkampD0Tcom">Leo Mekenkamp</a> * @since FIXME(since when?) */public final class OzoneObjectFactory extends AbstractFactory { private static class Info implements org.ozoneDB.FactoryClassInfo { public final void defaultDatabaseUrlChanged() { defaultInstance = null; } } static { addFactoryClassInfo(new Info()); } private static OzoneObjectFactory defaultInstance = null; /** * On the client side: returns a factory that is linked to a database * specified by the url passed to <code>setDefaultDatabaseUrl</code>. On the * server side: returns a factory that is linked to the server database. * Note that multiple calls to this method return the same value over and * over again, until <code>setDefaultDatabaseUrl</code> has been called. */ public static synchronized OzoneObjectFactory getDefault() throws Exception { if (defaultInstance == null) { defaultInstance = new OzoneObjectFactory(); } return defaultInstance; } /** * <p>Default constructor: creates a factory that is linked to the default * database.</p> */ public OzoneObjectFactory() throws Exception { } /** <p>Creates a factory that creates its objects in the database specified by * <code>url</code>. Note that this constructor can only be used if the database in * question is not opened by this client. Use <code>CarTypeImplFactory()</code> or * <code>CarTypeImplFactory(Factory)</code> to create a factory for an already opened * database. This might seem strange, or even annoying at first, but it is very * logical from an object oriented point of view: you probably want a factory that * creates its objects in the default database, or in the same realm as another * factory you already have created...</p> * @param url defining the remote database (something like * <code>ozone:remote://localhost:3333</code>) */ public OzoneObjectFactory(String url) throws Exception { super(url); } /** <p>Creates a factory that creates its objects in the same database as a specific * other fatory does.</p> * @param factory the factory that creates its objects in the same database as the new factory * should */ public OzoneObjectFactory(AbstractFactory factory) { super(factory); } /** * Gets called automatically to indicate that the default database has been * closed. DO NOT CALL THIS METHOD YOURSELF. */ protected void defaultClosed() { defaultInstance = null; } /** * <p>Retrieves an object through its handle. <emp>Do not use this function * unless you are perfectly sure about what you are doing</emp>. See the * examples for some hands-on information on when to use handles. Note that * normally one would use names or just standard java object references.</p> */ public OzoneRemote objectForHandle(String handle) throws Exception { return (OzoneRemote) getDatabase().objectForHandle(handle); } /** * <p>Retrieves an object from the database through its name.</p> */ public OzoneRemote objectForName(String name) throws Exception { return (OzoneRemote) getDatabase().objectForName(name); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -