design - How to write a Java class following the SRP principle ? My specific case study -
i doing small project/exercise in among other things have create html parser evaluate server-side script, give values variables in -${ ... } - , that´s all.
to achieve have written single class called "parser" have 1 public method "parsehtml()" , couple of private methods hard work. issue comes when think 1 of 5 s.o.l.i.d principles: srp = class should have 1 reason change:
i got not idea how structure class having know private methods called inside public one. kind of facade pattern made method.
+ public parsehtml() - private getexpressionsfromhtml() //method grab ${..} file - private evaluatejavascript(script) - private deletejssnippetfromhtml() - private setvaluestoexpressionsinhtml() - private evaluatedataif() - private evaluatedatafor()
here there link author explain principle of single responsability well... still got doubts it:
http://www.codeproject.com/articles/567768/object-oriented-design-principles
so, missing in this?
when write code, tend code following these sort of design: define related methods single actor going use, , sort of interface/public method deal of mini private methods. keep them sort can a.k.a less 20? lines per method, 1 or 2 lines.
as can see above method´s names descriptive possible avoid write unnecessary comments.
regards
edit
the aim of application given "index.html" able to: * read file * process javascript in .html - javascript engine - rhino * parser final html render in browser.
a servlet take program , render "parsed file" in browser. servlet looks ...
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string path = request.getservletcontext().getrealpath("index.html") ; parser parser = new parser(); string screen = parser.doparsehtmlfile(path); printwriter out = response.getwriter(); out.println(screen); // rendering page, ok this? }
now list classes makes parse possible. record, parse entirely done using regular expressions, have heard many times not appropiate html due nested tags guess little project/exercise work:
parser.java
public string doparsehtmlfile(string filename) { /*not sure if method extract class called instance: proccesshtml. looks me facade class?*/ // readfile read.readfile(filename); this.htmlparsed = read.gethtmlfile(); // processserverside this.attributesmap = processor.processserverside(this.htmlparsed, read.getscriptsnippet()); // parsehtml parsehtml(); return this.htmlparsed; } private void parsehtml() { /*this method 1 keeping responsability in class of formatting htmlparsed variable*/ if( !htmlparsed.isempty() && !attributesmap.isempty() ) { this.removejsfromhtml(); this.setvaluestoexpressionsinhtml(); this.evaluatedataif(); this.evaluatedatafor(); }
}
processor.java
- public map processserverside(string file, string script) // method uses list of private below - facade ?
- private scriptengine processjavascript(string script)
- private void getdataattributes(string file)
- private void evaluatedataattributes(scriptengine engine)
all private methods relies on each other. attributes going evaluate if don´t call first 'getdataattributes'???
reader.java
- public void readfile(string filepath)
- public string getscriptsnippet() // javascript section
- public string gethtmlfile() // whole html
Comments
Post a Comment