c# - How can I avoid writing an empty template pattern hook method? -
i'm trying apply template pattern 'hook', that's activated boolean 'true' value virtual method defaults false. implementing classes can override activate hook. however, functionality within hook has virtual because not implementing classes need implement it. below implementation not aesthetically pleasing me - there better way, avoid default exception throw?
// template pattern applied in function public virtual tobject compileentityfromdocument<tobject>(document document) { tobject dummyobject = createentity(); setattributesforentity(dummyobject, null, null); if (mustsetrelationsforentity()) { setrelationsforentity(dummyobject, null); } return (tobject)dummyobject; } public abstract void setattributesforentity(object entity, document maindocumenttocompilefrom, list<vfield> documentfieldrecords); public abstract tobject createentity<tobject>(); public virtual void setrelationsforentity<tobject>(tobject entity, list<vfield> documentfieldrecords) { throw new notimplementedexception("this must overridden in implementing classes"); } public virtual bool mustsetrelationsforentity() { return false; }
why don't leave empty in base, , let derived classes override when relations must set?
like this:
// template pattern applied in function public virtual tobject compileentityfromdocument<tobject>(document document) { tobject dummyobject = createentity(); setattributesforentity(dummyobject, null, null); setrelationsforentity(dummyobject, null); return (tobject)dummyobject; } public abstract void setattributesforentity(object entity, document maindocumenttocompilefrom, list<vfield> documentfieldrecords); public abstract tobject createentity<tobject>(); public virtual void setrelationsforentity<tobject>(tobject entity, list<vfield> documentfieldrecords) { //do nothing in base }
Comments
Post a Comment