| 1 | <!--- |
|---|
| 2 | Name : objectFactory.cfc |
|---|
| 3 | Author : Rob Gonda |
|---|
| 4 | Created : August 25, 2006 |
|---|
| 5 | Last Updated : August 25, 2006 |
|---|
| 6 | History : |
|---|
| 7 | Purpose : Simple Object Factory / Service Locator |
|---|
| 8 | ---> |
|---|
| 9 | <cfcomponent displayname="objectFactory" hint="I am a simple object factory"> |
|---|
| 10 | |
|---|
| 11 | <!--- |
|---|
| 12 | function init |
|---|
| 13 | in: |
|---|
| 14 | out: this |
|---|
| 15 | notes: usually initialized in application |
|---|
| 16 | ---> |
|---|
| 17 | <cffunction name="init" access="public" output="No" returntype="objectFactory"> |
|---|
| 18 | |
|---|
| 19 | <cfscript> |
|---|
| 20 | // persistance of objects |
|---|
| 21 | variables.com = structNew(); |
|---|
| 22 | </cfscript> |
|---|
| 23 | |
|---|
| 24 | <cfreturn this /> |
|---|
| 25 | </cffunction> |
|---|
| 26 | |
|---|
| 27 | <!--- |
|---|
| 28 | function getObject |
|---|
| 29 | in: name of object |
|---|
| 30 | out: object |
|---|
| 31 | notes: |
|---|
| 32 | ---> |
|---|
| 33 | <cffunction name="get" access="public" output="No" returntype="any"> |
|---|
| 34 | <cfargument name="objName" required="false" type="string" /> |
|---|
| 35 | <cfargument name="singleton" required="false" type="boolean" default="true" /> |
|---|
| 36 | |
|---|
| 37 | <cfscript> |
|---|
| 38 | var obj = ''; //local var to hold object |
|---|
| 39 | if (arguments.singleton and singletonExists(arguments.objName)) { |
|---|
| 40 | return getSingleton(arguments.objName); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | switch(arguments.objName) { |
|---|
| 44 | case "conference": |
|---|
| 45 | obj = createObject('component','conference').init(); |
|---|
| 46 | if (arguments.singleton) { // scope singleton |
|---|
| 47 | addSingleton(arguments.objName, obj); |
|---|
| 48 | } |
|---|
| 49 | // inject dependencies through setter |
|---|
| 50 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 51 | obj.setForum( get('forum', arguments.singleton) ); |
|---|
| 52 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 53 | return obj; |
|---|
| 54 | break; |
|---|
| 55 | |
|---|
| 56 | case "forum": |
|---|
| 57 | obj = createObject('component','forum').init(); |
|---|
| 58 | if (arguments.singleton) { // scope singleton |
|---|
| 59 | addSingleton(arguments.objName, obj); |
|---|
| 60 | } |
|---|
| 61 | // inject dependencies through setter |
|---|
| 62 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 63 | obj.setThread( get('thread', arguments.singleton) ); |
|---|
| 64 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 65 | return obj; |
|---|
| 66 | break; |
|---|
| 67 | |
|---|
| 68 | case "galleonSettings": |
|---|
| 69 | obj = createObject('component','galleon'); |
|---|
| 70 | if (arguments.singleton) { // scope singleton |
|---|
| 71 | addSingleton(arguments.objName, obj); |
|---|
| 72 | } |
|---|
| 73 | return obj; |
|---|
| 74 | break; |
|---|
| 75 | |
|---|
| 76 | case "message": |
|---|
| 77 | obj = createObject('component','message').init(); |
|---|
| 78 | if (arguments.singleton) { // scope singleton |
|---|
| 79 | addSingleton(arguments.objName, obj); |
|---|
| 80 | } |
|---|
| 81 | // inject dependencies through setter |
|---|
| 82 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 83 | obj.setThread( get('thread', arguments.singleton) ); |
|---|
| 84 | obj.setForum( get('forum', arguments.singleton) ); |
|---|
| 85 | obj.setConference( get('conference', arguments.singleton) ); |
|---|
| 86 | obj.setUser( get('user', arguments.singleton) ); |
|---|
| 87 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 88 | return obj; |
|---|
| 89 | break; |
|---|
| 90 | |
|---|
| 91 | case "rank": |
|---|
| 92 | obj = createObject('component','rank').init(); |
|---|
| 93 | if (arguments.singleton) { // scope singleton |
|---|
| 94 | addSingleton(arguments.objName, obj); |
|---|
| 95 | } |
|---|
| 96 | // inject dependencies through setter |
|---|
| 97 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 98 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 99 | return obj; |
|---|
| 100 | break; |
|---|
| 101 | |
|---|
| 102 | case "thread": |
|---|
| 103 | obj = createObject('component','thread').init(); |
|---|
| 104 | if (arguments.singleton) { // scope singleton |
|---|
| 105 | addSingleton(arguments.objName, obj); |
|---|
| 106 | } |
|---|
| 107 | // inject dependencies through setter |
|---|
| 108 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 109 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 110 | obj.setMessage( get('message', arguments.singleton) ); |
|---|
| 111 | return obj; |
|---|
| 112 | break; |
|---|
| 113 | |
|---|
| 114 | case "user": |
|---|
| 115 | obj = createObject('component','user').init(); |
|---|
| 116 | if (arguments.singleton) { // scope singleton |
|---|
| 117 | addSingleton(arguments.objName, obj); |
|---|
| 118 | } |
|---|
| 119 | // inject dependencies through setter |
|---|
| 120 | obj.setSettings( get('galleonSettings', arguments.singleton) ); |
|---|
| 121 | obj.setUtils( get('utils', arguments.singleton) ); |
|---|
| 122 | return obj; |
|---|
| 123 | break; |
|---|
| 124 | |
|---|
| 125 | case "utils": |
|---|
| 126 | obj = createObject('component','utils'); |
|---|
| 127 | if (arguments.singleton) { // scope singleton |
|---|
| 128 | addSingleton(arguments.objName, obj); |
|---|
| 129 | } |
|---|
| 130 | // inject dependencies through setter |
|---|
| 131 | return obj; |
|---|
| 132 | break; |
|---|
| 133 | |
|---|
| 134 | } |
|---|
| 135 | </cfscript> |
|---|
| 136 | |
|---|
| 137 | </cffunction> |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | <cffunction name="singletonExists" access="public" output="No" returntype="boolean"> |
|---|
| 141 | <cfargument name="objName" required="Yes" type="string" /> |
|---|
| 142 | <cfreturn StructKeyExists(variables.com, arguments.objName) /> |
|---|
| 143 | </cffunction> |
|---|
| 144 | |
|---|
| 145 | <cffunction name="addSingleton" access="public" output="No" returntype="void"> |
|---|
| 146 | <cfargument name="objName" required="Yes" type="string" /> |
|---|
| 147 | <cfargument name="obj" required="Yes" /> |
|---|
| 148 | <cfset variables.com[arguments.objName] = arguments.obj /> |
|---|
| 149 | </cffunction> |
|---|
| 150 | |
|---|
| 151 | <cffunction name="getSingleton" access="public" output="No" returntype="any"> |
|---|
| 152 | <cfargument name="objName" required="Yes" type="string" /> |
|---|
| 153 | <cfreturn variables.com[arguments.objName] /> |
|---|
| 154 | </cffunction> |
|---|
| 155 | |
|---|
| 156 | <cffunction name="removeSingleton" access="public" output="No" returntype="void"> |
|---|
| 157 | <cfargument name="objName" required="Yes" /> |
|---|
| 158 | <cfscript> |
|---|
| 159 | if ( StructKeyExists(variables.com, arguments.objName) ){ |
|---|
| 160 | structDelete(variables.com, arguments.objName); |
|---|
| 161 | } |
|---|
| 162 | </cfscript> |
|---|
| 163 | </cffunction> |
|---|
| 164 | |
|---|
| 165 | </cfcomponent> |
|---|