How do you write your Factory?

12 views
Skip to first unread message

Henry

unread,
Feb 24, 2010, 12:27:53 PM2/24/10
to CFCDev
Currently I'm writing mine like this, imagine there's an entity calls
Entity, and subclass of entity calls EntityX, EntityY, EntityZ

EntityFactory.cfc:

/** Dependency injection */
property service;

/** some constant */
property c;

EntityX function createEntityX(required a) {
var x = new EntityX();

x.setService(s);
x.setConstant(c);
x.setA(a);

return entityX;
}

EntityY function createEntityY(required b) {
// similar stuff
}

EntityZ function createEntityZ(required c, d) {
// similar stuff
}

// -------------------- END ----------------------


As you can see, I map one class to one method in the Factory. I like
it because the user of my API can just browse to this CFC and see all
the different type of Entity he can create. Also, since CF9 orm can't
let us use init() with required argument, I pushed this responsibility
to the factory.

Currently my EntityService does not have handle any creation, and the
user of this API needs to get the EntityFactory from Coldspring to
create instances of EntityX/Y/Z. It is because I don't want to add 10
methods in both EntityService and EntityFactory if I have 10 Entity
subtypes.

What do you think? Is this bad?

Is this how you usually write your factory? How can I improve my
version?


Thanks,
Henry

Kevan Stannard

unread,
Feb 27, 2010, 3:25:15 PM2/27/10
to cfc...@googlegroups.com
Henry, this is also generally how I write factories, with one exception. You mentioned that you subclass this factory to use it's methods - is that what you meant? I'd compose your factory into whatever object needs it and not subclass it.

Kevan



--
You received this message because you are subscribed to the Google Groups "CFCDev" group.
To post to this group, send email to cfc...@googlegroups.com.
To unsubscribe from this group, send email to cfcdev+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfcdev?hl=en.


Tom Chiverton

unread,
Mar 1, 2010, 10:17:52 AM3/1/10
to cfc...@googlegroups.com
On Wednesday 24 Feb 2010, Henry wrote:
> EntityY function createEntityY(required b) {
> // similar stuff
> }
>
> EntityZ function createEntityZ(required c, d) {
> // similar stuff
> }

I have a single createEntity()-like method with the return type set to the
common interface, something like:
<cffunction name="init" returntype="foo.IBar">
<cfargument name="type">
<cfargument name="email">
<cfset var rpt=createObject("component",foo.#arguments.type#Bar")>
<cfset rpt.setArgument('email',arguments.email)>
<cfreturn rpt>
</cffunction>

--

Tom Chiverton
Developer

Tom.Ch...@halliwells.com
3 Hardman Square, Manchester, M3 3EB

****************************************************

This email is sent for and on behalf of Halliwells LLP.

Halliwells LLP is a limited liability partnership registered in England and Wales under registered number OC307980 whose registered office address is at Halliwells LLP, 3 Hardman Square, Spinningfields, Manchester, M3 3EB. A list of members is available for inspection at the registered office together with a list of those non members who are referred to as partners. We use the word “partner” to refer to a member of the LLP, or an employee or consultant with equivalent standing and qualifications. Regulated by the Solicitors Regulation Authority.

CONFIDENTIALITY

This email is intended only for the use of the addressee named above and may be confidential or legally privileged. If you are not the addressee you must not read it and must not use any information contained in nor copy it nor inform any person other than Halliwells LLP or the addressee of its existence or contents. If you have received this email in error please delete it and notify Halliwells LLP IT Department on 0870 365 2500.

For more information about Halliwells LLP visit
www.Halliwells.com.

Reply all
Reply to author
Forward
0 new messages