I recently read the Head First model. This book is very well written. The only drawback is that the whole book is written in JAVA, although JAVA and C ++ are both object-oriented, some terms are still different. For example, JAVA contains the term "interface", which is not in C ++. However, these models can all be implemented in C ++.
In chapter 1's policy model, the duck example is a classic example, and I believe it will always happen in software development. Separate the unchanged things from the changed things, which can save a lot of time when you modify or add new functions in the future. Next, I will try to use C ++ to implement the policy mode. Due to limited capabilities, there may be some problems. You are welcome to point out your suggestions for modification and accept them modestly.
Let's start talking about duck. For the sake of simplification, we only assume that the fly needs to change. Will the duck be called a temporary assumption.
According to the description in the book, duck and fly should be separated. in JAVA, fly is used as an interface, and in C ++, fly is used as an abstract class, which is the same as described in the book, these two classes are of the same level. The duck class contains some common and unchanged behavior of ducks. The fly class contains two Abstract Functions to set whether to fly or not. In addition to these two classes, I also added cn_duck (Chinese duck) this class, And duck_fly, is a type of duck. We want it to fly, regardless of the actual situation, duck_fly is a virtual function in the fly class. The structure of the class chart is as follows:
See figure 1.
As you can see, the cn_duck class inherits the duck class, so it can call methods in the duck class, and the duck class has interface communication with the fly class, so that the cn_duck class can also call methods in the fly class, the premise is to pass a duck_fly Class Object to the cn_duck class. When you need to add more flights, you can add classes at the same level as the duck_fly class, for example, adding a rocket flight class. When adding a duck type, you can add classes on the cn_duck class level, for example, adding an American duck. This is the rule mode I understand. You do not need to modify the duck class. You do not need to modify the fly class when flying using other methods (unless you add the flight method but the flight-related behavior, for example, add duck wings to fly, and Chinese duck steel wings nb duck ). The code for the project is provided below. Please click it.
View plaincopy to clipboardprint?
// Duck. h
# Include "fly. h"
Class duck
{
Public:
Void quack ();
Void swim ();
Virtual void display ();
Fly * fy;
Void set_fly (fly * fy );
};
// Duck. h
# Include "fly. h"
Class duck
{
Public:
Void quack ();
Void swim ();
Virtual void display ();
Fly * fy;
Void set_fly (fly * fy );
};
View plaincopy to clipboardprint?
// Duck. cpp
# Include "duck. h"
# Include <iostream>
Using namespace std;
Void duck: quack ()
{
Cout <"I'm screaming" <endl;
}
Void duck: swim ()
{
Cout <"I am swimming" <endl;
}
Void duck: display ()
{
Cout <"I'm a duck Yellow mouth" <endl;
}
Void duck: set_fly (fly * fy)
{
This-> fy = fy;
}
// Duck. cpp
# Include "duck. h"
# Include <iostream>
Using namespace std;
Void duck: quack ()
{
Cout <"I'm screaming" <endl;
}
Void duck: swim ()
{
Cout <"I am swimming" <endl;
}
Void duck: display ()
{
Cout <"I'm a duck Yellow mouth" <endl;
}
Void duck: set_fly (fly * fy)
{
This-> fy = fy;
}
View plaincopy to clipboardprint?
// Fly. h
# Ifndef FLY_EGG
# Define FLY_EGG
Class fly
{
Public:
Virtual void can_fly () = 0;
Virtual void no_fly () = 0;
Virtual void fly_way () = 0;
};
# Endif
// Fly. h
# Ifndef FLY_EGG
# Define FLY_EGG
Class fly
{
Public:
Virtual void can_fly () = 0;
Virtual void no_fly () = 0;
Virtual void fly_way () = 0;
};
# Endif
View plaincopy to clipboardprint?
// Duck_fly.h
# Include <iostream>
# Include "fly. h"
Using namespace std;
Class duck_fly: public fly
{
Public:
Void can_fly ()
{
Cout <"duck fly! "<Endl;
}
Void no_fly ()
{
Cout <"duck not fly! "<Endl;
}
Void fly_way ()
{
Cout <"fly with wings" <endl;
}
};
// Duck_fly.h
# Include <iostream>
# Include "fly. h"
Using namespace std;
Class duck_fly: public fly
{
Public:
Void can_fly ()
{
Cout <"duck fly! "<Endl;
}
Void no_fly ()
{
Cout <"duck not fly! "<Endl;
}
Void fly_way ()
{
Cout <"fly with wings" <endl;
}
};
View plaincopy to clipboardprint?
// Cn_duck.h
# Include "duck. h"
Class cn_duck: public duck
{
Public:
Cn_duck ();
Void display ();
};
// Cn_duck.h
# Include "duck. h"
Class cn_duck: public duck
{
Public:
Cn_duck ();
Void display ();
};
View plaincopy to clipboardprint?
// Cn_duck.cpp
# Include "cn-duck.h"
# Include <iostream>
Using namespace std;
Cn_duck: cn_duck ()
{
Fy = NULL;
}
Void cn_duck: display ()
{
Cout <"I Am a red mouth" <endl;
}
// Cn_duck.cpp
# Include "cn-duck.h"
# Include <iostream>
Using namespace std;
Cn_duck: cn_duck ()
{
Fy = NULL;
}
Void cn_duck: display ()
{
Cout <"I Am a red mouth" <endl;
}
View plaincopy to clipboardprint?
// Main. cpp
# Include "cn-duck.h"
# Include "duck_fly.h"
Int main ()
{
// The specified interface indicates duck Fei.
Duck_fly * df = new duck_fly ();
Cn_duck cd;
Cd. set_fly (df); // set to fly
Cd. fy-> can_fly (); // call the flight in duck_fly
Cd. fy-> fly_way (); // call the flight method
Return 0;
}
// Main. cpp
# Include "cn-duck.h"
# Include "duck_fly.h"
Int main ()
{
// The specified interface indicates duck Fei.
Duck_fly * df = new duck_fly ();
Cn_duck cd;
Cd. set_fly (df); // set to fly
Cd. fy-> can_fly (); // call the flight in duck_fly
Cd. fy-> fly_way (); // call the flight method
Return 0;
}
The above is my understanding. You are welcome to give improvement suggestions. Of course, the program has some problems. If the duck_fly object is not given to cn_duck, the program will not prompt, but an error will be reported, however, it is also reasonable to report an error. The duck will not report an error without any doubt.
The following is the program running interface: