What's the best way of naming my class?
I have some reports; Report1, Report2, Report3
and I'm using some Builder classes to build these reports programmatically for different scenarios and sections.
One way of naming my Grid Table builder classes is this:
var builder = new Report1.GridTableBuilderForSection1()
where Report1 is a namespace (folder).
The second approach is to use this:
var builder = new GridTableBuilderForReport1ForSection1()
in the section approach name of the classes become very long.
What's the best way to name these classes in your view开发者_高级运维?
First of all "Section1" is a terrible choice. The above version is nicer, because you get to put things into different folders. Try be more imaginative, or whatever:
var bldr; (not so important)
bldr = new InitialReport.SectionHeaderBuilder()
bldr2= new InitialReport.SectionDescriptionBuilder()
bldr3= new GraphSummary.SectionDataBuilder()
alternatively: (sorted the other way, I like this a lot.. but it's kinda weird depending on what your native language is)
b1 = new ReportInitial.getBuilderSecHeader()
b2 = new ReportSubsequent.getBuilderSecMain()
b3 = new ReportFinal.getBuilderSecGraphics()
.. this is great if you use auto-completion a lot.
I've built and designed a successful high performance reporting engine for a data warehouse company a couple years ago and I used a Strategy pattern or Abstract Factory pattern to solve a similar issue...
Strategy Pattern (wiki)
Abstract Factory Pattern (wiki)
If you use a Strategy approach your code might look like this...
def report1 = new Report1()
report1.buildHeader(new FancyHeader(data);
report1.buildSection(new GridTable(data));
report1.buildFooter(new SimpleFooter(data));
Or if you simply looking to organize concrete classes with long names that very similar names you could use a type of static class hierarchy to make the code a little more readable...
def thing = Reports.GridBuilders.EmployeeGrids.DailyEmployeeAttendance
def another = Reports.GridBuilders.EmployeeGrids.WeeklyPayroll
def oneMore = Reports.GridBuilders.EmployeeGrids.Managers
精彩评论