Take a simple example to remember this abstract technique:
< issues >implement three methods: 1, invert string, 2, invert list;3, invert all elements in set<string>Requirements: The parameter check, the result check</questions > General notation: (The logic of the following three methods is 1. parameter check 2. Processing 3. Result verification, except 2 is basically the same, how much code because of various checks and become bloated t_t)
Publicstring Reverse (String str) {if(str = =NULL){ //playing Log,return} String s=stringutils.reverse (str); if(s = =NULL){ //playing Log,return } returns; } Publiclist Reverse (List list) {if(List = =NULL){ //playing Log,return} List result=lists.reverse (list); if(Result = =NULL){ //playing Log,return } returnresult; } PublicSet<string> Reverse (set<string>stringset) { if(Stringset = =NULL){ //playing Log,return} Set<String> set =Sets.newhashset (); for(String s:stringset) {Set.add (Stringutils.reverse (s)); } if(Set = =NULL){ //playing Log,return } returnset; }
Abstract Writing: (Passing parameters and processing methods to the processor, check the things such as the processor unified completion)
//first define the interface, only one method, pass in a parameter, get a return value Public InterfaceProcessor<p,r>{ PublicR Process (P p);}//To write a processor, the logic of this method is: 1, the parameter check 2, processing 3, the result is verifiedPrivate<p, r> R dosomething (P p, processor<p, r>processor) {if(p = =NULL){//playing Log,return}r Result=processor.process (p);if(Result = =NULL){//playing Log,return}returnresult;} //The following three questions require the method as follows, the parameters and the way to deal with the above dosomething processing, directly return the results can be//you can see that all three methods just rewrite the process method Publicstring Reverse (String str) {returnDoSomething (str,NewProcessor<string, string>() {@Override Publicstring Process (string s) {returnStringutils.reverse (s);}});} Publiclist Reverse (List list) {returnDoSomething (list,NewProcessor<list, list>() {@Override Publiclist process (List list) {returnlists.reverse (list);}});} PublicSet<string> Reverse (set<string>stringset) {returnDoSomething (Stringset,NewProcessor<set<string>, set<string>>() {@Override PublicSet<string> Process (set<string>strings) {Set<String> set =Sets.newhashset (); for(String s:strings) {Set.add (Stringutils.reverse (s));}returnset;});}
Summary:for some methods that have a lot of public operations, consider extracting the different parts of them, and the public ones doing it in one place, so that you don't need to change them all when you modify a part of the public, and the logic is clearer. also attached:The reverse of the list used above is the Lists.reverse () method provided by guava, and the realization of this method is very useful for learning. Generally speaking of reversing list, think of is to move the list of elements N to size-n-1 place, time complexity is n, but the implementation of guava is not the same, the time complexity of 1! It does this by re-implementing a Reverselist class, inheriting list, overriding the list method, replacing the normal list with position-related methods, such asGet (index) {return Forwardlist.get (size-1-index);//forwardlist is the list before conversion, as a member variable of reservelist exists}
In other words, the data has not changed at all, just change the way the outside operation data! A new way of thinking has wood, more detailed implementation of the guava source code to see the specific implementation ~
Java Learning: The abstraction of processing logic, processor