代码很简单,无需注释
Author: Kevin Yin
#include <iostream>
using namespace std;
class KeyPressEvent
{
private:
int _refCount;
public:
KeyPressEvent()
{
_refCount = 0;
}
virtual void KeyDown(int a)=0;
virtual void KeyUp(int a)=0;
virtual void KeyPress(int a)=0;
void AddRef()
{
_refCount++;
}
void RemoveRef()
{
_refCount--;
}
int GetRefCount()
{
return _refCount;
}
};
class Window
{
protected:
KeyPressEvent *KeyPressListener;
public:
void AddKeyPressListener(KeyPressEvent *kpe)
{
KeyPressListener = kpe;
KeyPressListener->AddRef();
}
void RemoveKeyPressListener()
{
KeyPressListener->RemoveRef();
KeyPressListener = NULL;
}
~Window()
{
if (KeyPressListener != NULL){
KeyPressListener->RemoveRef();
if ( KeyPressListener->GetRefCount() == 1 ){
delete KeyPressListener;
}
}
}
};
class Windows2 : public Window
{
private:
class KeyKey : public KeyPressEvent
{
void KeyDown(int a)
{
cout<<"Key Down"<<endl;
}
void KeyUp(int a)
{
cout<<"Key Up"<<endl;
}
void KeyPress(int a)
{
cout<<"Key Press"<<endl;
}
};
public:
Windows2()
{
AddKeyPressListener(new KeyKey());
}
void Do()
{
if (KeyPressListener != NULL){
cout<<KeyPressListener->GetRefCount()<<endl;
KeyPressListener->KeyDown(1);
KeyPressListener->KeyUp(2);
KeyPressListener->KeyPress(3);
}
}
};
class KeyKey2 : public KeyPressEvent
{
void KeyDown(int a)
{
cout<<"Key Down"<<endl;
}
void KeyUp(int a)
{
cout<<"Key Up"<<endl;
}
void KeyPress(int a)
{
cout<<"Key Press"<<endl;
}
};
class Windows3 : public Window
{
public:
Windows3()
{}
void Do()
{
if (KeyPressListener != NULL){
cout<<KeyPressListener->GetRefCount()<<endl;
KeyPressListener->KeyDown(1);
KeyPressListener->KeyUp(2);
KeyPressListener->KeyPress(3);
}
}
};
int main()
{
Windows2 win;
win.Do();
Windows3 win3;
win3.AddKeyPressListener(new KeyKey2());
win3.Do();
return 0;
}
Author: Kevin Yin
#include <iostream>
using namespace std;
class KeyPressEvent
{
private:
int _refCount;
public:
KeyPressEvent()
{
_refCount = 0;
}
virtual void KeyDown(int a)=0;
virtual void KeyUp(int a)=0;
virtual void KeyPress(int a)=0;
void AddRef()
{
_refCount++;
}
void RemoveRef()
{
_refCount--;
}
int GetRefCount()
{
return _refCount;
}
};
class Window
{
protected:
KeyPressEvent *KeyPressListener;
public:
void AddKeyPressListener(KeyPressEvent *kpe)
{
KeyPressListener = kpe;
KeyPressListener->AddRef();
}
void RemoveKeyPressListener()
{
KeyPressListener->RemoveRef();
KeyPressListener = NULL;
}
~Window()
{
if (KeyPressListener != NULL){
KeyPressListener->RemoveRef();
if ( KeyPressListener->GetRefCount() == 1 ){
delete KeyPressListener;
}
}
}
};
class Windows2 : public Window
{
private:
class KeyKey : public KeyPressEvent
{
void KeyDown(int a)
{
cout<<"Key Down"<<endl;
}
void KeyUp(int a)
{
cout<<"Key Up"<<endl;
}
void KeyPress(int a)
{
cout<<"Key Press"<<endl;
}
};
public:
Windows2()
{
AddKeyPressListener(new KeyKey());
}
void Do()
{
if (KeyPressListener != NULL){
cout<<KeyPressListener->GetRefCount()<<endl;
KeyPressListener->KeyDown(1);
KeyPressListener->KeyUp(2);
KeyPressListener->KeyPress(3);
}
}
};
class KeyKey2 : public KeyPressEvent
{
void KeyDown(int a)
{
cout<<"Key Down"<<endl;
}
void KeyUp(int a)
{
cout<<"Key Up"<<endl;
}
void KeyPress(int a)
{
cout<<"Key Press"<<endl;
}
};
class Windows3 : public Window
{
public:
Windows3()
{}
void Do()
{
if (KeyPressListener != NULL){
cout<<KeyPressListener->GetRefCount()<<endl;
KeyPressListener->KeyDown(1);
KeyPressListener->KeyUp(2);
KeyPressListener->KeyPress(3);
}
}
};
int main()
{
Windows2 win;
win.Do();
Windows3 win3;
win3.AddKeyPressListener(new KeyKey2());
win3.Do();
return 0;
}