11
Delegate
- #include "Application.h"
- #include "Window.h"
- #include <iostream>
- int main()
- {
- using namespace std;
- Application app;
- Window w;
- cout << "Moving window to (100, 200)" << endl;
- w.moveTo( 100, 200);
- cout << "Setting delegate..." << endl;
- w.setDelegate( &app );
- cout << "Moving window to (10, 20)" << endl;
- w.moveTo( 10, 20 );
- cout << "Clearing delegate..." << endl;
- w.setDelegate( 0 );
- cout << "Moving window to (50, 70)" << endl;
- w.moveTo( 50, 70 );
- }
- #if !defined( _WINDOW_H )
- #define _WINDOW_H
- #include "WindowDelegate.h"
- class WindowDelegate;
- class Window
- {
- public:
- Window( unsigned x=0, unsigned y=0 ) : _x( x ), _y( y ) { _delegate = 0; }
- unsigned getX() { return _x; };
- unsigned getY() { return _y; };
- void moveTo( unsigned, unsigned );
- void setDelegate( WindowDelegate *wd ) { _delegate = wd; }
- private:
- unsigned _x, _y;
- WindowDelegate *_delegate;
- };
- #endif
- #include "Window.h"
- void Window::moveTo( unsigned x, unsigned y )
- {
- _x = x;
- _y = y;
- if ( _delegate )
- _delegate->windowDidMove( this );
- }
- #if defined( STANDALONE )
- #include <iostream>
- using namespace std;
- int main()
- {
- }
- #endif
- #if !defined( _WINDOW_DELEGATE_H )
- #define _WINDOW_DELEGATE_H
- class Window;
- class WindowDelegate
- {
- public:
- virtual void windowDidMove( Window * ) = 0;
- };
- #endif
- #include "WindowDelegate.h"
- #if defined( STANDALONE )
- #include <iostream>
- using namespace std;
- int main()
- {
- }
- #endif
- #if !defined( _APPLICATION_H )
- #define _APPLICATION_H
- #include "Window.h"
- #include "WindowDelegate.h"
- class Application : public WindowDelegate
- {
- public:
- void windowDidMove( Window * );
- };
- #endif
- #include "Application.h"
- #include <iostream>
- void Application::windowDidMove( Window *w )
- {
- std::cout << "\nWindow moved to (" << w->getX() << ", " << w->getY() << ")!!!\n" << std::endl;
- }
- TEST=tdel
- TEST_SRCS=main.cpp
- TEST_OBJS=$(TEST_SRCS:.cpp=.o)
- SRCS=Application.cpp Window.cpp WindowDelegate.cpp
- OBJS=$(SRCS:.cpp=.o)
- CPP=g++
- CPPFLAGS=-g
- all: $(OBJS)
- test: $(TEST)
- $(TEST): $(TEST_OBJS) $(OBJS)
- $(CPP) $(CPPFLAGS) $(TEST_OBJS) $(OBJS) -o $@
- .PHONY: clean wipe
- clean:
- rm -f *.o
- wipe: clean
- rm -f $(TEST)
$ make test
g++ -g -c -o main.o main.cpp
g++ -g -c -o Application.o Application.cpp
g++ -g -c -o Window.o Window.cpp
g++ -g -c -o WindowDelegate.o WindowDelegate.cpp
g++ -g main.o Application.o Window.o WindowDelegate.o -o tdel
$ tdel
Moving window to (100, 200)
Setting delegate...
Moving window to (10, 20)
Window moved to (10, 20)!!!
Clearing delegate...
Moving window to (50, 70)
Comments