|
I'm a newbie, know a little about templates, for example
First a template class e.g.
template <class T, class HandleType>
class StartThread
{
private:
..............;
public:
HandleType Run (T&t)
{
...;
return t.run ();
}
};
The template class means that this T has a member function that returns HandleType run ();
You can put this paragraph .....; return t.run (); This part of the code uses the linux version or win32 version of your library, etc. It is only related to run (); this text (macro can also )
But if you change to OO, this is what it looks like:
#if defined (_WIN32)
typedef void * MyLibHandle;
#else
typedef int MyLibHandle;
#endif
class AbstractRunnerable
{
virtual MyLibHandle run () = 0;
};
class StartThread
{
public:
MyLibHandle Run (AbstractRunnerable&t)
{
.....;
return t.run ();
}
};
This is just a small example, maybe others have more elegant OO methods.
I personally think that both the run-time polymorphism of OO and the compile-time polymorphism of GP provide a function similar to control inversion.
In the final analysis, this GP StartThread or OO StartThread can only guarantee that a part of the code is fixed, they do not have complete behavior, and need external types to fill, inheriting AbstractRunnerable or
The meaning of StartThread <Runner> is to fill a behavior; I think that theoretically it doesn't matter which one you choose, but this is just some basic libraries, and their processes are basically fixed, such as vector, list, or thread , Most of their processes are fixed, such as sorting, etc., just need to inject an operator <and so on; GP is more suitable to express the concept of the process. The template pattern of the design pattern is almost the same;
However, in most software development, it may be the process that is constantly changing. At this time, the function of the packaging process (algorithm) provided by the GP is not very useful. You can use OO to express a type. |
|