Once upon a time, there was a Tibetan little girl named Zhuoma. The family is very poor, leaning on the garbage (trash) for a living. Every day, you will be late, working hard. Zhuo Ma is only two kinds of garbage, waste paper (Paper) and gold (Gold) (Wow, no mistake, gold is also garbage), after home, then place garbage classification, waste paper into paper 箩 (Paperbin) ), Gold is put into the golden library (see a small girl with a treasure in the house to go to the garbage?). Ok, the story tells here, for the time being. Dry point.
Before entering the topic, I hope that the reader reads (Think In C 2nd Edition Volume 2, Chapter 10 Design Patterns) and you can download it at http://pcbook.51soft.com
In "Think In C ", the author implements garbage classification with Double Dispatch methods in oo.
And I use the GP method.
Class trash // Garbage {}; class paperbin; // trash (plain waste paper) Class Goldlib; // trash can (放) Class Paper: public trash // Waste paper {}; Class Gold: Public Trash // Gold {}; Class Paperbin: Public Vector
Class Management // Little Girl Zhuoma is an example of management {private: paperbin _mypaperbin; // Little girl Zhuoma home trash can only open Goldlib_mygoldlib;};
What we have to do is automatically classify the garbage. Waste paper is placed in Paperbin, gold is put into the Goldlib
Talking about the external language of programming, Coding Style. Personally, the programming style affects code quality and ultimately affects product quality. My style is: 1. The first letter of the class name. Such as: trash, gold. Don't use pinyin letters. Laji, Jingzhi. Pinyin letters are too bad in the program. Friends in English can use Jinshan word to translate. 2. When defining member functions or variables, first defined as private. 3. Private members will be underlined. Such as: _mypaperbin4. First define member variables. Before you want to talk. The programming style also includes many aspects, but it is not used in the code above. I don't want to talk about it. Interested friends can refer to << Windows Coding Style >> Microsoft Press
Finishing the programming style, saying a few questions about design ideas. The program is always from nothing, from it is unrelated to interconnection, from simple to complex, function is less than, and gradually. I will talk about how to do it. 1. From the inside: first pick the nouns in the request, create a Class. Don't take the relationship between them. In the above example, we first build a few empty classes first. Such as: Trash, Paper, Gold, Paperbin, Goldlib. Management. 2. From unrelated to interconnection: Consider the relationship between them. Such as: Paper, Gold and Trash are inheritance relationships, management and paperbin, Goldlib are relationships. 3. Consider the status of each class: that is, the member variable is defined. TRASH, Paper, Gold, we still don't know what to do. Defined as empty. Management is used to manage the trash, so it has two member variables. They are _mygoldlib, _mypaperbin. 4. Consider the interaction between them. This only needs to pick the verbs in the request. In the above example, Xiao Zhuoma put the waste paper into the paper, gold into the Goldlib. We define an action, pay attention, a small Poema made actions, put, add (paper, paperbin), add (gold, goldlib). 5. General Thinking: Xiao Zhuo Ma as a manager, there must be a good thing, but it is too tired. Whether we can buy a automatic waste classifying machine for Xiaowa. In this way, Xiao Zhuoma is only necessary to put it (garbage) add (Trash) (not Add (Paper, Paperbin), add (gold, goldlib)). Automatic waste classifiers put different garbage into different containers. This is the center of this chapter, and will talk about it later. I am talking about design ideas. Ok, we add a member function Add (trash) for management. Management looks like this: class management {private: paperbin _mypaperbin; goldlib _mygoldlib; public: template
6. Refine. That is to say, add (trash). And provide optimal implementation based on different situations. (Talking, there is no example, you can marry me. If you are analyzing allocator in STL, you will know that allocator can provide optimal memory according to different situations (TRIVIAL, NONTRIVIAL " Management plan. Jianlixuan "STL Original Analysis". There is a preview of the top 5 chapters on the Internet, enough to let you know the STL's principles. No need to buy a book. Brother also believes, Waiting Chinese is not very good, the same text method is the same, it is better to read E culture's refreshment)
The central idea of GP (Generic Programming, the same) is to achieve code reuse. In order to do code Reuse, a necessary condition is to reduce the coupling between the class. Every class should take things you should manage (personal self-sweeping door, surpassing others tile frost (problem with moral quality?)). Such as: Paper should only manage your own things, now there is fine, good, rest (empty class). Not like the paper in "Think In C ", how to put it from the ADD to the trash bin all day (PAPER wants to implement an add virtual function). Paperbin in "Think In C " is not something, you are a container, it should be restless, don't like the old man of the brothel, leaning on the door, say, come in, come in. I also kick it out (no money, I dare to visit brothel?) (Paperbin also implements an ADD virtual function, and cooperate with the Add of the PAPER to achieve the purpose of garbage classification). You can see that Paperbin and Paper in "Think In C " have done things that don't do. In the brothers' code, each class is dry and uninterrupted. Ok, the principles and methods are almost almost. It is now necessary. The ugly daughter-in-law is going to see the in-laws in the morning and evening.
Define header files, nothing to say #include
Class trash {}; class goldlib; class paper: public trash {public: typedef paperbin abin;
Class gold: public trash {public: typedef goldlib abin;
Class Paperbin: Public Vector
The following two classes are the difficulties in this chapter, they are used to determine that two types (T, U) are the same type. T = u? If yes, EXISTS in enum is true, otherwise exists = false Details in C / C Users journal, generic
Template
} Template
PUBLIC: TEMPLATE
};}; int Main () {management zhuoma; generate two garbage paper apapers; gold bold; put rubbish into the trash box zhuoma.Add (APAPER); zhuoma.add (agold); return 0;}
The description of the program is written here. Below, let us compare the methods and GP methods in "Think In C ". 1. Expansion: Xiao Zhuo Ma should add a job and pick waste glass. As long as we make the following modification: add two classes: class grassbin; class glass {public: typedef glasbin abin;}; class grassbin: public vector
Confused "Think In C ", you can try to add a job to see if it is so simple, the key is that it is not so clear.
2. Efficiency: GP method does not have to be strengthened, no vTable. So all OO methods are better in time and space.
3. Safety: GP method uses the powerful function of the compiler to compile. It is more early to find problems earlier than OO. Prevent bugs from bringing BUG to the product due to insufficient testing.
4. A simpler user interface: The user wants to know is only management.add (trash). Add a new category does not change the user interface.
Ok, I think so much. However, the story is still not finished. Our Xiaowa should sell garbage to the garbage collection station every week. At that time, we also consider the price of waste paper and waste glasses, as well as gold exchange rate (to deal with banks). How to do it? Decomposition of the next time (today our code is still valid, let everyone see the power of the GP expansion code function)
Speaking of nonsense: I am based on a hacker's basic concept: propagation knowledge, and improves together. The following statement: 1. You can freely copy, modify, and spread without profitability. 2. Please include these two nonsense included.
In addition, a friend wants to write a story of Zhuoma with me, please email give me. Bu3bu4@263.net. The condition is that your masterpiece has no title.
It's good to feel.