c# - Design Pattern for creating a set of data objects -
i'm writing code stuff , i'm pretty sure not designed @ moment can't think how should refactoring make nicer...
the simple summary have code goes through files in directory structure , different directories contain different content types. have limited number of these content types , have content type object creating lot of add list in way such follows:
contenttypes.add(new contenttype { contentname = "2010 call report", foldername = "2010 reports", renamefile = false }); contenttypes.add(new contenttype { contentname = "2010 email report", foldername = "2010 reports", renamefile = false }); contenttypes.add(new contenttype { contentname = "above average call recording", foldername = "call recordings", renamefile = true, hasmultiple = true }); contenttypes.add(new contenttype { contentname = "below average call recording", foldername = "call recordings", renamefile = true, hasmultiple = true });
this doesn't feel right (11 lines of virtually identical code in total) can't think else should doing.
the contenttype
class contents few properties can seen above , single public method called getnewfilename
. getnewfilename
method simple , shared content types. however, want have few of contenttype
objects have own versions of method...
things have considered are:
1) subclass contenttype
create class each content type
this didn't seem right me because i'd have 11 classes, of never have information altered , there never point in having more 1 of. didn't same right class (i know singletons have heard if using them may doign wrong).
2) func
property on contenttype
i figured set delegate on contenttype
deal getnewfilename
function being different still feels messy generating them in way described above.
3) factory classes
i've never had use factory classes before (as far i'm aware) know used generating classes. reading on them suggested pattern used generating different subtypes rather set of instances of class.
4) config file
the data have above put in config file or database or , loaded , looped through generate more nicely (this occured me) still wouldn't solve problem of varying getnewfilename
method. not sure can put delegate in config file easily. :)
5) having different getnewfilename methods on 1 class
i have content class have different methods want , use kind of select choose right one. seems missing point bit though.
so can suggest way this?
here current signature contenttype
class (with logic cut away - ask if think relevant).
public class contenttype { public string contentname { get; set; } public string foldername { get; set; } public bool renamefile { get; set; } public bool hasmultiple { get; set; } public string getnewfilename(string originalfilename, int fileindex) {...} // method needs diffent things different contenttypes }
if want more details of how class used ask , can paste in didn't want swamp class in code didn't think relevant.
this 1 use bit of code (to move files around appropriate directories put on new website , ensure named correctly) best possible code isn't vital going bug me if don't @ least know should doing. if correct way looks take long (eg rewrite code scratch) won't bother @ least i'll know next time. :)
p.s. realise constructor or 2 set initial values , making them readonly appropriate change make neaten still doesn't solve problems.
have contenttype
class base class , make getnewfilename
method virtual. derive contenttype
classes each file-type may need special handling in getnewfilename
method , override virtual implementation. create instances of inherited classes needed when file-types require special handling found in directory search, otherwise create instance of contenttype
class.
public class contenttype { public virtual string getnewfilename(string originalfilename, int fileindex) { // file name here } } public sealed class specialcontenttype : contenttype { // inherrits properties of contenttype public override string getnewfilename(string originalfilename, int fileindex) { // special file name here } }
Comments
Post a Comment