面向对象编程和基于对象编程

来源:互联网 发布:阿里云服务器怎么用 编辑:程序博客网 时间:2024/05/21 17:22

面向对象编程:

        面向对象也就是把对象作为“接口”暴露出去,一般内部是一个 接口 或者抽象类。

 

    Thread.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef _THREAD_H_ 
#define _THREAD_H_
#include <pthread.h>
classThread{
public:
 
    Thread();
    virtual~Thread();
    voidStart();
    voidJoin();
 
private:
    // 纯虚函数 ²»ÐҪ 
    voidRun() =0 ;
    pthread_t threadId_ ; 
 
};#ifndef _THREAD_H_ 
#define _THREAD_H_
#include <pthread.h>
classThread{
public:
 
    Thread();
    virtual~Thread();
    voidStart();
    voidJoin();
 
private:
    // 纯虚函数 ²»ÐҪ 
    voidRun() =0 ;
    pthread_t threadId_ ; 
 
};



 Thread.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Thread.h"
#include <iostream>
usingnamespace std;
 
 
Thread::Thread() : autoDelete_(false)
{
    cout<<"Thread ..."<<endl;
}
 
Thread::~Thread()
{
    cout<<"~Thread ..."<<endl;
}
 
voidThread::Start()
{
    pthread_create(&threadId_, NULL, ThreadRoutine,this);
}
 
voidThread::Join()
{
    pthread_join(threadId_, NULL);
}
 
void* Thread::ThreadRoutine(void* arg)
{
    Thread*thread = static_cast<Thread*>(arg);
    thread->Run();
    if(thread->autoDelete_)
        deletethread;
    returnNULL;
}
 
voidThread::SetAutoDelete(boolautoDelete)
{
    autoDelete_ = autoDelete;
}




Thread_test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Thread.h"
#include <unistd.h>
#include <iostream>
usingnamespace std;
 
classTestThread : publicThread
{
public:
    TestThread(intcount) : count_(count)
    {
        cout<<"TestThread ..."<<endl;
    }
 
    ~TestThread()
    {
        cout<<"~TestThread ..."<<endl;
    }
 
private:
    voidRun()
    {
        while(count_--)
        {
            cout<<"this is a test ..."<<endl;
            sleep(1);
        }
    }
 
    intcount_;
};
 
int main(void)
{
    /*
    TestThread t(5);
    t.Start();
 
    t.Join();
    */
 
    TestThread* t2 =new TestThread(5);
    t2->SetAutoDelete(true);
    t2->Start();
    t2->Join();
 
    for(; ; )
        pause();
 
    return0;
}





基于对象编程:

       基于对象编程,提供出来的就不是“接口”了, 而是一个具体的类,虽然他和面向对象想一个都是起到回调的作用,但是他的回调不是通过继承来实现相应的虚函数,可以独立的函数,或者成员函数。看下面的例子就知道了。

        Thread.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _THREAD_
#define _THREAD_
#include <pthread.h>
#include <boost/function.hpp>
classThread {
public:
    typedefboost::function<void()> ThreadFunc ;
    explicitThread( constThreadFunc &func) ;
    ~Thread();
    voidStart () ;
    voidJoin();
    voidSetAutoDelete(boolautoDelete);
private:
    staticvoid* ThreadRoutine(void *arg) ;
    voidRun();
    ThreadFunc func ;
    pthread_t threadId ;
    boolautoDelete_;
} ;
#endif // _THREAD_H_


    


Thread.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Thread.h"
#include <boost/bind.hpp>
#include <iostream>
usingnamespace std ;
Thread::Thread( const ThreadFunc &func_ ) : func(func_),autoDelete_(false){
 
}
Thread::~Thread()
{
    cout<<__FUNCTION__<<endl;
}
 
 
voidThread::Start()
{
    pthread_create(&threadId, NULL, ThreadRoutine,this);
}
 
voidThread::Join(){
 
    pthread_join(threadId,NULL);
}
void* Thread::ThreadRoutine( void*arg){
    Thread *thread= static_cast<Thread*>(arg) ;
    thread->Run();
    if( thread->autoDelete_)
        delete(thread);
    cout<<"After delete"<<endl;
 
 
 
}
voidThread::Run(){
    func();
}
voidThread::SetAutoDelete(boolautoDelete)  {
    this->autoDelete_ = autoDelete;
 
}


Thread_test.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <unistd.h>
#include "Thread.h"
#include <iostream>
#include <boost/bind.hpp>
usingnamespace std ;
 
classNumberFunction{
public:
    NumberFunction(int count):coun_t(count){}
        voidNFunction() {
        while(coun_t-- && coun_t>0){
            cout<<__FUNCTION__<<endl;
            sleep(1);
        }
    }
private:
    intcoun_t ;
};
 
voidthreadFunc (){
 
    inti = 9 ;
    while(i--){
        cout<<"threadFunc...!!"<<endl ;
        sleep(1);
    }
 
    cout<<endl ;
}
 
voidthreadFunc2(inti ){
    while(i--&&i>=0){
        cout<<"Threadfunc2 ...."<<endl;
        sleep(1);
    }
 
}
int main (){
 
    Thread *t =new Thread(threadFunc);
    Thread *t2 =new Thread(boost::bind( threadFunc2 , 10)) ;
    NumberFunction foo(10) ;
    Thread *t3 =new Thread( boost::bind(&NumberFunction::NFunction,&foo)) ;
    t->SetAutoDelete(true);
    t->Start();
 
    t2->SetAutoDelete(true) ;
    t2->Start();
 
    t3->SetAutoDelete(true) ;
    t3->Start();
 
    t2->Join();
    t->Join();
    t3->Join();
    inti = 9 ;
    while(i--){
        cout<<__FUNCTION__<<endl;
    }
 
 
    return0 ;
}


我们看一下下面这张图

class EchoServer
{
    public :
        EchoServer(){
            server.SetConnectionCallback (boost::bind( onConnection()) ;
            server.SetConnectionCallback (boost::bind( onMessage() ) ;
            server.SetConnectionCallback (boost::bind( onClose() ) ;
            TcpServer server ;
        }
}

面向对象风格:
        用一个EchoServer 继承TcpServer(抽象类),然后EchoServer实现三个接口onConnection、onMessage、onClose。
基于对象风格:
        用一个EchoServer包含一个TcpServer(具体类)对象,在构造函数中使用boost::bind来注册 三个成员函数 onConnection、onMessage、onClose。