Again, this is to discuss the responsibilities of the class and the encapsulated content of the object file. Class responsibilities refer to what should we do for a class? How much? Taking my library simulation as an example, obviously, one of the main responsibilities is to display the results, that is, the library list. However, many methods are required in the library list. Do these methods need to be implemented in the main class? As in the previous example, we still have three classes: ratingbook, bookdepot, and Main. What are their respective responsibilities? We can focus on the methods that involve ratingbook. First, like all object files, ratingbook should have methods for setting data attributes and returning data attributes, and of course there is also a constructor. Generally, the principle that should be followed at the beginning of the design class is that the responsibilities are as simple as possible so that the readability is high.
Then there is bookdepot. The biggest responsibility of this class is to generate a book, but since I set it randomly in a string array, to be close to the real situation, we need to remove some duplicates. Originally, this partCodeI put it in the main class, but then I put it in bookdepot, because I think the main function of this class is to return a Booklist, so as an additional requirement, returning a non-repeated booklist can reduce the logic redundancy of the Code (the so-called logical redundancy means that a piece of code contains too many irrelevant logic. Besides, I use it as a static method here because I need to get a booklist in two places, but if I want to create a bookdepot in both places, I will change it to a static method, in this way, you do not need to create an object that is useless at all.
Finally, it is the main class. Of course, after the above modification, the logic of the main class has been much simpler, and its main job is to process the list, which can be placed in the main class, if you put them in bookdepot, this object file will assume too many responsibilities, and some of them should not be processed when the object is generated, its main function is to generate the list we want without repeating it. As for the list processing, it should be handed over to other classes for processing. Of course, if we want to write a processing class, we can also, however, in this way, the main class is just to start the processing class. I think this kind of processing is an excessive subdivision of the class's responsibilities. The class's responsibilities do need to be subdivided. This is a very important principle of restructuring, however, blind subdivision is not a correct choice. Therefore, the primary class must assume certain responsibilities.
Based on the above, I think that the class should assume many responsibilities. This problem should be related to our logical classification. Just like in my example, all logics can be divided into the production of Booklist, as for the setting of ratingbook and the processing of Booklist, there are three responsibility classes, which then process these logics. This is also applicable to object files. The logic of object file encapsulation should be related to its own logic. However, you must be aware that, that is, the logic of object files should not contain the information of other objects, especially the creation of objects.
Finally, I want to talk about the naming of method names. In my opinion, the Code with high readability does not have to be annotated in detail. In fact, the method name and parameter list are already the best annotations. If the method name is correct, we can use the method name as a comment. Therefore, the method name is very important. Of course, the method name is determined based on the function and parameter list of the method. It really requires a high level of literacy. For example, in my example, it is really difficult to meet the requirements, this requires constant exploration in practice.
This refactoring once again exposes the weakness of my extremely low programming literacy, and even I don't even know some basic common sense. For example, a member variable with m in front of a general name is a class member variable, the static member with S is a member, but I don't know it. It has always been named casually. It can be seen that the disadvantage of not reading other people's code is so obvious! In addition, I also found that if the class is created in the Code only to call a method in it, there is no other purpose, then set this method to static, in this way, you do not need to create useless class instances every time.
This example is far from complete, because it contains many things that I need to think about until I think it is almost no problem.
Public Class Library { Public Static Void Main (string [] ARGs) Throws Interruptedexception, ioexception {searchbookandgetresultofsearch ();}
Private Static Void Searchbookandgetresultofsearch (string name) Throws Ioexception {string name =Enterandgetname ();
List <Ratingbook> List = Getbooklistfromzhongzhuantaibyname (name ); List <Ratingbook> depotlist = bookdepot. createbooklistfromdepot (10, 1 ); List <Ratingbook> booklist = Getlistcontainsname (name, depotlist ); For ( Ratingbook librarybook: booklist ){ For ( Ratingbook zhongzhuantaibook: List ){ If (Librarybook. getname (). Equals (zhongzhuantaibook. getname () {librarybook. setrating (zhongzhuantaibook. getrating () ;}} showlist ( "List of books in the library :", Booklist );}
Private StaticString enterandgetname ()ThrowsIoexception {
Bufferedreader name=NewBufferedreader (NewInputstreamreader (system. In ));
String entername=Name. Readline (). tolowercase ();
ReturnEntername;
}
Private Static List <ratingbook>Getbooklistfromzhongzhuantaibyname (string name ){ List <Ratingbook> List = bookdepot. createbooklistfromdepot (10, 6 ); List <Ratingbook> containsbook = Getlistcontainsname (name, list); showlist ( "List of intermediate turntable books :" , List); showlist ( "Eligible books :" , Containsbook ); Return List ;} Private Static List <ratingbook>Getlistcontainsname (string name, list <Ratingbook> List) {list <Ratingbook> booklist = New Arraylist <ratingbook> (); For ( Ratingbook: List ) {String book = Ratingbook. getname (). tolowercase (); If (Book. Contains (name) {Booklist. Add (ratingbook );}} Return Booklist ;} Private Static Void Showlist (string, list <ratingbook> List) {sortbookbyrating (list); system. Out. println (string ); For ( Ratingbook book: List ) {System. Out. println (book. getname () + "rating:" + book. getrating ());}} Private Static Void Sortbookbyrating (list <ratingbook> List ){ For ( Int I = 0; I <list. Size ()-1; I ++ ){ For ( Int J = I + 1; j <list. Size (); j ++ ){ If (List. Get (I). getrating () <= (List. Get (j). getrating () {string temp = List. Get (I). getname (); Int Rating = List. Get (I). getrating (); list. Get (I). setname (list. Get (j). getname ());
List. Get (I). setrating (list. Get (j). getrating ());
List. Get (I). setname (temp );
List. Get (I). setrating (rating );
}}}}}
Public Class Ratingbook { Private Int Mrating = 0 ; Private String mname = ""; Public Ratingbook (){} Public Ratingbook (string name, Int Rating) {mname = Name; mrating = Rating ;} Public Void Setrating ( Int Rating) {mrating = Rating ;} Public Void Setname (string name) {mname = Name ;} Public String getname (){ Return Mname ;} Public Int Getrating (){ Return Mrating ;} }
Public Class Bookdepot { Static String [] sbookdepot = {"Java and JavaScript", "Java and C ++" , "Java and C", "SQL", "cehnjinsheng", "Java", "C ++", "JavaScript" , "Java", "Java and C", "Java and SQL", "chenjinsehng", "int" }; Static List <ratingbook> createbooklistfromdepot ( Int Num, Int Rating) {list <Ratingbook> List = New Arraylist <ratingbook> (); Int Len = new random (). nextint (Num );
For ( Int I = 0; I <Len; I ++ ) {Ratingbook = New Ratingbook (sbookdepot [Len], returnrandom (rating); list. Add (book);} List <Ratingbook> newlist = Removesamebookfromlist (list ); Return Newlist ;} Private Static List <ratingbook> removesamebookfromlist (list <ratingbook> List) {Map <String, integer> map = Createmapwithsingalkeyofbookname (list); List <Ratingbook> booklist = New Arraylist <ratingbook> (); @ Suppresswarnings ( "Rawtypes" ) Set entries = Map. entryset (); If (Entries! = Null ) {@ Suppresswarnings ( "Rawtypes") Iterator = Entries. iterator (); While (Iterator. hasnext () {@ suppresswarnings ( "Rawtypes" ) Map. Entry entry = (Entry) iterator. Next (); string key = (String) entry. getkey (); integer value = (Integer) entry. getvalue (); ratingbook = New Ratingbook (); book. setbook (Key, value); Booklist. Add (book );}} Return Booklist ;} Private Static Map <string, integer> Createmapwithsingalkeyofbookname (list <Ratingbook> List) {Map <String, integer> map = New Hashmap <string, integer> (); For (Ratingbook book: List ) {Map. Put (book. getname (), Book. getrating ());} Return Map ;} }