The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This topic describes how to implement the rule mode.
Policy mode refers to defining a series of algorithms, encapsulating them one by one, and making them replaceable. This mode allows algorithms to change independently of customers who use it. That is to say, these algorithms provide the same functions as external interfaces, but their implementations differ. It is better to encapsulate algorithms in policy mode. The following uses the cache replacement algorithm as an example to implement the policy mode.
What is the cache replacement algorithm? When a cache is missing, the cache controller must select a row in the cache and replace it with the data to be obtained. The selected policy is the cache replacement algorithm. The corresponding UML diagram is given below.
Replacealgorithm is an abstract class that defines the algorithm interface. There are three classes inherited from this abstract class, that is, the specific algorithm implementation. The replacement algorithm must be used in the cache class, so a replacealgorithm object is maintained. The structure of this UML diagram is a typical structure of the Policy mode. The following provides the corresponding implementation based on the UML diagram.
First, define the replacement algorithm.
// Abstract interface class replacealgorithm {public: Virtual void Replace () = 0 ;}; // three specific replacement algorithms class lru_replacealgorithm: Public replacealgorithm {public: void Replace () {cout <"least recently used replace algorithm" <Endl ;}}; class implements o_replacealgorithm: Public replacealgorithm {public: void Replace () {cout <"first in first out replace algorithm" <Endl ;}}; class random_replacealgorithm: Public replacealgorithm {public: void Replace () {cout <"random replace algorithm" <Endl ;}};
The definition of the cache is given. The key here is that the implementation of the cache directly affects the customer's usage. The key is how to specify the replacement algorithm.
Method 1: Specify parameters directly to input a pointer to a specific algorithm.
// The cache must use the replacement algorithm class cache {PRIVATE: replacealgorithm * m_ra; public: cache (replacealgorithm * RA) {m_ra = Ra ;}~ Cache () {Delete m_ra;} void Replace () {m_ra-> Replace ();}};
If this method is used, the customer needs to know the specific definitions of these algorithms. This method can only be used in the following ways. You can see that too many details are exposed.
Int main () {cache (New lru_replacealgorithm (); // exposes the algorithm definition cache. Replace (); Return 0 ;}
Method 2: It is also directly specified by parameters, but not a pointer, but a tag. In this way, the customer only needs to know the corresponding label of the algorithm, instead of the specific definition of the algorithm.
// The replacement algorithm Enum Ra {LRU, FIFO, random} is required for the cache; // The label class cache {PRIVATE: replacealgorithm * m_ra; public: cache (Enum RA) {If (RA = LRU) m_ra = new lru_replacealgorithm (); else if (RA = FIFO) m_ra = new paio_replacealgorithm (); else if (RA = random) m_ra = new random_replacealgorithm (); else m_ra = NULL ;}~ Cache () {Delete m_ra;} void Replace () {m_ra-> Replace ();}};
Compared with method 1, this method is much easier to use. In fact, this method combines the simple factory mode with the policy mode. The algorithm definition uses the policy mode, while the definition of cache actually uses the simple factory mode.
Int main () {cache (LRU); // specify the tag to cache. Replace (); Return 0 ;}
In the preceding two methods, the constructor requires parameters. Can constructors use no parameters? The third implementation method is provided below.
Method 3: Use the template. The algorithm is specified by the real parameters of the template. Of course, parameters are still used, but they are not parameters of the constructor. In policy mode, parameter transmission is difficult to avoid. You must specify an algorithm.
// The cache must use the replacement algorithm template <class Ra> class cache {PRIVATE: RA m_ra; public: cache (){}~ Cache () {} void Replace () {m_ra.replace ();}};
The usage is as follows:
Int main () {cache <random_replacealgorithm> cache; // template real parameter cache. Replace (); Return 0 ;}
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985