├── 1010 ├── protected.cpp ├── protected2.cpp └── url ├── 1020 ├── upcasting1.cpp ├── upcasting2.cpp └── url ├── 1030 ├── abstract.cpp ├── coupling1.cpp ├── coupling2.cpp └── url ├── 1040 ├── shape1.cpp ├── shape2.cpp ├── shape3.cpp └── url ├── 2010 ├── edit1.cpp ├── edit2.cpp ├── edit3.cpp └── url ├── 2020 ├── app.cpp ├── func1.cpp ├── func2.cpp ├── policy1.cpp ├── policy2.cpp ├── policy3.cpp └── url ├── 2030 ├── state1.cpp ├── state2.cpp ├── state3.cpp ├── state4.cpp └── url ├── 3010 ├── menu1.cpp ├── menu2.cpp ├── menu3.cpp ├── menu4.cpp └── url ├── 3020 ├── menu.hpp ├── menu_event1.cpp ├── menu_event2.cpp ├── menu_event3.cpp ├── menu_event4.cpp ├── menu_event5.cpp ├── menu_event6.cpp ├── menu_event7.cpp └── url ├── 3030 ├── decorator1.cpp ├── decorator2.cpp ├── decorator3.cpp ├── decorator4.cpp ├── decorator5.cpp └── url ├── 4010 ├── TextView.h ├── adapter1.cpp ├── adapter2.cpp ├── adapter3.cpp ├── adapter4.cpp └── url ├── 4020 ├── CalcProxy.cpp ├── ICalc.h ├── Proxy.cpp ├── client1.cpp ├── client2.cpp ├── client3.cpp ├── client4.cpp ├── client5.cpp ├── client6.cpp ├── ecourse_dp.hpp ├── server1.cpp ├── smartptr.h └── url ├── 4030 ├── facade1.cpp ├── facade2.cpp ├── facade3.cpp └── url ├── 4040 ├── Pimpl.cpp ├── Point1.cpp ├── Point1.h ├── Point2.cpp ├── Point2.h ├── PointImpl.cpp ├── PointImpl.h ├── bridge1.cpp ├── bridge2.cpp └── url ├── 5010 ├── observer1.cpp ├── observer2.cpp ├── observer3.cpp ├── observer4.cpp └── url ├── 5020 ├── Vector.h ├── VectorImpl.h ├── cont1.cpp ├── cont2.cpp ├── cont3.cpp ├── cont4.cpp └── url ├── 5030 ├── iterator1.cpp ├── iterator2.cpp ├── iterator3.cpp ├── iterator4.cpp └── url ├── 5040 ├── url ├── visitor1.cpp ├── visitor2.cpp ├── visitor3.cpp └── visitor4.cpp ├── 6010 ├── create1.cpp ├── create2.cpp ├── create3.cpp ├── create4.cpp └── url ├── 6020 ├── Singleton.h ├── singleton.hpp ├── singleton1.cpp ├── singleton2.cpp ├── singleton3.cpp ├── singleton4.cpp ├── singleton5.cpp ├── singleton6.cpp ├── singleton7.cpp └── url ├── 6030 ├── factory1.cpp ├── factory2.cpp ├── factory3.cpp ├── factory4.cpp ├── factory5.cpp ├── singleton.hpp └── url ├── 6040 ├── abfactory1.cpp ├── abfactory2.cpp └── url ├── 6050 ├── fbmethod1.cpp ├── fbmethod2.cpp └── url ├── 6060 ├── builder1.cpp ├── builder2.cpp ├── builder3.cpp └── url └── README.md /1010/protected.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Animal 3 | { 4 | protected: 5 | Animal() {} 6 | }; 7 | 8 | class Dog : public Animal 9 | { 10 | public: 11 | Dog() {} // Dog() : Animal() {} 12 | }; 13 | 14 | int main() 15 | { 16 | Animal a; // error 17 | Dog d; // ok. 18 | } 19 | -------------------------------------------------------------------------------- /1010/protected2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Car 5 | { 6 | public: 7 | Car() {} 8 | 9 | void Destroy() { delete this;} 10 | protected: 11 | ~Car(){ cout << "~Car" << endl;} 12 | }; 13 | 14 | int main() 15 | { 16 | //Car c; // 스택에 객체를 만들수 없다. 17 | 18 | Car* p = new Car; 19 | p->Destroy(); 20 | 21 | //delete p; 22 | } 23 | -------------------------------------------------------------------------------- /1010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /1020/upcasting1.cpp: -------------------------------------------------------------------------------- 1 | class Animal 2 | { 3 | int age; 4 | }; 5 | class Dog : public Animal 6 | { 7 | int color; 8 | }; 9 | 10 | int main() 11 | { 12 | Dog d; 13 | 14 | Dog* p1 = &d; // ok. 15 | 16 | double* p2 = &d; // error. 17 | 18 | Animal* p3 = &d; // ok. 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 | -------------------------------------------------------------------------------- /1020/upcasting2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal 5 | { 6 | int age; 7 | public: 8 | virtual void Cry() { cout << "Animal Cry" << endl;} 9 | }; 10 | 11 | class Dog : public Animal 12 | { 13 | int color; 14 | public: 15 | // override 16 | virtual void Cry() { cout << "Dog Cry" << endl;} 17 | }; 18 | 19 | int main() 20 | { 21 | Dog d; 22 | Animal* p = &d; 23 | 24 | p->Cry(); 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 | 64 | 65 | // 66 | -------------------------------------------------------------------------------- /1020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /1030/abstract.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Shape // 추상 클래스. 3 | { 4 | public: 5 | virtual void Draw() = 0; // 순수 가상함수. 6 | 7 | }; 8 | class Rect : public Shape 9 | { 10 | public: 11 | }; 12 | virtual void Draw() {} // 구현부를 제공하면 추상 아님. 13 | 14 | int main() 15 | { 16 | // Shape s; // error. 17 | Shape* p; // ok.. 18 | 19 | Rect r; // Draw()구현이 없으면 error 20 | } 21 | -------------------------------------------------------------------------------- /1030/coupling1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Camera 5 | { 6 | public: 7 | void take() { cout << "take picture" << endl;} 8 | }; 9 | 10 | class HDCamera 11 | { 12 | public: 13 | void take() { cout << "take HD picture" << endl;} 14 | }; 15 | 16 | class People 17 | { 18 | public: 19 | void useCamera(Camera* p) { p->take(); } 20 | void useCamera(HDCamera* p) { p->take(); } 21 | }; 22 | 23 | int main() 24 | { 25 | People p; 26 | Camera c; 27 | p.useCamera(&c); 28 | 29 | HDCamera hc; 30 | p.useCamera(&hc); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | // 53 | -------------------------------------------------------------------------------- /1030/coupling2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // 규칙 : 모든 카메라는 아래 클래스로부터 파생되어야 한다. 6 | // 7 | // 모든 카메라는 아래 인터페이스를 구현해야 한다. 8 | 9 | //#define interface struct 10 | 11 | struct ICamera 12 | { 13 | virtual void take() = 0; 14 | virtual ~ICamera() {} 15 | }; 16 | 17 | 18 | 19 | // 카메라가 없어도 카메라를 사용하는 코드를 만들수 있다. 20 | class People 21 | { 22 | public: 23 | void useCamera( ICamera* p ) { p->take(); } 24 | }; 25 | 26 | 27 | 28 | class Camera : public ICamera 29 | { 30 | public: 31 | void take() { cout << "take picture" << endl;} 32 | }; 33 | 34 | class HDCamera : public ICamera 35 | { 36 | public: 37 | void take() { cout << "take HD picture" << endl;} 38 | }; 39 | 40 | class UHDCamera : public ICamera 41 | { 42 | public: 43 | void take() { cout << "take UHD picture" << endl;} 44 | }; 45 | 46 | 47 | int main() 48 | { 49 | People p; 50 | Camera c; 51 | p.useCamera(&c); 52 | 53 | HDCamera hc; 54 | p.useCamera(&hc); 55 | 56 | UHDCamera uhc; 57 | p.useCamera(&uhc); 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | // 80 | -------------------------------------------------------------------------------- /1030/url: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /1040/shape1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Shape 6 | { 7 | public: 8 | virtual void Draw() { cout << "Draw Shape" << endl;} 9 | }; 10 | 11 | class Rect : public Shape 12 | { 13 | public: 14 | virtual void Draw() { cout << "Draw Rect" << endl;} 15 | }; 16 | 17 | class Circle : public Shape 18 | { 19 | public: 20 | virtual void Draw() { cout << "Draw Circle" << endl;} 21 | }; 22 | 23 | int main() 24 | { 25 | vector v; 26 | 27 | while( 1 ) 28 | { 29 | int cmd; 30 | cin >> cmd; 31 | 32 | if ( cmd == 1 ) v.push_back( new Rect ); 33 | else if ( cmd == 2 ) v.push_back( new Circle ); 34 | else if ( cmd == 9 ) 35 | { 36 | for ( auto p : v ) 37 | p->Draw(); 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 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | // 74 | -------------------------------------------------------------------------------- /1040/shape2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Shape 6 | { 7 | public: 8 | int type; 9 | virtual void Draw() { cout << "Draw Shape" << endl;} 10 | 11 | // 자신의 복사본을 만드는 가상함수. 12 | virtual Shape* Clone() { return new Shape(*this);} 13 | }; 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | class Rect : public Shape 22 | { 23 | public: 24 | Rect() { type = 1;} 25 | virtual void Draw() { cout << "Draw Rect" << endl;} 26 | 27 | virtual Shape* Clone() { return new Rect(*this);} 28 | }; 29 | 30 | class Circle: public Shape 31 | { 32 | public: 33 | Circle() { type = 2;} 34 | virtual void Draw() { cout << "Draw Circle" << endl;} 35 | 36 | virtual Shape* Clone() { return new Circle(*this);} 37 | }; 38 | 39 | 40 | 41 | class Triangle : public Shape 42 | { 43 | public: 44 | virtual void Draw() { cout << "Draw Triangle" << endl;} 45 | }; 46 | 47 | int main() 48 | { 49 | vector v; 50 | 51 | while(1) 52 | { 53 | int cmd; 54 | cin >> cmd; 55 | 56 | if ( cmd == 1 ) v.push_back(new Rect); 57 | else if ( cmd == 2 ) v.push_back(new Circle); 58 | 59 | 60 | 61 | else if ( cmd == 8 ) 62 | { 63 | cout << "index >> "; 64 | int k; 65 | cin >> k; 66 | 67 | // k 번째 도형의 복사본을 v에 추가한다. 68 | 69 | v.push_back( v[k]->Clone() ); // 다형성. 70 | 71 | /* 72 | // k 번째 도형의 복사본을 v에 추가한다. 73 | switch( v[k]->type ) 74 | { 75 | case 1: break; 76 | case 2: break; 77 | } 78 | */ 79 | 80 | } 81 | 82 | 83 | else if ( cmd == 9 ) 84 | { 85 | for ( auto p : v) 86 | p->Draw(); // 다형성 87 | } 88 | } 89 | 90 | } 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | // 124 | -------------------------------------------------------------------------------- /1040/shape3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Shape 6 | { 7 | protected: 8 | // 변하는 것을 가상함수로 뽑아낸다. 9 | virtual void DrawImp() 10 | { 11 | cout << "Draw Shape" << endl; 12 | } 13 | 14 | public: 15 | // final : 파생 클래스가 재정의 할수 없게 한다. 16 | virtual void Draw() final 17 | { 18 | cout << "mutex lock" << endl; 19 | 20 | DrawImp(); 21 | cout << "mutex unlock" << endl; 22 | } 23 | 24 | 25 | virtual Shape* Clone() { return new Shape(*this);} 26 | }; 27 | 28 | 29 | 30 | class Rect : public Shape 31 | { 32 | public: 33 | virtual void DrawImp() { cout << "Draw Rect" << endl;} 34 | 35 | 36 | 37 | virtual Shape* Clone() { return new Rect(*this);} 38 | }; 39 | 40 | class Circle: public Shape 41 | { 42 | public: 43 | virtual void DrawImp() { cout << "Draw Circle" << endl;} 44 | virtual Shape* Clone() { return new Circle(*this);} 45 | }; 46 | 47 | 48 | int main() 49 | { 50 | vector v; 51 | 52 | while(1) 53 | { 54 | int cmd; 55 | cin >> cmd; 56 | 57 | if ( cmd == 1 ) v.push_back(new Rect); 58 | else if ( cmd == 2 ) v.push_back(new Circle); 59 | else if ( cmd == 8 ) 60 | { 61 | cout << "index >> "; 62 | int k; 63 | cin >> k; 64 | v.push_back( v[k]->Clone() ); 65 | } 66 | else if ( cmd == 9 ) 67 | { 68 | for ( auto p : v) 69 | p->Draw(); 70 | } 71 | } 72 | 73 | } 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | // 107 | -------------------------------------------------------------------------------- /1040/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /2010/edit1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // getch() 4 | using namespace std; 5 | 6 | class Edit 7 | { 8 | string data; 9 | public: 10 | string getData() 11 | { 12 | data.clear(); 13 | 14 | while( 1 ) 15 | { 16 | char c = getch(); 17 | 18 | if ( c == 13 ) break; 19 | 20 | if ( isdigit(c) ) 21 | { 22 | data.push_back(c); 23 | cout << c; 24 | } 25 | } 26 | cout << endl; 27 | return data; 28 | } 29 | }; 30 | 31 | 32 | int main() 33 | { 34 | Edit edit; 35 | 36 | while(1) 37 | { 38 | string s = edit.getData(); 39 | cout << s << endl; 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | // 83 | -------------------------------------------------------------------------------- /2010/edit2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Edit 7 | { 8 | string data; 9 | public: 10 | 11 | virtual bool validate(char c) 12 | { 13 | return isdigit(c); 14 | } 15 | 16 | string getData() 17 | { 18 | data.clear(); 19 | 20 | while( 1 ) 21 | { 22 | char c = getch(); 23 | 24 | if ( c == 13 ) break; 25 | 26 | if ( validate(c) ) 27 | { 28 | data.push_back(c); 29 | cout << c; 30 | } 31 | } 32 | cout << endl; 33 | return data; 34 | } 35 | }; 36 | //-------------------- 37 | class AddressEdit : public Edit 38 | { 39 | public: 40 | virtual bool validate(char c) 41 | { 42 | return true; 43 | } 44 | }; 45 | 46 | int main() 47 | { 48 | AddressEdit edit; 49 | 50 | while(1) 51 | { 52 | string s = edit.getData(); 53 | cout << s << endl; 54 | } 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | // 97 | -------------------------------------------------------------------------------- /2010/edit3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | // validation 을 위한 인터페이스 8 | struct IValidator 9 | { 10 | virtual bool validate(string s, char c) = 0; 11 | virtual bool iscomplete(string s) { return true;} 12 | 13 | virtual ~IValidator() {} 14 | }; 15 | 16 | // 주민 번호 : 801 1 확인. 17 | 18 | 19 | 20 | class Edit 21 | { 22 | string data; 23 | //--------------------- 24 | IValidator* pVal = 0; 25 | public: 26 | void setValidator(IValidator* p ) { pVal = p;} 27 | //--------------------------- 28 | 29 | string getData() 30 | { 31 | data.clear(); 32 | 33 | while( 1 ) 34 | { 35 | char c = getch(); 36 | 37 | if ( c == 13 && 38 | ( pVal == 0 || pVal->iscomplete(data) ) ) break; 39 | 40 | if ( pVal == 0 || pVal->validate(data, c) ) 41 | { 42 | data.push_back(c); 43 | cout << c; 44 | } 45 | } 46 | cout << endl; 47 | return data; 48 | } 49 | }; 50 | 51 | class LimitDigitValidator : public IValidator 52 | { 53 | int value; 54 | public: 55 | LimitDigitValidator(int n) : value(n) {} 56 | 57 | virtual bool validate( string s, char c ) 58 | { 59 | return s.size() < value && isdigit(c); 60 | } 61 | 62 | virtual bool iscomplete( string s) 63 | { 64 | return s.size() == value; 65 | } 66 | }; 67 | 68 | 69 | int main() 70 | { 71 | Edit edit; 72 | LimitDigitValidator v(5); 73 | edit.setValidator(&v); 74 | 75 | while(1) 76 | { 77 | string s = edit.getData(); 78 | cout << s << endl; 79 | } 80 | } 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | // 122 | -------------------------------------------------------------------------------- /2010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /2020/app.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class CWinApp; // 클래스 전방 선언. 6 | 7 | CWinApp* g_app = 0; 8 | 9 | class CWinApp 10 | { 11 | public: 12 | CWinApp() { g_app = this;} 13 | virtual bool InitInstance() { return false; } 14 | virtual int ExitInstance() { return false; } 15 | virtual int Run() { return 0; } 16 | }; 17 | 18 | int main() 19 | { 20 | if ( g_app->InitInstance() == true) 21 | g_app->Run(); 22 | g_app->ExitInstance(); 23 | } 24 | 25 | //----------------------- 26 | // 라이브러리 사용자 27 | // 1. CWinApp 의 파생 클래스 만들어야 한다. 28 | // 2. 사용자 클래스를 전역객체 생성 29 | 30 | class MyApp : public CWinApp 31 | { 32 | public: 33 | virtual bool InitInstance() 34 | { 35 | cout << "initialization" << endl; 36 | return true; 37 | } 38 | 39 | virtual int ExitInstance() 40 | { 41 | cout << "finish" << endl; 42 | return 0; 43 | } 44 | }; 45 | MyApp theApp; 46 | 47 | // 1. 전역변수 생성자. 기반 클래스 생성자. 48 | // 2. main 함수 실행. 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | // 82 | -------------------------------------------------------------------------------- /2020/func1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // qsort() 6 | void Sort(int* x, int sz, bool(*cmp)(int, int)) 7 | { 8 | for (int i = 0; i < sz-1; i++) 9 | { 10 | for( int j = i + 1; j < sz; j++) 11 | { 12 | //if ( x[i] < x[j] ) 13 | 14 | if ( cmp( x[i], x[j]) ) 15 | swap( x[i], x[j] ); 16 | } 17 | } 18 | } 19 | 20 | bool cmp1( int a, int b) { return a < b;} 21 | bool cmp2( int a, int b) { return a > b;} 22 | int main() 23 | { 24 | int x[10] = { 1,3,5,7,9,2,4,6,8,10}; 25 | 26 | Sort( x, 10, &cmp2); 27 | 28 | for ( auto n : x) 29 | cout << n << ", "; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | // 45 | -------------------------------------------------------------------------------- /2020/func2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //void Sort(int* x, int sz, bool(*cmp)(int, int)) 6 | 7 | template 8 | void Sort(int* x, int sz, T cmp) 9 | { 10 | for (int i = 0; i < sz-1; i++) 11 | { 12 | for( int j = i + 1; j < sz; j++) 13 | { 14 | if ( cmp( x[i], x[j]) ) 15 | swap( x[i], x[j] ); 16 | } 17 | } 18 | } 19 | 20 | bool cmp1( int a, int b) { return a < b;} 21 | bool cmp2( int a, int b) { return a > b;} 22 | 23 | int main() 24 | { 25 | int x[10] = { 1,3,5,7,9,2,4,6,8,10}; 26 | 27 | //Sort( x, 10, &cmp2); 28 | 29 | Sort( x, 10, [](int a, int b) { return a > b;}); 30 | 31 | for ( auto n : x) 32 | cout << n << ", "; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | // 48 | -------------------------------------------------------------------------------- /2020/policy1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template class List 5 | { 6 | public: 7 | void push_front(const T& a) 8 | { 9 | // Lock(); 10 | //... 11 | // Unlock(); 12 | } 13 | }; 14 | 15 | List s; // 멀티 스레드에 안전하지 않다. 16 | 17 | int main() 18 | { 19 | 20 | s.push_front(10); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /2020/policy2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ISync 5 | { 6 | virtual void Lock() = 0; 7 | virtual void UnLock() = 0; 8 | virtual ~ISync() {} 9 | }; 10 | 11 | template class List 12 | { 13 | ISync* pSync = 0; 14 | public: 15 | void setSync(ISync* p) { pSync = p;} 16 | 17 | void push_front(const T& a) 18 | { 19 | if ( pSync != 0 ) pSync->Lock(); 20 | //... 21 | if ( pSync != 0 ) pSync->UnLock(); 22 | } 23 | }; 24 | 25 | class MutexLock : public ISync 26 | { 27 | // mutex m; 28 | public: 29 | virtual void Lock() {} 30 | virtual void UnLock() {} 31 | }; 32 | 33 | List s; 34 | 35 | int main() 36 | { 37 | MutexLock m; 38 | s.setSync(&m); 39 | 40 | s.push_front(10); 41 | 42 | 43 | NoLock m2; 44 | s.setSync(&m2); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /2020/policy3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | template class List 6 | { 7 | ThreadModel tm; // 동기화 정책을 담은 클래스 8 | public: 9 | void push_front(const T& a) 10 | { 11 | tm.Lock(); 12 | //... 13 | tm.Unlock(); 14 | } 15 | }; 16 | 17 | class MutexLock 18 | { 19 | // mutex m; 20 | public: 21 | inline void Lock() { cout << "mutex lock" << endl;} 22 | inline void Unlock() { cout << "mutex Unlock" << endl;} 23 | }; 24 | class NoLock 25 | { 26 | public: 27 | inline void Lock() {} 28 | inline void Unlock() {} 29 | }; 30 | //List s; 31 | List s; 32 | 33 | int main() 34 | { 35 | 36 | s.push_front(10); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /2020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /2030/state1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Character 5 | { 6 | int gold = 0; 7 | int item = 0; 8 | public: 9 | void run() { cout << "run" << endl; } 10 | void attack() { cout << "attack" << endl; } 11 | }; 12 | 13 | int main() 14 | { 15 | Character* p = new Character; 16 | p->run(); 17 | p->attack(); 18 | } 19 | -------------------------------------------------------------------------------- /2030/state2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Character 5 | { 6 | int gold = 0; 7 | int item = 0; 8 | public: 9 | void run() 10 | { 11 | if ( item == 1) 12 | cout << "run" << endl; 13 | else if ( item == 2 ) 14 | cout << "fast run" << endl; 15 | } 16 | void attack() 17 | { 18 | if ( item == 1) 19 | cout << "attack" << endl; 20 | else if ( item == 2 ) 21 | cout << "attack2" << endl; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | Character* p = new Character; 28 | p->run(); 29 | p->attack(); 30 | } 31 | -------------------------------------------------------------------------------- /2030/state3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Character 5 | { 6 | int gold = 0; 7 | int item = 0; 8 | public: 9 | void run() { runImp(); } 10 | void attack() { attackImp(); } 11 | 12 | virtual void runImp() {} 13 | virtual void attackImp() {} 14 | }; 15 | 16 | class PowerItemCharacter : public Character 17 | { 18 | public: 19 | virtual void runImp() { cout << "fast run" << endl;} 20 | virtual void attackImp() { cout << "attack" << endl;} 21 | }; 22 | 23 | class NormalCharacter : public Character 24 | { 25 | public: 26 | virtual void runImp() { cout << "run" << endl;} 27 | virtual void attackImp() { cout << "power attack" << endl;} 28 | }; 29 | 30 | 31 | int main() 32 | { 33 | Character* p = new NormalCharacter; 34 | p->run(); 35 | p->attack(); 36 | 37 | p = new PowerItemCharacter; 38 | p->run(); 39 | p->attack(); 40 | 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | // 52 | -------------------------------------------------------------------------------- /2030/state4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct IState 5 | { 6 | virtual void run() = 0; 7 | virtual void attack() = 0; 8 | virtual ~IState() {} 9 | }; 10 | 11 | class Character 12 | { 13 | int gold = 0; 14 | int item = 0; 15 | IState* state; 16 | public: 17 | void changeState(IState* p) { state = p;} 18 | void run() { state->run(); } 19 | void attack() { state->attack(); } 20 | }; 21 | class NormalState : public IState 22 | { 23 | virtual void run() { cout << "run" << endl;} 24 | virtual void attack(){ cout << "attack" << endl;} 25 | }; 26 | class PowerItemState : public IState 27 | { 28 | virtual void run() { cout << "fast run" << endl;} 29 | virtual void attack(){ cout << "power attack" << endl;} 30 | }; 31 | int main() 32 | { 33 | NormalState ns; 34 | PowerItemState ps; 35 | 36 | Character* p = new Character; 37 | p->changeState(&ns); 38 | p->run(); 39 | p->attack(); 40 | 41 | p->changeState(&ps); // 아이템 획득. 42 | p->run(); 43 | p->attack(); 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | // 53 | -------------------------------------------------------------------------------- /2030/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /3010/menu1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | class BaseMenu 8 | { 9 | string title; 10 | public: 11 | BaseMenu( string s) : title(s) {} 12 | 13 | string getTitle() const { return title;} 14 | 15 | virtual void command() = 0; // !! 16 | }; 17 | 18 | class MenuItem : public BaseMenu 19 | { 20 | int id; 21 | public: 22 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 23 | 24 | virtual void command() 25 | { 26 | cout << getTitle() << endl; 27 | getchar(); 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | MenuItem m("sound", 11); // ok.. 34 | 35 | } 36 | 37 | 38 | 39 | 40 | // 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | // 51 | -------------------------------------------------------------------------------- /3010/menu2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class BaseMenu 7 | { 8 | string title; 9 | public: 10 | BaseMenu( string s) : title(s) {} 11 | string getTitle() const { return title;} 12 | 13 | virtual void command() = 0; 14 | }; 15 | 16 | class MenuItem : public BaseMenu 17 | { 18 | int id; 19 | public: 20 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 21 | 22 | virtual void command() 23 | { 24 | cout << getTitle() << endl; 25 | getchar(); 26 | } 27 | }; 28 | 29 | 30 | class PopupMenu : public BaseMenu 31 | { 32 | vector v; 33 | public: 34 | PopupMenu( string s) : BaseMenu(s) {} 35 | 36 | void addMenu(BaseMenu* p) { v.push_back(p);} 37 | 38 | virtual void command() 39 | { 40 | while( 1 ) 41 | { 42 | system("cls"); 43 | 44 | int sz = v.size(); 45 | 46 | for ( int i = 0; i < sz; i++) 47 | { 48 | cout << i + 1 << ". " << v[i]->getTitle() << endl; 49 | } 50 | 51 | cout << sz + 1 << ". 상위 메뉴로 " << endl; 52 | 53 | //------------------------------ 54 | int cmd; 55 | cout << "메뉴를 선택하세요 >> "; 56 | cin >> cmd; 57 | 58 | if ( cmd < 1 || cmd > sz + 1 ) // 잘못된 입력 59 | continue; 60 | 61 | if ( cmd == sz + 1 ) 62 | break; 63 | 64 | 65 | // 선택된 메뉴 실행.. 66 | v[cmd-1]-> command(); // 핵심.. ! 67 | } 68 | 69 | } 70 | }; 71 | 72 | 73 | 74 | 75 | int main() 76 | { 77 | PopupMenu pm("화면설정"); 78 | } 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | // 94 | -------------------------------------------------------------------------------- /3010/menu3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/DP/e934c1f14b30c4cdc7836a091deac37fedae9b3a/3010/menu3.cpp -------------------------------------------------------------------------------- /3010/menu4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class BaseMenu 7 | { 8 | string title; 9 | public: 10 | BaseMenu( string s) : title(s) {} 11 | string getTitle() const { return title;} 12 | 13 | virtual BaseMenu* getSubMenu(int idx) 14 | { 15 | //throw "unsupported function.." 16 | return 0; 17 | } 18 | void addMenu(BaseMenu* p) 19 | { 20 | throw "unsupported function.."; 21 | } 22 | 23 | virtual void command() = 0; 24 | }; 25 | 26 | 27 | 28 | class MenuItem : public BaseMenu 29 | { 30 | int id; 31 | public: 32 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 33 | 34 | virtual void command() 35 | { 36 | cout << getTitle() << endl; 37 | getchar(); 38 | } 39 | }; 40 | 41 | 42 | class PopupMenu : public BaseMenu 43 | { 44 | vector v; 45 | public: 46 | PopupMenu( string s) : BaseMenu(s) {} 47 | 48 | void addMenu(BaseMenu* p) { v.push_back(p);} 49 | 50 | virtual void command() 51 | { 52 | while( 1 ) 53 | { 54 | system("cls"); 55 | 56 | int sz = v.size(); 57 | 58 | for ( int i = 0; i < sz; i++) 59 | { 60 | cout << i + 1 << ". " << v[i]->getTitle() << endl; 61 | } 62 | 63 | cout << sz + 1 << ". 상위 메뉴로" << endl; 64 | 65 | //------------------------------ 66 | int cmd; 67 | cout << "메뉴를 선택하세요 >> "; 68 | cin >> cmd; 69 | 70 | if ( cmd < 1 || cmd > sz + 1 ) 71 | continue; 72 | 73 | if ( cmd == sz + 1 ) 74 | break; 75 | 76 | 77 | 78 | v[cmd-1]-> command(); 79 | } 80 | 81 | } 82 | BaseMenu* getSubMenu(int idx) 83 | { 84 | return v[idx]; 85 | } 86 | }; 87 | 88 | int main() 89 | { 90 | PopupMenu* menubar = new PopupMenu("mebuBar"); 91 | PopupMenu* pm1 = new PopupMenu("화면설정"); 92 | PopupMenu* pm2 = new PopupMenu("소리설정"); 93 | MenuItem* m1 = new MenuItem("정보 확인", 11); 94 | menubar->addMenu( pm1 ); 95 | menubar->addMenu( pm2 ); 96 | menubar->addMenu( m1 ); 97 | pm1->addMenu( new MenuItem("해상도변경", 21)); 98 | pm1->addMenu( new MenuItem("명암 변경", 22)); 99 | pm2->addMenu( new MenuItem("음량 조절", 31)); 100 | 101 | BaseMenu* p = menubar->getSubMenu(1)->getSubMenu(0); 102 | 103 | menubar->getSubMenu(1)->addMenu( new MenuItem("AA", 100)); 104 | 105 | 106 | // 시작하려면 107 | menubar->command(); 108 | 109 | } 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | // 125 | -------------------------------------------------------------------------------- /3010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /3020/menu.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class BaseMenu 7 | { 8 | string title; 9 | public: 10 | BaseMenu( string s) : title(s) {} 11 | string getTitle() const { return title;} 12 | 13 | virtual void command() = 0; 14 | }; 15 | 16 | class PopupMenu : public BaseMenu 17 | { 18 | vector v; 19 | public: 20 | PopupMenu( string s) : BaseMenu(s) {} 21 | 22 | void addMenu(BaseMenu* p) { v.push_back(p);} 23 | 24 | virtual void command() 25 | { 26 | while( 1 ) 27 | { 28 | system("cls"); 29 | 30 | int sz = v.size(); 31 | 32 | for ( int i = 0; i < sz; i++) 33 | { 34 | cout << i + 1 << ". " << v[i]->getTitle() << endl; 35 | } 36 | 37 | cout << sz + 1 << ". 상위 메뉴로" << endl; 38 | 39 | //------------------------------ 40 | int cmd; 41 | cout << "메뉴를 선택하세요 >> "; 42 | cin >> cmd; 43 | 44 | if ( cmd < 1 || cmd > sz + 1 ) 45 | continue; 46 | 47 | if ( cmd == sz + 1 ) 48 | break; 49 | 50 | 51 | 52 | v[cmd-1]-> command(); 53 | } 54 | 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /3020/menu_event1.cpp: -------------------------------------------------------------------------------- 1 | #include "menu.hpp" 2 | 3 | class MenuItem : public BaseMenu 4 | { 5 | int id; 6 | public: 7 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 8 | 9 | virtual void command() 10 | { 11 | // 변해야 하는 것을 가상함수로. 12 | doCommand(); 13 | } 14 | virtual void doCommand() {} 15 | }; 16 | 17 | class AddStudentMenu : public MenuItem 18 | { 19 | public: 20 | using MenuItem::MenuItem; // 생성자 상속.. 21 | 22 | virtual void doCommand() { cout << "Add Student" << endl; } 23 | }; 24 | 25 | class RemoveStudentMenu : public MenuItem 26 | { 27 | public: 28 | using MenuItem::MenuItem; 29 | virtual void doCommand() { cout << "Remove Student" << endl; } 30 | }; 31 | 32 | int main() 33 | { 34 | AddStudentMenu m1( "Add Student " , 11); 35 | RemoveStudentMenu m2( "Remove Student " , 12); 36 | 37 | m1.command(); 38 | m2.command(); 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | // 55 | -------------------------------------------------------------------------------- /3020/menu_event2.cpp: -------------------------------------------------------------------------------- 1 | #include "menu.hpp" 2 | 3 | // 메뉴 메세지를 처리하려면 아래 인터페이스를 정의 해야 한다. 4 | struct IMenuListener 5 | { 6 | virtual void doCommand( int id) = 0; 7 | virtual ~IMenuListener() {} 8 | }; 9 | 10 | class MenuItem : public BaseMenu 11 | { 12 | int id; 13 | IMenuListener* pListener = 0; 14 | 15 | public: 16 | void setListener(IMenuListener* p) { pListener = p;} 17 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 18 | 19 | virtual void command() 20 | { 21 | // 변하는 것을 다른 클래스로..! 22 | if ( pListener != 0 ) 23 | pListener->doCommand( id ); 24 | } 25 | }; 26 | 27 | class Dialog : public IMenuListener 28 | { 29 | public: 30 | virtual void doCommand(int id) 31 | { 32 | //cout << "Dialog doCommand" << endl; 33 | switch( id ) 34 | { 35 | case 11: cout << "11" << endl; break; 36 | case 12: cout << "12" << endl; break; 37 | } 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | Dialog dlg; 44 | MenuItem m1( "Add Student " , 11); 45 | MenuItem m2( "Remove Student " , 12); 46 | 47 | m1.setListener(&dlg); 48 | m2.setListener(&dlg); 49 | 50 | 51 | m1.command(); 52 | m2.command(); 53 | } 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | // 69 | -------------------------------------------------------------------------------- /3020/menu_event3.cpp: -------------------------------------------------------------------------------- 1 | #include "menu.hpp" 2 | 3 | 4 | class MenuItem : public BaseMenu 5 | { 6 | int id; 7 | 8 | void (Dialog::*handler)(); 9 | Dialog* target; 10 | 11 | public: 12 | void setHandler( void(*f)() ) { handler = f;} 13 | 14 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 15 | 16 | virtual void command() 17 | { 18 | handler(); 19 | } 20 | }; 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | class Dialog : public IMenuListener 29 | { 30 | public: 31 | virtual void doCommand(int id) 32 | { 33 | //cout << "Dialog doCommand" << endl; 34 | switch( id ) 35 | { 36 | case 11: cout << "11" << endl; break; 37 | case 12: cout << "12" << endl; break; 38 | } 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | Dialog dlg; 45 | MenuItem m1( "Add Student " , 11); 46 | MenuItem m2( "Remove Student " , 12); 47 | 48 | m1.setListener(&dlg); 49 | m2.setListener(&dlg); 50 | 51 | 52 | m1.command(); 53 | m2.command(); 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | // 70 | -------------------------------------------------------------------------------- /3020/menu_event4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void foo() { cout << "foo" << endl;} 5 | 6 | class Dialog 7 | { 8 | public: 9 | void Close() { cout << "Dialog Close" << endl;} 10 | }; 11 | 12 | 13 | int main() 14 | { 15 | void(*f1)() = &foo; 16 | void(Dialog::*f2)() = &Dialog::Close; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | // 27 | -------------------------------------------------------------------------------- /3020/menu_event5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void foo() { cout << "foo" << endl;} 5 | 6 | class Dialog 7 | { 8 | public: 9 | void Close() { cout << "Dialog Close" << endl;} 10 | }; 11 | 12 | //----------------------- 13 | 14 | struct IAction 15 | { 16 | virtual void Execute() = 0; 17 | virtual ~IAction() {} 18 | }; 19 | 20 | class FunctionAction : public IAction 21 | { 22 | typedef void(*FP)(); 23 | 24 | FP handler; 25 | public: 26 | FunctionAction(FP f) : handler(f) {} 27 | 28 | virtual void Execute() {handler();} 29 | }; 30 | 31 | template 32 | class MemberAction : public IAction 33 | { 34 | typedef void(T::*FP)(); 35 | 36 | FP handler; 37 | T* target; 38 | public: 39 | MemberAction(FP f, T* obj) : handler(f), target(obj) {} 40 | 41 | virtual void Execute() { (target->*handler)();} 42 | }; 43 | 44 | int main() 45 | { 46 | Dialog dlg; 47 | IAction* p1 = new FunctionAction(&foo); 48 | IAction* p2 = new MemberAction(&Dialog::Close, &dlg); 49 | 50 | p1->Execute(); // foo 실행 51 | p2->Execute(); // Dialog::Close 52 | 53 | 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | // 69 | -------------------------------------------------------------------------------- /3020/menu_event6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void foo() { cout << "foo" << endl;} 5 | 6 | class Dialog 7 | { 8 | public: 9 | void Close() { cout << "Dialog Close" << endl;} 10 | }; 11 | 12 | //----------------------- 13 | 14 | struct IAction 15 | { 16 | virtual void Execute() = 0; 17 | virtual ~IAction() {} 18 | }; 19 | 20 | class FunctionAction : public IAction 21 | { 22 | typedef void(*FP)(); 23 | 24 | FP handler; 25 | public: 26 | FunctionAction(FP f) : handler(f) {} 27 | 28 | virtual void Execute() {handler();} 29 | }; 30 | 31 | template 32 | class MemberAction : public IAction 33 | { 34 | typedef void(T::*FP)(); 35 | 36 | FP handler; 37 | T* target; 38 | public: 39 | MemberAction(FP f, T* obj) : handler(f), target(obj) {} 40 | 41 | virtual void Execute() { (target->*handler)();} 42 | }; 43 | 44 | // 함수 템플릿 45 | template 46 | MemberAction* action( void(T::*f)(), T* obj) 47 | { 48 | return new MemberAction( f, obj); 49 | } 50 | 51 | FunctionAction* action( void(*f)() ) 52 | { 53 | return new FunctionAction( f); 54 | } 55 | 56 | 57 | int main() 58 | { 59 | Dialog dlg; 60 | //IAction* p1 = new FunctionAction(&foo); 61 | //IAction* p2 = new MemberAction(&Dialog::Close, &dlg); 62 | 63 | IAction* p1 = action(&foo); 64 | IAction* p2 = action(&Dialog::Close, &dlg); 65 | 66 | p1->Execute(); 67 | p2->Execute(); 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | /* 84 | //--------------------------------------------- 85 | template void square( T a) { return a * a; } 86 | 87 | square(3); 88 | square(3); 89 | 90 | 91 | list s(10,3); 92 | 93 | */ 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | // 104 | -------------------------------------------------------------------------------- /3020/menu_event7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void foo() { cout << "foo" << endl;} 6 | void goo(int n) { cout << "goo : " << n << endl;} 7 | 8 | class Dialog 9 | { 10 | public: 11 | void Close() { cout << "Dialog Close" << endl;} 12 | }; 13 | 14 | int main() 15 | { 16 | function f; 17 | f = &foo; 18 | f(); // foo 호출 19 | 20 | Dialog dlg; 21 | 22 | f = bind(&Dialog::Close, &dlg); // action(&Dialog::Close, &dlg) 23 | f(); // dlg.Close() 24 | 25 | f = bind(&goo, 5); 26 | f(); // goo(5) 27 | 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | // 42 | -------------------------------------------------------------------------------- /3020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /3030/decorator1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class SpaceCraft 5 | { 6 | int color; 7 | int speed; 8 | public: 9 | void Fire() { cout << "Space Craft : ----------" << endl;} 10 | }; 11 | 12 | int main() 13 | { 14 | SpaceCraft sc; 15 | sc.Fire(); 16 | } 17 | -------------------------------------------------------------------------------- /3030/decorator2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class SpaceCraft 5 | { 6 | int color; 7 | int speed; 8 | public: 9 | void Fire() { cout << "Space Craft : ----------" << endl;} 10 | }; 11 | 12 | // 상속을 통한 기능의 추가. 13 | class LeftMissile : public SpaceCraft 14 | { 15 | public: 16 | void Fire() 17 | { 18 | SpaceCraft::Fire(); // 기존 기능 수행. 19 | cout << "Left Missile : >>>>>>>>" << endl; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | SpaceCraft sc; 26 | sc.Fire(); 27 | 28 | LeftMissile lm; 29 | lm.Fire(); 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | // 39 | -------------------------------------------------------------------------------- /3030/decorator3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class SpaceCraft 5 | { 6 | int color; 7 | int speed; 8 | public: 9 | void Fire() { cout << "Space Craft : ----------" << endl;} 10 | }; 11 | 12 | // 구성(Composition) 통한 기능의 추가. 13 | class LeftMissile 14 | { 15 | SpaceCraft* craft; 16 | public: 17 | LeftMissile( SpaceCraft* p) : craft(p) {} 18 | 19 | void Fire() 20 | { 21 | craft->Fire(); // 기존 기능 수행. 22 | cout << "Left Missile : >>>>>>>>" << endl; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | SpaceCraft sc; 29 | sc.Fire(); 30 | 31 | LeftMissile lm(&sc); 32 | lm.Fire(); 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | // 42 | -------------------------------------------------------------------------------- /3030/decorator4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 우주선과 기능추가객체의 공통의 기반 클래스 5 | struct Component 6 | { 7 | virtual void Fire() = 0; 8 | virtual ~Component() {} 9 | }; 10 | 11 | 12 | class SpaceCraft : public Component 13 | { 14 | int color; 15 | int speed; 16 | public: 17 | void Fire() { cout << "Space Craft : ----------" << endl;} 18 | }; 19 | 20 | class LeftMissile : public Component 21 | { 22 | Component* craft; 23 | public: 24 | LeftMissile( Component* p) : craft(p) {} 25 | 26 | void Fire() 27 | { 28 | craft->Fire(); 29 | cout << "Left Missile : >>>>>>>>" << endl; 30 | } 31 | }; 32 | 33 | class RightMissile : public Component 34 | { 35 | Component* craft; 36 | public: 37 | RightMissile( Component* p) : craft(p) {} 38 | 39 | void Fire() 40 | { 41 | craft->Fire(); 42 | cout << "Right Missile : >>>>>>>>" << endl; 43 | } 44 | }; 45 | 46 | int main() 47 | { 48 | SpaceCraft sc; 49 | // sc.Fire(); 50 | 51 | LeftMissile lm(&sc); 52 | // lm.Fire(); 53 | 54 | //RightMissile rm(&sc); 55 | RightMissile rm(&lm); // 기능추가된 객체에 다시 기능 추가 56 | rm.Fire(); 57 | 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | // 67 | -------------------------------------------------------------------------------- /3030/decorator5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 우주선과 기능추가객체의 공통의 기반 클래스 5 | struct Component 6 | { 7 | virtual void Fire() = 0; 8 | virtual ~Component() {} 9 | }; 10 | 11 | 12 | class SpaceCraft : public Component 13 | { 14 | int color; 15 | int speed; 16 | public: 17 | void Fire() { cout << "Space Craft : ----------" << endl;} 18 | }; 19 | 20 | //--------------- 21 | // 기능 추가 클래스의 공통의 기반 클래스 22 | class IDecorator : public Component 23 | { 24 | Component* craft; 25 | public: 26 | IDecorator( Component* p) : craft(p) {} 27 | 28 | void Fire() { craft->Fire();} 29 | }; 30 | 31 | 32 | class LeftMissile : public IDecorator 33 | { 34 | public: 35 | LeftMissile( Component* p) : IDecorator(p) {} 36 | 37 | void Fire() 38 | { 39 | IDecorator::Fire(); 40 | cout << "Left Missile : >>>>>>>>" << endl; 41 | } 42 | }; 43 | 44 | class RightMissile : public IDecorator 45 | { 46 | public: 47 | RightMissile( Component* p) : IDecorator(p) {} 48 | 49 | void Fire() 50 | { 51 | IDecorator::Fire(); 52 | cout << "Rigth Missile : >>>>>>>>" << endl; 53 | } 54 | }; 55 | 56 | 57 | int main() 58 | { 59 | SpaceCraft sc; 60 | 61 | LeftMissile lm(&sc); 62 | RightMissile rm(&lm); 63 | 64 | rm.Fire(); 65 | } 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | // 74 | -------------------------------------------------------------------------------- /3030/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /4010/TextView.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class TextView 5 | { 6 | std::string data; 7 | std::string font; 8 | int width; 9 | public: 10 | TextView( std::string s, std::string fo ="나눔고딕", int w=24 ) 11 | : data(s), font(fo), width(w) {} 12 | 13 | void Show() { std::cout << data << std::endl;} 14 | }; 15 | 16 | // TextView tv("hello"); 17 | // tv.Show(); 18 | -------------------------------------------------------------------------------- /4010/adapter1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "TextView.h" 4 | using namespace std; 5 | 6 | 7 | 8 | class Shape 9 | { 10 | public: 11 | virtual void Draw() { cout << "Draw Shape" << endl;} 12 | }; 13 | 14 | // TextView 를 도형편집기에서 사용하기 위해 15 | // 인터페이스 변경(함수 이름 변경.) 16 | class Text : public TextView, public Shape 17 | { 18 | public: 19 | Text( string s) : TextView(s) {} 20 | 21 | virtual void Draw() { TextView::Show(); } 22 | }; 23 | 24 | 25 | 26 | 27 | class Rect : public Shape 28 | { 29 | public: 30 | virtual void Draw() { cout << "Draw Rect" << endl;} 31 | }; 32 | 33 | class Circle : public Shape 34 | { 35 | public: 36 | virtual void Draw() { cout << "Circle Rect" << endl;} 37 | }; 38 | 39 | int main() 40 | { 41 | vector v; 42 | v.push_back(new Rect); 43 | v.push_back(new Circle); 44 | v.push_back(new Text("hello") ); 45 | 46 | for ( auto p : v) 47 | p->Draw(); 48 | 49 | } 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | // 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | // 92 | -------------------------------------------------------------------------------- /4010/adapter2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "TextView.h" 4 | using namespace std; 5 | 6 | 7 | class Shape 8 | { 9 | public: 10 | virtual void Draw() { cout << "Draw Shape" << endl;} 11 | }; 12 | 13 | // TextView 클래스의 인터페이스를 도형편집기에 맞도록 수정 14 | 15 | class Text : public TextView, public Shape 16 | { 17 | public: 18 | Text( string s) : TextView(s) {} 19 | 20 | virtual void Draw() { TextView::Show(); } 21 | }; 22 | 23 | /* 24 | class Text : public Shape 25 | { 26 | TextView tv; 27 | public: 28 | Text( string s) : tv(s) {} 29 | 30 | virtual void Draw() { tv.Show(); } 31 | }; 32 | */ 33 | 34 | // 객체 어답터. 35 | class ObjectAdapter : public Shape 36 | { 37 | TextView* pView; // 포인터가 핵심 38 | public: 39 | ObjectAdapter( TextView* p) : pView(p) {} 40 | 41 | virtual void Draw() { pView->Show(); } 42 | }; 43 | 44 | int main() 45 | { 46 | vector v; 47 | 48 | TextView tv("world"); // 이미 존재 하던 객체.. 49 | //v.push_back(&tv); // error. 50 | 51 | v.push_back( new ObjectAdapter(&tv)); 52 | 53 | v.push_back( new Text("hello")); 54 | 55 | for ( auto p : v) 56 | p->Draw(); 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | // 99 | -------------------------------------------------------------------------------- /4010/adapter3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // stack 을 만들어 봅시다. 8 | // list의 함수이름을 stack 처럼 보이도록 변경 9 | /* 10 | template class Stack : private list 11 | { 12 | public: 13 | void push(const T& a) { list::push_back(a);} 14 | void pop() { list::pop_back();} 15 | T& top() { return list::back();} 16 | }; 17 | */ 18 | /* 19 | template > class Stack 20 | { 21 | C st; 22 | public: 23 | inline void push(const T& a) { st.push_back(a);} 24 | void pop() { st.pop_back();} 25 | T& top() { return st.back();} 26 | }; 27 | */ 28 | #include 29 | 30 | int main() 31 | { 32 | //Stack > s; 33 | //Stack > s; 34 | stack s; 35 | s.push(10); 36 | s.push(20); 37 | 38 | // s.push_front(20); 39 | 40 | cout << s.top() << endl; // 20 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | // 52 | -------------------------------------------------------------------------------- /4010/adapter4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | list s = { 1,2,3,4}; 9 | 10 | auto p1 = s.begin(); 11 | auto p2 = s.end(); 12 | 13 | //reverse_iterator< list::iterator > p3(p2); // --p2 로 초기화 14 | reverse_iterator< list::iterator > p4(p1); // --p1 로 초기화 15 | 16 | auto p3 = make_reverse_iterator(p2); 17 | 18 | //cout << *p3 << endl; // 4 19 | //++p3; // -- 20 | //cout << *p3 << endl; // 3 21 | 22 | //for_each( p1, p2, [](int a) { cout << a << endl;}); 23 | for_each( p3, p4, [](int a) { cout << a << endl;}); 24 | 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | // 43 | -------------------------------------------------------------------------------- /4010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /4020/CalcProxy.cpp: -------------------------------------------------------------------------------- 1 | #include "ecourse_dp.hpp" 2 | #include "ICalc.h" 3 | using namespace ecourse; 4 | 5 | class Calc : public ICalc 6 | { 7 | int server; 8 | int count = 0; 9 | public: 10 | Calc() { server = ec_find_server("CalcService"); } 11 | ~Calc() { cout << "~Calc" << endl; } 12 | 13 | void AddRef() { ++count;} 14 | void Release() { if ( --count == 0 ) delete this; } 15 | 16 | int Add(int a, int b) { return ec_send_server(server, 1, a, b);} 17 | int Sub(int a, int b) { return ec_send_server(server, 2, a, b);} 18 | }; 19 | 20 | extern "C" __declspec(dllexport) 21 | ICalc* CreateCalc() 22 | { 23 | return new Calc; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | // 32 | -------------------------------------------------------------------------------- /4020/ICalc.h: -------------------------------------------------------------------------------- 1 | // ICalc.h 2 | 3 | // 참조계수를 책임지는 함수는 인터페이스에 있어야 한다. 4 | 5 | struct IRefCount // IUnknown 6 | { 7 | virtual void AddRef() = 0; 8 | virtual void Release() = 0; 9 | virtual ~IRefCount() {} 10 | }; 11 | 12 | struct ICalc : public IRefCount 13 | { 14 | virtual int Add(int a, int b) = 0; 15 | virtual int Sub(int a, int b) = 0; 16 | 17 | virtual ~ICalc() {} 18 | }; 19 | -------------------------------------------------------------------------------- /4020/Proxy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Image 7 | { 8 | public: 9 | Image(string imageName) 10 | { 11 | //this_thread::sleep(1000s); 12 | } 13 | }; 14 | 15 | class ImageProxy 16 | { 17 | Image* pImage; 18 | public: 19 | ImageProxy(string imageName) 20 | { 21 | cout << "Load..." << endl; 22 | pImage = new Image; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | ImageProxy* p = new ImageProxy("aaa.com\\a.png"); 29 | } 30 | -------------------------------------------------------------------------------- /4020/client1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | using namespace std; 4 | using namespace ecourse; 5 | 6 | int main() 7 | { 8 | int server = ec_find_server("CalcService"); 9 | 10 | cout << "server : " << server << endl; 11 | 12 | int ret = ec_send_server(server, 1, 10, 20); 13 | 14 | cout << ret << endl; // 30 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /4020/client2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | using namespace std; 4 | using namespace ecourse; 5 | 6 | // Proxy.. 7 | class Calc 8 | { 9 | int server; 10 | public: 11 | Calc() { server = ec_find_server("CalcService"); } 12 | 13 | int Add(int a, int b) { return ec_send_server(server, 1, a, b);} 14 | int Sub(int a, int b) { return ec_send_server(server, 2, a, b);} 15 | }; 16 | 17 | int main() 18 | { 19 | Calc* pCalc = new Calc; 20 | 21 | cout << pCalc->Add(1, 2) << endl; 22 | cout << pCalc->Sub(10, 8) << endl; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | // 34 | -------------------------------------------------------------------------------- /4020/client3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | #include "ICalc.h" 4 | using namespace std; 5 | using namespace ecourse; 6 | 7 | class Calc : public ICalc 8 | { 9 | int server; 10 | public: 11 | Calc() { server = ec_find_server("CalcService"); } 12 | 13 | int Add(int a, int b) { return ec_send_server(server, 1, a, b);} 14 | int Sub(int a, int b) { return ec_send_server(server, 2, a, b);} 15 | }; 16 | 17 | int main() 18 | { 19 | Calc* pCalc = new Calc; 20 | 21 | cout << pCalc->Add(1, 2) << endl; 22 | cout << pCalc->Sub(10, 8) << endl; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | // 34 | -------------------------------------------------------------------------------- /4020/client4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | #include "ICalc.h" 4 | using namespace std; 5 | using namespace ecourse; 6 | 7 | typedef ICalc* (*F)(); 8 | 9 | int main() 10 | { 11 | // 동적 모듈 load 12 | void* addr = ec_load_module("CalcProxy.dll"); 13 | 14 | F f = (F)ec_get_function_address(addr, "CreateCalc"); 15 | 16 | ICalc* pCalc = f(); // CreateCalc() 17 | //--------------------------------------- 18 | 19 | cout << pCalc->Add(1, 2) << endl; 20 | cout << pCalc->Sub(10, 8) << endl; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | // 34 | -------------------------------------------------------------------------------- /4020/client5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | #include "ICalc.h" 4 | using namespace std; 5 | using namespace ecourse; 6 | 7 | typedef ICalc* (*F)(); 8 | 9 | int main() 10 | { 11 | // 동적 모듈 load 12 | void* addr = ec_load_module("CalcProxy.dll"); 13 | 14 | F f = (F)ec_get_function_address(addr, "CreateCalc"); 15 | 16 | ICalc* pCalc = f(); // CreateCalc() 17 | pCalc->AddRef(); 18 | //--------------------------------------- 19 | 20 | cout << pCalc->Add(1, 2) << endl; 21 | cout << pCalc->Sub(10, 8) << endl; 22 | 23 | pCalc->Release(); 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | // 37 | -------------------------------------------------------------------------------- /4020/client6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | #include "ICalc.h" 4 | #include "smartptr.h" 5 | using namespace std; 6 | using namespace ecourse; 7 | 8 | typedef ICalc* (*F)(); 9 | 10 | int main() 11 | { 12 | // 동적 모듈 load 13 | void* addr = ec_load_module("CalcProxy.dll"); 14 | F f = (F)ec_get_function_address(addr, "CreateCalc"); 15 | 16 | smartptr pCalc = f(); 17 | //--------------------------------------- 18 | 19 | cout << pCalc->Add(1, 2) << endl; 20 | cout << pCalc->Sub(10, 8) << endl; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | // 34 | -------------------------------------------------------------------------------- /4020/ecourse_dp.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : Design Pattern 5 | * MODULE : ecourse_DP.hpp 6 | * Copyright (C) 2018 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #ifndef ECOURSE_CO_KR 10 | #define ECOURSE_CO_KR 11 | 12 | #if _MSC_VER > 1000 13 | #pragma once 14 | #endif 15 | 16 | #ifdef UNICODE 17 | #undef _UNICODE 18 | #undef UNICODE 19 | #endif 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #pragma comment(lib, "kernel32.lib") 29 | #pragma comment(lib, "user32.lib") 30 | #pragma comment(lib, "gdi32.lib") 31 | using namespace std; 32 | 33 | namespace ecourse 34 | { 35 | namespace module 36 | { 37 | #ifdef _WIN32 38 | #define IOEXPORT __declspec(dllexport) 39 | #else 40 | #define IOEXPORT 41 | #endif 42 | 43 | void* ec_load_module(string path) 44 | { 45 | return reinterpret_cast(LoadLibraryA(path.c_str())); 46 | } 47 | void ec_unload_module(void* p) 48 | { 49 | FreeLibrary((HMODULE)p); 50 | } 51 | void* ec_get_function_address(void* module, string func) 52 | { 53 | return reinterpret_cast(GetProcAddress((HMODULE)module, func.c_str())); 54 | } 55 | } 56 | namespace file 57 | { 58 | typedef int(*PFENUMFILE)(string, void*); 59 | 60 | void ec_enum_files(string path, string filter, PFENUMFILE f, void* param) 61 | { 62 | BOOL b = SetCurrentDirectory(path.c_str()); 63 | 64 | if (b == false) 65 | { 66 | cout << "[DEBUG] " << path.c_str() << " directory does not exit" << endl; 67 | return; 68 | } 69 | WIN32_FIND_DATA wfd = { 0 }; 70 | HANDLE h = ::FindFirstFile(filter.c_str(), &wfd); 71 | do 72 | { 73 | if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) 74 | { 75 | if (f(wfd.cFileName, param) == 0) break; 76 | } 77 | } while (FindNextFile(h, &wfd)); 78 | 79 | FindClose(h); 80 | } 81 | } 82 | 83 | namespace ipc 84 | { 85 | typedef int(*IPC_SERVER_HANDLER)(int, int, int); 86 | 87 | IPC_SERVER_HANDLER _proxyServerHandler; 88 | 89 | LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 90 | { 91 | if (msg >= WM_USER) 92 | return _proxyServerHandler(msg - WM_USER, wParam, lParam); 93 | 94 | switch (msg) 95 | { 96 | case WM_DESTROY: 97 | PostQuitMessage(0); 98 | return 0; 99 | } 100 | return DefWindowProc(hwnd, msg, wParam, lParam); 101 | } 102 | 103 | HWND MakeWindow(string name, int show) 104 | { 105 | HINSTANCE hInstance = GetModuleHandle(0); 106 | WNDCLASS wc; 107 | wc.cbClsExtra = 0; 108 | wc.cbWndExtra = 0; 109 | wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 110 | wc.hCursor = LoadCursor(0, IDC_ARROW); 111 | wc.hIcon = LoadIcon(0, IDI_APPLICATION); 112 | wc.hInstance = hInstance; 113 | wc.lpfnWndProc = WndProc; 114 | wc.lpszClassName = _T("First"); 115 | wc.lpszMenuName = 0; 116 | wc.style = 0; 117 | 118 | RegisterClass(&wc); 119 | 120 | HWND hwnd = CreateWindowEx(0, _T("First"), name.c_str(), WS_OVERLAPPEDWINDOW, 121 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0); 122 | 123 | ShowWindow(hwnd, show); 124 | return hwnd; 125 | } 126 | 127 | 128 | void ProcessMessage(IPC_SERVER_HANDLER handler) 129 | { 130 | _proxyServerHandler = handler; 131 | MSG msg; 132 | while (GetMessage(&msg, 0, 0, 0)) 133 | { 134 | TranslateMessage(&msg); 135 | DispatchMessage(&msg); 136 | } 137 | } 138 | //------------------------------------------------------------------ 139 | 140 | void ec_start_server(string name, IPC_SERVER_HANDLER handler, int show = SW_HIDE) 141 | { 142 | printf("[DEBUG MESSAGE] %s Server Start...\n", name.c_str()); 143 | 144 | MakeWindow(name, show); 145 | ProcessMessage(handler); 146 | } 147 | 148 | int ec_find_server(string name) 149 | { 150 | HWND hwnd = FindWindow(0, name.c_str()); 151 | 152 | if (hwnd == 0) 153 | { 154 | printf("[DEBUG] ???? : %s Server?? ????? ???????.\n", name.c_str()); 155 | return -1; 156 | } 157 | return (long long)hwnd; 158 | } 159 | 160 | int ec_send_server(long long serverid, int code, int param1, int param2) 161 | { 162 | return SendMessage((HWND)serverid, code + WM_USER, param1, param2); 163 | } 164 | 165 | } 166 | using namespace file; 167 | using namespace module; 168 | using namespace ipc; 169 | } 170 | 171 | #endif // ECOURSE_CO_KR 172 | -------------------------------------------------------------------------------- /4020/server1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ecourse_dp.hpp" 3 | #include "ICalc.h" 4 | using namespace std; 5 | using namespace ecourse; 6 | 7 | class Calc : public ICalc 8 | { 9 | public: 10 | int Add(int a, int b) { return a + b; } 11 | int Sub(int a, int b) { return a - b; } 12 | }; 13 | Calc calc; 14 | 15 | 16 | int dispatch( int code, int x, int y ) 17 | { 18 | printf("[DEBUG] %d, %d, %d\n", code, x, y ); 19 | 20 | switch( code ) 21 | { 22 | case 1: return calc.Add( x, y); 23 | case 2: return calc.Sub( x, y); 24 | } 25 | return -1; 26 | } 27 | 28 | int main() 29 | { 30 | ec_start_server("CalcService", dispatch); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | // 45 | -------------------------------------------------------------------------------- /4020/smartptr.h: -------------------------------------------------------------------------------- 1 | template class smartptr 2 | { 3 | T* obj; 4 | public: 5 | smartptr(T* p = 0) : obj(p) 6 | { 7 | if ( obj != 0 ) 8 | obj->AddRef(); 9 | } 10 | smartptr(const smartptr& p) : obj(p.obj) 11 | { 12 | if ( obj != 0 ) 13 | obj->AddRef(); 14 | } 15 | ~smartptr() 16 | { 17 | if ( obj != 0 ) 18 | obj->Release(); 19 | } 20 | 21 | T* operator->() { return obj;} 22 | T& operator*() { return *obj;} 23 | }; 24 | -------------------------------------------------------------------------------- /4020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /4030/facade1.cpp: -------------------------------------------------------------------------------- 1 | // how to build 2 | // g++ facade1.cpp -lws2_32 3 | // cl facade1.cpp /link ws2_32.lib 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | // 1. 네트워크 라이브러리 초기화 11 | WSADATA w; 12 | WSAStartup(0x202, &w); 13 | 14 | // 2. 소켓 생성 15 | int sock = socket(PF_INET, SOCK_STREAM, 0); // TCP 소켓 16 | 17 | // 3. 소켓에 주소 지정 18 | struct sockaddr_in addr = { 0 }; 19 | addr.sin_family = AF_INET; 20 | addr.sin_port = htons(4000); 21 | addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | bind(sock, (SOCKADDR*)&addr, sizeof(addr)); 24 | 25 | // 4. 소켓을 대기 상태로변경 26 | listen(sock, 5); 27 | 28 | // 5. 클라이언트가 접속할때 까지 대기 29 | struct sockaddr_in addr2 = { 0 }; 30 | int sz = sizeof(addr2); 31 | 32 | accept(sock, (SOCKADDR*)&addr2, &sz); 33 | 34 | // 6. socket 라이브러리 cleanup 35 | WSACleanup(); 36 | } 37 | -------------------------------------------------------------------------------- /4030/facade2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // network 라이브러리의 초기화와 cleanup을 담당. 6 | class NetworkInit 7 | { 8 | public: 9 | NetworkInit() 10 | { 11 | WSADATA w; 12 | WSAStartup(0x202, &w); 13 | } 14 | ~NetworkInit() 15 | { 16 | WSACleanup(); 17 | } 18 | }; 19 | 20 | // IP 주소 관리. 21 | class IPAddress 22 | { 23 | struct sockaddr_in addr; 24 | public: 25 | IPAddress(const char* ip, short port) 26 | { 27 | addr.sin_family = AF_INET; 28 | addr.sin_port = htons(port); 29 | addr.sin_addr.s_addr = inet_addr(ip); 30 | } 31 | struct sockaddr* getRawAddress() { return (struct sockaddr*)&addr; } 32 | }; 33 | 34 | // socket 프로그래밍의 일반적인 절차. 35 | class Socket 36 | { 37 | int sock; 38 | public: 39 | Socket(int type) { sock = socket(PF_INET, type, 0); } 40 | 41 | void Bind(IPAddress* ip) 42 | { 43 | ::bind(sock, ip->getRawAddress(), sizeof(struct sockaddr_in)); 44 | } 45 | void Listen() { ::listen(sock, 5); } 46 | void Accept() 47 | { 48 | struct sockaddr_in addr2 = { 0 }; 49 | int sz = sizeof(addr2); 50 | accept(sock, (struct sockaddr*)&addr2, &sz); 51 | } 52 | }; 53 | 54 | 55 | 56 | int main() 57 | { 58 | NetworkInit init; 59 | IPAddress ip("127.0.0.1", 4000); 60 | Socket sock(SOCK_STREAM); // TCP 61 | sock.Bind(&ip); 62 | sock.Listen(); 63 | sock.Accept(); 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | // 75 | -------------------------------------------------------------------------------- /4030/facade3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // network 라이브러리의 초기화와 cleanup을 담당. 7 | class NetworkInit 8 | { 9 | public: 10 | NetworkInit() 11 | { 12 | WSADATA w; 13 | WSAStartup(0x202, &w); 14 | } 15 | ~NetworkInit() 16 | { 17 | WSACleanup(); 18 | } 19 | }; 20 | 21 | // IP 주소 관리. 22 | class IPAddress 23 | { 24 | struct sockaddr_in addr; 25 | public: 26 | IPAddress(const char* ip, short port) 27 | { 28 | addr.sin_family = AF_INET; 29 | addr.sin_port = htons(port); 30 | addr.sin_addr.s_addr = inet_addr(ip); 31 | } 32 | struct sockaddr* getRawAddress() { return (struct sockaddr*)&addr; } 33 | }; 34 | 35 | // socket 프로그래밍의 일반적인 절차. 36 | class Socket 37 | { 38 | int sock; 39 | public: 40 | Socket(int type) { sock = socket(PF_INET, type, 0); } 41 | 42 | void Bind(IPAddress* ip) 43 | { 44 | ::bind(sock, ip->getRawAddress(), sizeof(struct sockaddr_in)); 45 | } 46 | void Listen() { ::listen(sock, 5); } 47 | void Accept() 48 | { 49 | struct sockaddr_in addr2 = { 0 }; 50 | int sz = sizeof(addr2); 51 | accept(sock, (struct sockaddr*)&addr2, &sz); 52 | } 53 | }; 54 | 55 | class TCPServer 56 | { 57 | NetworkInit init; 58 | Socket sock; 59 | public: 60 | TCPServer() : sock(SOCK_STREAM) {} 61 | 62 | void Start(const char* sip, short port) 63 | { 64 | IPAddress ip(sip, port); 65 | sock.Bind(&ip); 66 | sock.Listen(); 67 | sock.Accept(); 68 | } 69 | }; 70 | 71 | int main() 72 | { 73 | TCPServer server; 74 | server.Start( "127.0.0.1", 4000); 75 | } 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | // 92 | -------------------------------------------------------------------------------- /4030/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /4040/Pimpl.cpp: -------------------------------------------------------------------------------- 1 | //#include "Point1.h" 2 | #include "Point2.h" 3 | 4 | int main() 5 | { 6 | Point p(1,2); 7 | p.Print(); 8 | } 9 | -------------------------------------------------------------------------------- /4040/Point1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Point1.h" 3 | using namespace std; 4 | 5 | Point::Point( int a, int b) : x(a), y(b) {} 6 | 7 | void Point::Print() const 8 | { 9 | cout << x << ", " << y << endl; 10 | } 11 | -------------------------------------------------------------------------------- /4040/Point1.h: -------------------------------------------------------------------------------- 1 | // Point1.h 2 | class Point 3 | { 4 | int x, y; 5 | int debug; 6 | public: 7 | Point( int a = 0, int b = 0); 8 | 9 | void Print() const; 10 | }; 11 | -------------------------------------------------------------------------------- /4040/Point2.cpp: -------------------------------------------------------------------------------- 1 | // Point2.cpp 2 | #include "PointImpl.h" 3 | #include "Point2.h" 4 | 5 | Point::Point(int a, int b) 6 | { 7 | pImpl = new PointImpl(a, b); 8 | } 9 | 10 | void Point::Print() const 11 | { 12 | pImpl->Print(); 13 | } 14 | -------------------------------------------------------------------------------- /4040/Point2.h: -------------------------------------------------------------------------------- 1 | // Point2.h 2 | 3 | //#include "PointImpl.h" 4 | class PointImpl; // forward declaration.. 핵심.. 5 | 6 | class Point 7 | { 8 | PointImpl* pImpl; 9 | public: 10 | Point(int a = 0, int b = 0); 11 | 12 | void Print() const; 13 | }; 14 | -------------------------------------------------------------------------------- /4040/PointImpl.cpp: -------------------------------------------------------------------------------- 1 | // PointImpl.cpp 2 | #include 3 | #include "PointImpl.h" 4 | using namespace std; 5 | 6 | PointImpl::PointImpl( int a, int b) : x(a), y(b) {} 7 | 8 | void PointImpl::Print() const 9 | { 10 | cout << x << ", " << y << endl; 11 | } 12 | -------------------------------------------------------------------------------- /4040/PointImpl.h: -------------------------------------------------------------------------------- 1 | // PointImpl.h 2 | class PointImpl 3 | { 4 | int x, y; 5 | int debug; 6 | public: 7 | PointImpl( int a = 0, int b = 0); 8 | 9 | void Print() const; 10 | }; 11 | -------------------------------------------------------------------------------- /4040/bridge1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct IMP3 5 | { 6 | virtual void Play() = 0; 7 | virtual void Stop() = 0; 8 | virtual ~IMP3() {} 9 | }; 10 | 11 | class IPod : public IMP3 12 | { 13 | public: 14 | void Play() { cout << "Play MP3" << endl;} 15 | void Stop() { cout << "Stop MP3" << endl;} 16 | }; 17 | 18 | class People 19 | { 20 | public: 21 | void UseMP3(IMP3* p ) 22 | { 23 | p->Play(); 24 | p->PlayOneMinute(); 25 | } 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | // 40 | -------------------------------------------------------------------------------- /4040/bridge2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct IMP3 5 | { 6 | virtual void Play() = 0; 7 | virtual void Stop() = 0; 8 | virtual ~IMP3() {} 9 | }; 10 | 11 | class IPod : public IMP3 12 | { 13 | public: 14 | void Play() { cout << "Play MP3" << endl;} 15 | void Stop() { cout << "Stop MP3" << endl;} 16 | }; 17 | //--------------------------- 18 | class MP3 19 | { 20 | IMP3* pImpl; 21 | public: 22 | MP3() 23 | { 24 | pImpl = new IPod; 25 | } 26 | void Play() { pImpl->Play();} 27 | void Stop() { pImpl->Stop();} 28 | void PlayOneMinute() 29 | { 30 | pImpl->Play(); 31 | Sleep(1); 32 | pImpl->Stop(); 33 | } 34 | }; 35 | 36 | class People 37 | { 38 | public: 39 | void UseMP3(MP3* p ) 40 | { 41 | p->Play(); 42 | p->PlayOneMinute(); 43 | } 44 | }; 45 | 46 | 47 | int main() 48 | { 49 | 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | // 58 | -------------------------------------------------------------------------------- /4040/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /5010/observer1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Table 6 | { 7 | int data; 8 | public: 9 | void SetData(int d) { data = d; } 10 | }; 11 | 12 | class PieGraph 13 | { 14 | public: 15 | void Draw(int n) 16 | { 17 | cout << "Pie Graph : "; 18 | 19 | for ( i = 0; i < n; i++) 20 | cout << "*"; 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | // 48 | -------------------------------------------------------------------------------- /5010/observer2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | struct IGraph 7 | { 8 | virtual void update(int) = 0; 9 | 10 | virtual ~IGraph() {} 11 | }; 12 | 13 | 14 | class Table 15 | { 16 | vector v; 17 | 18 | int data; 19 | public: 20 | void attach(IGraph* p) { v.push_back(p);} 21 | void detach(IGraph* p) { } 22 | 23 | 24 | void SetData(int d) 25 | { 26 | data = d; 27 | 28 | for ( auto p : v) 29 | p->update(data); 30 | } 31 | }; 32 | 33 | 34 | 35 | 36 | class PieGraph : public IGraph 37 | { 38 | public: 39 | virtual void update(int n) 40 | { 41 | Draw(n); // 그래프를 다시 그린다. 42 | } 43 | void Draw(int n) 44 | { 45 | cout << "Pie Graph : "; 46 | 47 | for ( int i = 0; i < n; i++) 48 | cout << "*"; 49 | cout << endl; 50 | } 51 | }; 52 | 53 | class BarGraph : public IGraph 54 | { 55 | public: 56 | virtual void update(int n) 57 | { 58 | Draw(n); // 그래프를 다시 그린다. 59 | } 60 | void Draw(int n) 61 | { 62 | cout << "Bar Graph : "; 63 | 64 | for ( int i = 0; i < n; i++) 65 | cout << "+"; 66 | cout << endl; 67 | } 68 | }; 69 | 70 | int main() 71 | { 72 | BarGraph bg; 73 | PieGraph pg; 74 | 75 | Table t; 76 | t.attach( &bg); 77 | t.attach( &pg); 78 | 79 | while( 1 ) 80 | { 81 | int n; 82 | cin >> n; 83 | t.SetData(n); 84 | } 85 | } 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | // 107 | -------------------------------------------------------------------------------- /5010/observer3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | struct IGraph 7 | { 8 | virtual void update(int) = 0; 9 | 10 | virtual ~IGraph() {} 11 | }; 12 | 13 | // 관찰자의 기본 기능을 제공하는 클래스 14 | class Subject 15 | { 16 | vector v; 17 | public: 18 | void attach(IGraph* p) { v.push_back(p);} 19 | void detach(IGraph* p) { } 20 | void notify(int data) 21 | { 22 | for ( auto p : v) 23 | p->update(data); 24 | } 25 | }; 26 | 27 | class Table : public Subject 28 | { 29 | int data; 30 | public: 31 | void SetData(int d) 32 | { 33 | data = d; 34 | notify(data); 35 | } 36 | }; 37 | 38 | /* 39 | class Table3D : public Subject 40 | { 41 | int data[10]; 42 | public: 43 | void SetData(int d) 44 | { 45 | data = d; 46 | } 47 | }; 48 | */ 49 | 50 | 51 | class PieGraph : public IGraph 52 | { 53 | public: 54 | virtual void update(int n) 55 | { 56 | Draw(n); 57 | } 58 | void Draw(int n) 59 | { 60 | cout << "Pie Graph : "; 61 | 62 | for ( int i = 0; i < n; i++) 63 | cout << "*"; 64 | cout << endl; 65 | } 66 | }; 67 | 68 | class BarGraph : public IGraph 69 | { 70 | public: 71 | virtual void update(int n) 72 | { 73 | Draw(n); // 그래프를 다시 그린다. 74 | } 75 | void Draw(int n) 76 | { 77 | cout << "Bar Graph : "; 78 | 79 | for ( int i = 0; i < n; i++) 80 | cout << "+"; 81 | cout << endl; 82 | } 83 | }; 84 | 85 | int main() 86 | { 87 | BarGraph bg; 88 | PieGraph pg; 89 | 90 | Table t; 91 | t.attach( &bg); 92 | t.attach( &pg); 93 | 94 | while( 1 ) 95 | { 96 | int n; 97 | cin >> n; 98 | t.SetData(n); 99 | } 100 | } 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | // 122 | -------------------------------------------------------------------------------- /5010/observer4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | class Subject; 7 | 8 | struct IGraph 9 | { 10 | virtual void update(Subject*) = 0; 11 | 12 | virtual ~IGraph() {} 13 | }; 14 | 15 | 16 | 17 | 18 | 19 | class Subject 20 | { 21 | vector v; 22 | public: 23 | void attach(IGraph* p) { v.push_back(p);} 24 | void detach(IGraph* p) { } 25 | 26 | void notify() 27 | { 28 | for ( auto p : v) 29 | p->update(this); 30 | } 31 | }; 32 | 33 | class Table : public Subject 34 | { 35 | int data; 36 | int color; 37 | public: 38 | int GetData() { return data;} 39 | 40 | void SetData(int d) 41 | { 42 | data = d; 43 | notify(); 44 | } 45 | }; 46 | 47 | 48 | class PieGraph : public IGraph 49 | { 50 | public: 51 | virtual void update(Subject* p) 52 | { 53 | // table 에 접근해서 data를 꺼내 온다. 54 | //int n = p->GetData(); // error. 캐스팅 필요. 55 | int n = static_cast(p)->GetData(); 56 | 57 | Draw(n); 58 | } 59 | 60 | 61 | 62 | void Draw(int n) 63 | { 64 | cout << "Pie Graph : "; 65 | 66 | for ( int i = 0; i < n; i++) 67 | cout << "*"; 68 | cout << endl; 69 | } 70 | }; 71 | 72 | class BarGraph : public IGraph 73 | { 74 | public: 75 | virtual void update(Subject* p) 76 | { 77 | int n = static_cast(p)->GetData(); 78 | 79 | Draw(n); 80 | } 81 | 82 | void Draw(int n) 83 | { 84 | cout << "Bar Graph : "; 85 | 86 | for ( int i = 0; i < n; i++) 87 | cout << "+"; 88 | cout << endl; 89 | } 90 | }; 91 | 92 | int main() 93 | { 94 | BarGraph bg; 95 | PieGraph pg; 96 | 97 | Table t; 98 | t.attach( &bg); 99 | t.attach( &pg); 100 | 101 | while( 1 ) 102 | { 103 | int n; 104 | cin >> n; 105 | t.SetData(n); 106 | } 107 | } 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | // 129 | -------------------------------------------------------------------------------- /5010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /5020/Vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_VECTOR_H 18 | #define ANDROID_VECTOR_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | // --------------------------------------------------------------------------- 28 | 29 | namespace android { 30 | 31 | template 32 | class SortedVector; 33 | 34 | /*! 35 | * The main templated vector class ensuring type safety 36 | * while making use of VectorImpl. 37 | * This is the class users want to use. 38 | */ 39 | 40 | template 41 | class Vector : private VectorImpl 42 | { 43 | public: 44 | typedef TYPE value_type; 45 | 46 | /*! 47 | * Constructors and destructors 48 | */ 49 | 50 | Vector(); 51 | Vector(const Vector& rhs); 52 | explicit Vector(const SortedVector& rhs); 53 | virtual ~Vector(); 54 | 55 | /*! copy operator */ 56 | const Vector& operator = (const Vector& rhs) const; 57 | Vector& operator = (const Vector& rhs); 58 | 59 | const Vector& operator = (const SortedVector& rhs) const; 60 | Vector& operator = (const SortedVector& rhs); 61 | 62 | /* 63 | * empty the vector 64 | */ 65 | 66 | inline void clear() { VectorImpl::clear(); } 67 | 68 | /*! 69 | * vector stats 70 | */ 71 | 72 | //! returns number of items in the vector 73 | inline size_t size() const { return VectorImpl::size(); } 74 | //! returns whether or not the vector is empty 75 | inline bool isEmpty() const { return VectorImpl::isEmpty(); } 76 | //! returns how many items can be stored without reallocating the backing store 77 | inline size_t capacity() const { return VectorImpl::capacity(); } 78 | //! sets the capacity. capacity can never be reduced less than size() 79 | inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } 80 | 81 | /*! 82 | * set the size of the vector. items are appended with the default 83 | * constructor, or removed from the end as needed. 84 | */ 85 | inline ssize_t resize(size_t size) { return VectorImpl::resize(size); } 86 | 87 | /*! 88 | * C-style array access 89 | */ 90 | 91 | //! read-only C-style access 92 | inline const TYPE* array() const; 93 | //! read-write C-style access 94 | TYPE* editArray(); 95 | 96 | /*! 97 | * accessors 98 | */ 99 | 100 | //! read-only access to an item at a given index 101 | inline const TYPE& operator [] (size_t index) const; 102 | //! alternate name for operator [] 103 | inline const TYPE& itemAt(size_t index) const; 104 | //! stack-usage of the vector. returns the top of the stack (last element) 105 | const TYPE& top() const; 106 | 107 | /*! 108 | * modifying the array 109 | */ 110 | 111 | //! copy-on write support, grants write access to an item 112 | TYPE& editItemAt(size_t index); 113 | //! grants right access to the top of the stack (last element) 114 | TYPE& editTop(); 115 | 116 | /*! 117 | * append/insert another vector 118 | */ 119 | 120 | //! insert another vector at a given index 121 | ssize_t insertVectorAt(const Vector& vector, size_t index); 122 | 123 | //! append another vector at the end of this one 124 | ssize_t appendVector(const Vector& vector); 125 | 126 | 127 | //! insert an array at a given index 128 | ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length); 129 | 130 | //! append an array at the end of this vector 131 | ssize_t appendArray(const TYPE* array, size_t length); 132 | 133 | /*! 134 | * add/insert/replace items 135 | */ 136 | 137 | //! insert one or several items initialized with their default constructor 138 | inline ssize_t insertAt(size_t index, size_t numItems = 1); 139 | //! insert one or several items initialized from a prototype item 140 | ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1); 141 | //! pop the top of the stack (removes the last element). No-op if the stack's empty 142 | inline void pop(); 143 | //! pushes an item initialized with its default constructor 144 | inline void push(); 145 | //! pushes an item on the top of the stack 146 | void push(const TYPE& item); 147 | //! same as push() but returns the index the item was added at (or an error) 148 | inline ssize_t add(); 149 | //! same as push() but returns the index the item was added at (or an error) 150 | ssize_t add(const TYPE& item); 151 | //! replace an item with a new one initialized with its default constructor 152 | inline ssize_t replaceAt(size_t index); 153 | //! replace an item with a new one 154 | ssize_t replaceAt(const TYPE& item, size_t index); 155 | 156 | /*! 157 | * remove items 158 | */ 159 | 160 | //! remove several items 161 | inline ssize_t removeItemsAt(size_t index, size_t count = 1); 162 | //! remove one item 163 | inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } 164 | 165 | /*! 166 | * sort (stable) the array 167 | */ 168 | 169 | typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs); 170 | typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state); 171 | 172 | inline status_t sort(compar_t cmp); 173 | inline status_t sort(compar_r_t cmp, void* state); 174 | 175 | // for debugging only 176 | inline size_t getItemSize() const { return itemSize(); } 177 | 178 | 179 | /* 180 | * these inlines add some level of compatibility with STL. eventually 181 | * we should probably turn things around. 182 | */ 183 | typedef TYPE* iterator; 184 | typedef TYPE const* const_iterator; 185 | 186 | inline iterator begin() { return editArray(); } 187 | inline iterator end() { return editArray() + size(); } 188 | inline const_iterator begin() const { return array(); } 189 | inline const_iterator end() const { return array() + size(); } 190 | inline void reserve(size_t n) { setCapacity(n); } 191 | inline bool empty() const{ return isEmpty(); } 192 | inline void push_back(const TYPE& item) { insertAt(item, size(), 1); } 193 | inline void push_front(const TYPE& item) { insertAt(item, 0, 1); } 194 | inline iterator erase(iterator pos) { 195 | ssize_t index = removeItemsAt(static_cast(pos-array())); 196 | return begin() + index; 197 | } 198 | 199 | protected: 200 | virtual void do_construct(void* storage, size_t num) const; 201 | virtual void do_destroy(void* storage, size_t num) const; 202 | virtual void do_copy(void* dest, const void* from, size_t num) const; 203 | virtual void do_splat(void* dest, const void* item, size_t num) const; 204 | virtual void do_move_forward(void* dest, const void* from, size_t num) const; 205 | virtual void do_move_backward(void* dest, const void* from, size_t num) const; 206 | }; 207 | 208 | // --------------------------------------------------------------------------- 209 | // No user serviceable parts from here... 210 | // --------------------------------------------------------------------------- 211 | 212 | template inline 213 | Vector::Vector() 214 | : VectorImpl(sizeof(TYPE), 215 | ((traits::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0) 216 | |(traits::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0) 217 | |(traits::has_trivial_copy ? HAS_TRIVIAL_COPY : 0)) 218 | ) 219 | { 220 | } 221 | 222 | template inline 223 | Vector::Vector(const Vector& rhs) 224 | : VectorImpl(rhs) { 225 | } 226 | 227 | template inline 228 | Vector::Vector(const SortedVector& rhs) 229 | : VectorImpl(static_cast(rhs)) { 230 | } 231 | 232 | template inline 233 | Vector::~Vector() { 234 | finish_vector(); 235 | } 236 | 237 | template inline 238 | Vector& Vector::operator = (const Vector& rhs) { 239 | VectorImpl::operator = (rhs); 240 | return *this; 241 | } 242 | 243 | template inline 244 | const Vector& Vector::operator = (const Vector& rhs) const { 245 | VectorImpl::operator = (static_cast(rhs)); 246 | return *this; 247 | } 248 | 249 | template inline 250 | Vector& Vector::operator = (const SortedVector& rhs) { 251 | VectorImpl::operator = (static_cast(rhs)); 252 | return *this; 253 | } 254 | 255 | template inline 256 | const Vector& Vector::operator = (const SortedVector& rhs) const { 257 | VectorImpl::operator = (rhs); 258 | return *this; 259 | } 260 | 261 | template inline 262 | const TYPE* Vector::array() const { 263 | return static_cast(arrayImpl()); 264 | } 265 | 266 | template inline 267 | TYPE* Vector::editArray() { 268 | return static_cast(editArrayImpl()); 269 | } 270 | 271 | 272 | template inline 273 | const TYPE& Vector::operator[](size_t index) const { 274 | LOG_FATAL_IF(index>=size(), 275 | "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, 276 | int(index), int(size())); 277 | return *(array() + index); 278 | } 279 | 280 | template inline 281 | const TYPE& Vector::itemAt(size_t index) const { 282 | return operator[](index); 283 | } 284 | 285 | template inline 286 | const TYPE& Vector::top() const { 287 | return *(array() + size() - 1); 288 | } 289 | 290 | template inline 291 | TYPE& Vector::editItemAt(size_t index) { 292 | return *( static_cast(editItemLocation(index)) ); 293 | } 294 | 295 | template inline 296 | TYPE& Vector::editTop() { 297 | return *( static_cast(editItemLocation(size()-1)) ); 298 | } 299 | 300 | template inline 301 | ssize_t Vector::insertVectorAt(const Vector& vector, size_t index) { 302 | return VectorImpl::insertVectorAt(reinterpret_cast(vector), index); 303 | } 304 | 305 | template inline 306 | ssize_t Vector::appendVector(const Vector& vector) { 307 | return VectorImpl::appendVector(reinterpret_cast(vector)); 308 | } 309 | 310 | template inline 311 | ssize_t Vector::insertArrayAt(const TYPE* array, size_t index, size_t length) { 312 | return VectorImpl::insertArrayAt(array, index, length); 313 | } 314 | 315 | template inline 316 | ssize_t Vector::appendArray(const TYPE* array, size_t length) { 317 | return VectorImpl::appendArray(array, length); 318 | } 319 | 320 | template inline 321 | ssize_t Vector::insertAt(const TYPE& item, size_t index, size_t numItems) { 322 | return VectorImpl::insertAt(&item, index, numItems); 323 | } 324 | 325 | template inline 326 | void Vector::push(const TYPE& item) { 327 | return VectorImpl::push(&item); 328 | } 329 | 330 | template inline 331 | ssize_t Vector::add(const TYPE& item) { 332 | return VectorImpl::add(&item); 333 | } 334 | 335 | template inline 336 | ssize_t Vector::replaceAt(const TYPE& item, size_t index) { 337 | return VectorImpl::replaceAt(&item, index); 338 | } 339 | 340 | template inline 341 | ssize_t Vector::insertAt(size_t index, size_t numItems) { 342 | return VectorImpl::insertAt(index, numItems); 343 | } 344 | 345 | template inline 346 | void Vector::pop() { 347 | VectorImpl::pop(); 348 | } 349 | 350 | template inline 351 | void Vector::push() { 352 | VectorImpl::push(); 353 | } 354 | 355 | template inline 356 | ssize_t Vector::add() { 357 | return VectorImpl::add(); 358 | } 359 | 360 | template inline 361 | ssize_t Vector::replaceAt(size_t index) { 362 | return VectorImpl::replaceAt(index); 363 | } 364 | 365 | template inline 366 | ssize_t Vector::removeItemsAt(size_t index, size_t count) { 367 | return VectorImpl::removeItemsAt(index, count); 368 | } 369 | 370 | template inline 371 | status_t Vector::sort(Vector::compar_t cmp) { 372 | return VectorImpl::sort(reinterpret_cast(cmp)); 373 | } 374 | 375 | template inline 376 | status_t Vector::sort(Vector::compar_r_t cmp, void* state) { 377 | return VectorImpl::sort(reinterpret_cast(cmp), state); 378 | } 379 | 380 | // --------------------------------------------------------------------------- 381 | 382 | template 383 | void Vector::do_construct(void* storage, size_t num) const { 384 | construct_type( reinterpret_cast(storage), num ); 385 | } 386 | 387 | template 388 | void Vector::do_destroy(void* storage, size_t num) const { 389 | destroy_type( reinterpret_cast(storage), num ); 390 | } 391 | 392 | template 393 | void Vector::do_copy(void* dest, const void* from, size_t num) const { 394 | copy_type( reinterpret_cast(dest), reinterpret_cast(from), num ); 395 | } 396 | 397 | template 398 | void Vector::do_splat(void* dest, const void* item, size_t num) const { 399 | splat_type( reinterpret_cast(dest), reinterpret_cast(item), num ); 400 | } 401 | 402 | template 403 | void Vector::do_move_forward(void* dest, const void* from, size_t num) const { 404 | move_forward_type( reinterpret_cast(dest), reinterpret_cast(from), num ); 405 | } 406 | 407 | template 408 | void Vector::do_move_backward(void* dest, const void* from, size_t num) const { 409 | move_backward_type( reinterpret_cast(dest), reinterpret_cast(from), num ); 410 | } 411 | 412 | }; // namespace android 413 | 414 | 415 | // --------------------------------------------------------------------------- 416 | 417 | #endif // ANDROID_VECTOR_H 418 | -------------------------------------------------------------------------------- /5020/VectorImpl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_VECTOR_IMPL_H 18 | #define ANDROID_VECTOR_IMPL_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | // --------------------------------------------------------------------------- 26 | // No user serviceable parts in here... 27 | // --------------------------------------------------------------------------- 28 | 29 | namespace android { 30 | 31 | /*! 32 | * Implementation of the guts of the vector<> class 33 | * this ensures backward binary compatibility and 34 | * reduces code size. 35 | * For performance reasons, we expose mStorage and mCount 36 | * so these fields are set in stone. 37 | * 38 | */ 39 | 40 | class VectorImpl 41 | { 42 | public: 43 | enum { // flags passed to the ctor 44 | HAS_TRIVIAL_CTOR = 0x00000001, 45 | HAS_TRIVIAL_DTOR = 0x00000002, 46 | HAS_TRIVIAL_COPY = 0x00000004, 47 | }; 48 | 49 | VectorImpl(size_t itemSize, uint32_t flags); 50 | VectorImpl(const VectorImpl& rhs); 51 | virtual ~VectorImpl(); 52 | 53 | /*! must be called from subclasses destructor */ 54 | void finish_vector(); 55 | 56 | VectorImpl& operator = (const VectorImpl& rhs); 57 | 58 | /*! C-style array access */ 59 | inline const void* arrayImpl() const { return mStorage; } 60 | void* editArrayImpl(); 61 | 62 | /*! vector stats */ 63 | inline size_t size() const { return mCount; } 64 | inline bool isEmpty() const { return mCount == 0; } 65 | size_t capacity() const; 66 | ssize_t setCapacity(size_t size); 67 | ssize_t resize(size_t size); 68 | 69 | /*! append/insert another vector or array */ 70 | ssize_t insertVectorAt(const VectorImpl& vector, size_t index); 71 | ssize_t appendVector(const VectorImpl& vector); 72 | ssize_t insertArrayAt(const void* array, size_t index, size_t length); 73 | ssize_t appendArray(const void* array, size_t length); 74 | 75 | /*! add/insert/replace items */ 76 | ssize_t insertAt(size_t where, size_t numItems = 1); 77 | ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); 78 | void pop(); 79 | void push(); 80 | void push(const void* item); 81 | ssize_t add(); 82 | ssize_t add(const void* item); 83 | ssize_t replaceAt(size_t index); 84 | ssize_t replaceAt(const void* item, size_t index); 85 | 86 | /*! remove items */ 87 | ssize_t removeItemsAt(size_t index, size_t count = 1); 88 | void clear(); 89 | 90 | const void* itemLocation(size_t index) const; 91 | void* editItemLocation(size_t index); 92 | 93 | typedef int (*compar_t)(const void* lhs, const void* rhs); 94 | typedef int (*compar_r_t)(const void* lhs, const void* rhs, void* state); 95 | status_t sort(compar_t cmp); 96 | status_t sort(compar_r_t cmp, void* state); 97 | 98 | protected: 99 | size_t itemSize() const; 100 | void release_storage(); 101 | 102 | virtual void do_construct(void* storage, size_t num) const = 0; 103 | virtual void do_destroy(void* storage, size_t num) const = 0; 104 | virtual void do_copy(void* dest, const void* from, size_t num) const = 0; 105 | virtual void do_splat(void* dest, const void* item, size_t num) const = 0; 106 | virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0; 107 | virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0; 108 | 109 | private: 110 | void* _grow(size_t where, size_t amount); 111 | void _shrink(size_t where, size_t amount); 112 | 113 | inline void _do_construct(void* storage, size_t num) const; 114 | inline void _do_destroy(void* storage, size_t num) const; 115 | inline void _do_copy(void* dest, const void* from, size_t num) const; 116 | inline void _do_splat(void* dest, const void* item, size_t num) const; 117 | inline void _do_move_forward(void* dest, const void* from, size_t num) const; 118 | inline void _do_move_backward(void* dest, const void* from, size_t num) const; 119 | 120 | // These 2 fields are exposed in the inlines below, 121 | // so they're set in stone. 122 | void * mStorage; // base address of the vector 123 | size_t mCount; // number of items 124 | 125 | const uint32_t mFlags; 126 | const size_t mItemSize; 127 | }; 128 | 129 | 130 | 131 | class SortedVectorImpl : public VectorImpl 132 | { 133 | public: 134 | SortedVectorImpl(size_t itemSize, uint32_t flags); 135 | explicit SortedVectorImpl(const VectorImpl& rhs); 136 | virtual ~SortedVectorImpl(); 137 | 138 | SortedVectorImpl& operator = (const SortedVectorImpl& rhs); 139 | 140 | //! finds the index of an item 141 | ssize_t indexOf(const void* item) const; 142 | 143 | //! finds where this item should be inserted 144 | size_t orderOf(const void* item) const; 145 | 146 | //! add an item in the right place (or replaces it if there is one) 147 | ssize_t add(const void* item); 148 | 149 | //! merges a vector into this one 150 | ssize_t merge(const VectorImpl& vector); 151 | ssize_t merge(const SortedVectorImpl& vector); 152 | 153 | //! removes an item 154 | ssize_t remove(const void* item); 155 | 156 | protected: 157 | virtual int do_compare(const void* lhs, const void* rhs) const = 0; 158 | 159 | private: 160 | ssize_t _indexOrderOf(const void* item, size_t* order = 0) const; 161 | 162 | // these are made private, because they can't be used on a SortedVector 163 | // (they don't have an implementation either) 164 | ssize_t add(); 165 | void pop(); 166 | void push(); 167 | void push(const void* item); 168 | ssize_t insertVectorAt(const VectorImpl& vector, size_t index); 169 | ssize_t appendVector(const VectorImpl& vector); 170 | ssize_t insertArrayAt(const void* array, size_t index, size_t length); 171 | ssize_t appendArray(const void* array, size_t length); 172 | ssize_t insertAt(size_t where, size_t numItems = 1); 173 | ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); 174 | ssize_t replaceAt(size_t index); 175 | ssize_t replaceAt(const void* item, size_t index); 176 | }; 177 | 178 | }; // namespace android 179 | 180 | 181 | // --------------------------------------------------------------------------- 182 | 183 | #endif // ANDROID_VECTOR_IMPL_H 184 | -------------------------------------------------------------------------------- /5020/cont1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | Node* next; 8 | Node( int d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | class slist 12 | { 13 | Node* head = 0; 14 | public: 15 | void push_front(int n) { head = new Node(n, head);} 16 | int front() { return head->data;} 17 | }; 18 | 19 | int main() 20 | { 21 | slist s; 22 | 23 | s.push_front(10); 24 | s.push_front(20); 25 | s.push_front(30); 26 | s.push_front(40); 27 | s.push_front(50); 28 | 29 | int n = s.front(); 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | // 41 | -------------------------------------------------------------------------------- /5020/cont2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Object 5 | { 6 | virtual ~Object() {} 7 | }; 8 | // 모든 클래스는 object 로 부터 파생되어야 한다. 9 | 10 | class Dialog : public Object{}; 11 | class Point : public Object{}; 12 | class Rect : public Object{}; 13 | class Integer : public Object 14 | { 15 | int value; 16 | public: 17 | Integer(int n) : value(n) {} 18 | } 19 | 20 | 21 | struct Node 22 | { 23 | Object* data; 24 | Node* next; 25 | Node( Object* d, Node* n) : data(d), next(n) {} 26 | }; 27 | 28 | class slist 29 | { 30 | Node* head = 0; 31 | public: 32 | void push_front(Object* n) { head = new Node(n, head);} 33 | Object* front() { return head->data;} 34 | }; 35 | 36 | int main() 37 | { 38 | slist s; 39 | 40 | s.push_front(new Point); 41 | s.push_front(new Point); 42 | 43 | s.push_front ( 10 ); // error 44 | s.push_front ( new Integer(10) ); // ok. 45 | 46 | // Point* p = s.front(); 47 | Point* p = static_cast(s.front()); 48 | 49 | // s.push_front( new Dialog ); 50 | 51 | 52 | // int n = s.front(); 53 | } 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | // 64 | -------------------------------------------------------------------------------- /5020/cont3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Node 5 | { 6 | T data; 7 | Node* next; 8 | Node( const T& d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | template class slist 12 | { 13 | Node* head = 0; 14 | public: 15 | void push_front(const T& n) { head = new Node(n, head);} 16 | T front() { return head->data;} 17 | }; 18 | 19 | int main() 20 | { 21 | slist s2; 22 | 23 | slist s; 24 | 25 | s.push_front(10); 26 | s.push_front(20); 27 | s.push_front(30); 28 | //s.push_front( new Dialog); 29 | 30 | int n = s.front(); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | // 42 | -------------------------------------------------------------------------------- /5020/cont4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | void* data; 7 | Node* next; 8 | Node( void* d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | class slistImp 12 | { 13 | Node* head = 0; 14 | public: 15 | void push_front(void* n) { head = new Node(n, head);} 16 | void* front() { return head->data;} 17 | }; 18 | 19 | template class slist : public slistImp 20 | { 21 | public: 22 | inline void push_front(T n) { slistImp::push_front( (void*)n);} 23 | inline T front() { return (T)(slistImp::front());} 24 | }; 25 | 26 | 27 | int main() 28 | { 29 | slist s; 30 | 31 | s.push_front(10); 32 | s.push_front(20); 33 | s.push_front(30); 34 | s.push_front(40); 35 | s.push_front(50); 36 | 37 | int n = s.front(); 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | // 49 | -------------------------------------------------------------------------------- /5020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /5030/iterator1.cpp: -------------------------------------------------------------------------------- 1 | // cont3.cpp => iterator1.cpp 2 | #include 3 | using namespace std; 4 | 5 | template struct Node 6 | { 7 | T data; 8 | Node* next; 9 | Node( const T& d, Node* n) : data(d), next(n) {} 10 | }; 11 | 12 | template class slist 13 | { 14 | Node* head = 0; 15 | public: 16 | void push_front(const T& n) { head = new Node(n, head);} 17 | T front() { return head->data;} 18 | }; 19 | 20 | int main() 21 | { 22 | slist s; 23 | 24 | s.push_front(10); 25 | s.push_front(20); 26 | s.push_front(30); 27 | s.push_front(40); 28 | 29 | SlisEnumerator* p = s.GetEnumerator(); 30 | 31 | int n = p->GetObject(); // 40 32 | p->MoveNext(); // 33 | 34 | int n2 = p->GetObject(); // 50 35 | 36 | 37 | 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | // 49 | -------------------------------------------------------------------------------- /5030/iterator2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Node 5 | { 6 | T data; 7 | Node* next; 8 | Node( const T& d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | /* 12 | // 반복자의 규칙- C#1.0 13 | struct IEnumerator 14 | { 15 | virtual ~IEnumerator() {} 16 | virtual bool MoveNext() = 0; 17 | virtual Object* GetObject() = 0; 18 | }; 19 | */ 20 | 21 | // C# 2.0 22 | template struct IEnumerator 23 | { 24 | virtual ~IEnumerator() {} 25 | virtual bool MoveNext() = 0; 26 | virtual T& GetObject() = 0; 27 | }; 28 | 29 | // slist 반복자 30 | template class SlistEnumerator : public IEnumerator 31 | { 32 | Node* current = 0; 33 | public: 34 | SlistEnumerator( Node* p = 0) : current(p) {} 35 | 36 | virtual bool MoveNext() 37 | { 38 | current = current->next; 39 | return current; 40 | } 41 | virtual T& GetObject() { return current->data; } 42 | }; 43 | 44 | 45 | // 모든 컨테이너에서는 반복자를 꺼낼수 있어야 한다. 46 | // 컨테이너가 지켜야 하는 인터페이스 47 | template struct IEnumerable 48 | { 49 | virtual ~IEnumerable() {} 50 | virtual IEnumerator* GetEnumerator() = 0; 51 | }; 52 | 53 | 54 | template class slist : public IEnumerable 55 | { 56 | Node* head = 0; 57 | public: 58 | virtual IEnumerator* GetEnumerator() 59 | { 60 | return new SlistEnumerator( head); 61 | } 62 | 63 | void push_front(const T& n) { head = new Node(n, head);} 64 | T front() { return head->data;} 65 | }; 66 | 67 | int main() 68 | { 69 | slist s; 70 | 71 | s.push_front(10); 72 | s.push_front(20); 73 | s.push_front(30); 74 | s.push_front(40); 75 | 76 | IEnumerator* p = s.GetEnumerator(); 77 | 78 | int n = p->GetObject(); // 40 79 | cout << n << endl; 80 | p->MoveNext(); // 81 | 82 | cout << p->GetObject() << endl; // 30 83 | } 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | // 94 | -------------------------------------------------------------------------------- /5030/iterator3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Node 5 | { 6 | T data; 7 | Node* next; 8 | Node( const T& d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | template struct IEnumerator 12 | { 13 | virtual ~IEnumerator() {} 14 | virtual bool MoveNext() = 0; 15 | virtual T& GetObject() = 0; 16 | }; 17 | 18 | 19 | template class SlistEnumerator : public IEnumerator 20 | { 21 | Node* current = 0; 22 | public: 23 | SlistEnumerator( Node* p = 0) : current(p) {} 24 | 25 | virtual bool MoveNext() 26 | { 27 | current = current->next; 28 | return current; 29 | } 30 | virtual T& GetObject() { return current->data; } 31 | }; 32 | 33 | template struct IEnumerable 34 | { 35 | virtual ~IEnumerable() {} 36 | virtual IEnumerator* GetEnumerator() = 0; 37 | }; 38 | 39 | 40 | template class slist : public IEnumerable 41 | { 42 | Node* head = 0; 43 | public: 44 | virtual IEnumerator* GetEnumerator() 45 | { 46 | return new SlistEnumerator( head); 47 | } 48 | 49 | void push_front(const T& n) { head = new Node(n, head);} 50 | T front() { return head->data;} 51 | }; 52 | 53 | 54 | template void Show( IEnumerator* p ) 55 | { 56 | do 57 | { 58 | cout << p->GetObject() << endl; 59 | } while( p->MoveNext() ); 60 | } 61 | 62 | int main() 63 | { 64 | int x[10] = {1,2,3,4,5,6,7,8,9,10}; 65 | int* p1 = x; 66 | Show( p1); 67 | 68 | slist s; 69 | 70 | s.push_front(10); 71 | s.push_front(20); 72 | s.push_front(30); 73 | s.push_front(40); 74 | 75 | IEnumerator* p = s.GetEnumerator(); 76 | 77 | Show( p ); 78 | delete p; 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | // 90 | -------------------------------------------------------------------------------- /5030/iterator4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Node 5 | { 6 | T data; 7 | Node* next; 8 | Node( const T& d, Node* n) : data(d), next(n) {} 9 | }; 10 | 11 | 12 | 13 | 14 | template class slist_iterator 15 | { 16 | Node* current = 0; 17 | public: 18 | inline slist_iterator( Node* p = 0) : current(p) {} 19 | 20 | inline slist_iterator& operator++() 21 | { 22 | current = current->next; 23 | return *this; 24 | } 25 | inline T& operator*() { return current->data; } 26 | }; 27 | // ++p, *p 28 | 29 | 30 | 31 | template class slist 32 | { 33 | Node* head = 0; 34 | public: 35 | slist_iterator begin() 36 | { 37 | return slist_iterator( head); 38 | } 39 | 40 | void push_front(const T& n) { head = new Node(n, head);} 41 | T front() { return head->data;} 42 | }; 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | template void Show( T p, T p2 ) 52 | { 53 | do 54 | { 55 | cout << *p << endl; 56 | } while( ++p != p2); 57 | } 58 | 59 | int main() 60 | { 61 | int x[10] = {1,2,3,4,5,6,7,8,9,10}; 62 | int* p1 = x; 63 | Show( p1, x+10); 64 | 65 | slist s; 66 | 67 | s.push_front(10); 68 | s.push_front(20); 69 | s.push_front(30); 70 | s.push_front(40); 71 | 72 | slist_iterator p = s.begin(); 73 | 74 | cout << *p << endl; 75 | ++p; 76 | cout << *p << endl; // 30 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | // 88 | -------------------------------------------------------------------------------- /5030/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /5040/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /5040/visitor1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | list s = { 1,2,3,4,5,6,7,8,9,10}; 8 | 9 | // 모든 요소를 2배로 만들고 싶다. 10 | // 방법1. 외부에서 직접 연산 수행. 11 | for ( auto& n : s ) 12 | n = n * 2; 13 | 14 | // 방법 2. 멤버 함수로 기능을 제공 15 | s.twice_all_element(); 16 | s.show_all_element(); 17 | 18 | // 방법 3. 방문자 패턴을 사용한다. 19 | TwiceVisitor tv; // 방문자. 20 | s.accept(&tv); 21 | 22 | ShowVisitor sv; // 방문자. 23 | s.accept(&sv); 24 | 25 | } 26 | 27 | 28 | 29 | 30 | 31 | // 32 | -------------------------------------------------------------------------------- /5040/visitor2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 방문자(visitor)의 인터페이스 6 | template struct IVisitor 7 | { 8 | virtual void visit(T& elem) = 0; 9 | virtual ~IVisitor() {} 10 | }; 11 | 12 | template class TwiceVisitor : public IVisitor 13 | { 14 | public: 15 | virtual void visit(T& elem) { elem = elem * 2;} 16 | }; 17 | 18 | template class ShowVisitor : public IVisitor 19 | { 20 | public: 21 | virtual void visit(T& elem) { cout << elem << endl;} 22 | }; 23 | //------------------------ 24 | // 방문의 대상의 인터페이스 25 | template struct IAcceptor 26 | { 27 | virtual void accept( IVisitor* p) = 0; 28 | virtual ~IAcceptor() {} 29 | }; 30 | 31 | template class List : public list, public IAcceptor 32 | { 33 | public: 34 | using list::list; // c++11 생성자 상속 35 | virtual void accept( IVisitor* p) 36 | { 37 | // 모든 요소를 방문자에게 전달. 38 | for( auto& e : *this) 39 | p->visit(e); 40 | } 41 | }; 42 | 43 | template class TripleVisitor : public IVisitor 44 | { 45 | public: 46 | virtual void visit(T& elem) { elem = elem * 3;} 47 | }; 48 | 49 | int main() 50 | { 51 | List s = { 1,2,3,4,5,6,7,8,9,10}; 52 | 53 | TwiceVisitor tv; 54 | s.accept(&tv); 55 | 56 | TripleVisitor trv; 57 | s.accept(&trv); 58 | 59 | ShowVisitor sv; 60 | s.accept(&sv); 61 | 62 | 63 | //s.triple_all_element(); 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | // 79 | -------------------------------------------------------------------------------- /5040/visitor3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | 9 | class BaseMenu 10 | { 11 | string title; 12 | public: 13 | BaseMenu( string s) : title(s) {} 14 | string getTitle() const { return title;} 15 | 16 | virtual void command() = 0; 17 | 18 | // 함수 추가 = 0 19 | }; 20 | 21 | class MenuItem : public BaseMenu 22 | { 23 | int id; 24 | public: 25 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 26 | 27 | virtual void command() 28 | { 29 | cout << getTitle() << endl; 30 | getchar(); 31 | } 32 | }; 33 | 34 | 35 | class PopupMenu : public BaseMenu 36 | { 37 | vector v; 38 | public: 39 | PopupMenu( string s) : BaseMenu(s) {} 40 | 41 | void addMenu(BaseMenu* p) { v.push_back(p);} 42 | 43 | virtual void command() 44 | { 45 | while( 1 ) 46 | { 47 | system("cls"); 48 | 49 | int sz = v.size(); 50 | 51 | for ( int i = 0; i < sz; i++) 52 | { 53 | cout << i + 1 << ". " << v[i]->getTitle() << endl; 54 | } 55 | 56 | cout << sz + 1 << ". << back " << endl; 57 | 58 | //------------------------------ 59 | int cmd; 60 | cout << "choose menu >> "; 61 | cin >> cmd; 62 | 63 | if ( cmd < 1 || cmd > sz + 1 ) // 잘못된 입력 64 | continue; 65 | 66 | if ( cmd == sz + 1 ) 67 | break; 68 | 69 | 70 | // 선택된 메뉴 실행.. 71 | v[cmd-1]-> command(); // 핵심.. ! 72 | } 73 | 74 | } 75 | }; 76 | 77 | 78 | int main() 79 | { 80 | PopupMenu* p1 = new PopupMenu("MENUBAR"); 81 | 82 | p1->addMenu( new PopupMenu("SCREEN")); 83 | p1->addMenu( new PopupMenu("SOUND")); 84 | p1->addMenu( new MenuItem("power off", 11)); 85 | 86 | // [ SCREEN ] 87 | // p1은 복합객체.. 88 | 89 | // 방법1. 타이틀을 변경하는 멤버 함수 90 | // 방법2. 방문자 패턴. 91 | MenuTitleChangeVisitor mtcv; 92 | p1->accept(&mtcv); 93 | 94 | //MenuColorChangeVisitor mccv; 95 | //p1->accept(&mccv ); 96 | p1->command(); 97 | 98 | } 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | // 123 | -------------------------------------------------------------------------------- /5040/visitor4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //--------------------------- 7 | class BaseMenu; 8 | class MenuItem; 9 | class PopupMenu; 10 | 11 | // 방문자의 인터페이스 12 | struct IMenuVisitor 13 | { 14 | virtual ~IMenuVisitor() {} 15 | 16 | virtual void visit(BaseMenu* p) = 0; 17 | virtual void visit(MenuItem* p) = 0; 18 | virtual void visit(PopupMenu* p) = 0; 19 | virtual void visit(PopupMenu* p) = 0; 20 | }; 21 | 22 | struct IAcceptor 23 | { 24 | virtual ~IAcceptor() {} 25 | virtual void accept(IMenuVisitor* p) = 0; 26 | }; 27 | //----------------------------------------------- 28 | 29 | class BaseMenu : public IAcceptor 30 | { 31 | string title; 32 | public: 33 | BaseMenu( string s) : title(s) {} 34 | void setTitle(string s) { title = s;} 35 | string getTitle() const { return title;} 36 | 37 | virtual void command() = 0; 38 | }; 39 | 40 | class MenuItem : public BaseMenu 41 | { 42 | int id; 43 | public: 44 | virtual void accept(IMenuVisitor* p) 45 | { 46 | p->visit(this); 47 | } 48 | 49 | MenuItem(string s, int n) : BaseMenu(s), id(n) {} 50 | 51 | virtual void command() 52 | { 53 | cout << getTitle() << endl; 54 | getchar(); 55 | } 56 | }; 57 | 58 | 59 | class PopupMenu : public BaseMenu 60 | { 61 | vector v; 62 | public: 63 | PopupMenu( string s) : BaseMenu(s) {} 64 | 65 | 66 | 67 | void addMenu(BaseMenu* p) { v.push_back(p);} 68 | 69 | virtual void command() 70 | { 71 | while( 1 ) 72 | { 73 | system("cls"); 74 | 75 | int sz = v.size(); 76 | 77 | for ( int i = 0; i < sz; i++) 78 | { 79 | cout << i + 1 << ". " << v[i]->getTitle() << endl; 80 | } 81 | 82 | cout << sz + 1 << ". << back " << endl; 83 | 84 | //------------------------------ 85 | int cmd; 86 | cout << "choose menu >> "; 87 | cin >> cmd; 88 | 89 | if ( cmd < 1 || cmd > sz + 1 ) // 잘못된 입력 90 | continue; 91 | 92 | if ( cmd == sz + 1 ) 93 | break; 94 | 95 | 96 | // 선택된 메뉴 실행.. 97 | v[cmd-1]-> command(); // 핵심.. ! 98 | } 99 | 100 | } 101 | virtual void accept(IMenuVisitor* p) 102 | { 103 | p->visit(this); 104 | 105 | for ( auto m : v) 106 | m->accept(p); 107 | } 108 | }; 109 | 110 | class MenuTitleChangeVisitor : public IMenuVisitor 111 | { 112 | public: 113 | virtual void visit(BaseMenu* p) {} 114 | virtual void visit(MenuItem* p) {} 115 | virtual void visit(PopupMenu* p) 116 | { 117 | // popupmenu 의 타이틀을 변경한다. 118 | string s = p->getTitle(); 119 | s = "[ " + s + " ]"; 120 | 121 | p->setTitle(s); 122 | } 123 | }; 124 | 125 | class EraseTitleChangeVisitor : public IMenuVisitor 126 | { 127 | public: 128 | virtual void visit(BaseMenu* p) {} 129 | virtual void visit(MenuItem* p) {} 130 | virtual void visit(PopupMenu* p) 131 | { 132 | p->setTitle("..."); 133 | } 134 | }; 135 | 136 | int main() 137 | { 138 | PopupMenu* p1 = new PopupMenu("MENUBAR"); 139 | 140 | p1->addMenu( new PopupMenu("SCREEN")); 141 | p1->addMenu( new PopupMenu("SOUND")); 142 | p1->addMenu( new MenuItem("power off", 11)); 143 | 144 | //--------------------- 145 | MenuTitleChangeVisitor mtcv; 146 | p1->accept(&mtcv); 147 | 148 | EraseTitleChangeVisitor etcv; 149 | p1->accept(&etcv); 150 | // 1. 메뉴 (복합객체)는 accept 가 필요 151 | // 2. 방문자 인터페이스 필요.. 152 | 153 | p1->command(); 154 | } 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | // 180 | -------------------------------------------------------------------------------- /6010/create1.cpp: -------------------------------------------------------------------------------- 1 | class Shape 2 | { 3 | public: 4 | virtual ~Shape() {} 5 | }; 6 | 7 | class Rect : public Shape 8 | { 9 | public: 10 | }; 11 | class Circle : public Shape 12 | { 13 | public: 14 | }; 15 | 16 | int main() 17 | { 18 | Rect r; 19 | 20 | Shape* p = new Rect; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /6010/create2.cpp: -------------------------------------------------------------------------------- 1 | class Shape 2 | { 3 | public: 4 | virtual ~Shape() {} 5 | }; 6 | 7 | class Rect : public Shape 8 | { 9 | Rect() {} // private 생성자. 10 | public: 11 | static Shape* Create() { return new Rect;} 12 | }; 13 | class Circle : public Shape 14 | { 15 | Circle() {} 16 | public: 17 | static Shape* Create() { return new Circle;} 18 | }; 19 | 20 | // 도형을 만들어서 그림을 그리는 함수 21 | //void CreateAndDraw(string s) 22 | //void CreateAndDraw(int type) 23 | void CreateAndDraw(Shape* (*f)() ) 24 | { 25 | // .. 26 | // new s; 27 | // if ( type == 1 ) new Rect; 28 | 29 | Shape* p = f(); 30 | p->Draw(); 31 | } 32 | 33 | 34 | int main() 35 | { 36 | CreateAndDraw(&Rect::Create ); 37 | //CreateAndDraw("Rect"); 38 | 39 | //Shape* p = Rect::Create(); 40 | 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | // 52 | -------------------------------------------------------------------------------- /6010/create3.cpp: -------------------------------------------------------------------------------- 1 | class Shape 2 | { 3 | public: 4 | virtual ~Shape() {} 5 | }; 6 | 7 | class Rect : public Shape 8 | { 9 | Rect() {} 10 | public: 11 | friend class ShapeFactory; 12 | }; 13 | 14 | class Circle : public Shape 15 | { 16 | Circle() {} 17 | public: 18 | friend class ShapeFactory; 19 | }; 20 | 21 | class ShapeFactory 22 | { 23 | public: 24 | Shape* CreateShape( int type ) 25 | { 26 | Shape* p = 0; 27 | switch( type ) 28 | { 29 | case 1: p = new Rect; break; 30 | case 2: p = new Circle; break; 31 | } 32 | return p; 33 | } 34 | }; 35 | 36 | int main() 37 | { 38 | ShapeFactory factory; 39 | Shape* p = factory.CreateShape(1); 40 | 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | // 57 | -------------------------------------------------------------------------------- /6010/create4.cpp: -------------------------------------------------------------------------------- 1 | class Shape 2 | { 3 | public: 4 | virtual ~Shape() {} 5 | 6 | virtual Shape* Clone() = 0; 7 | }; 8 | 9 | class Rect : public Shape 10 | { 11 | public: 12 | virtual Shape* Clone() { return new Rect(*this);} 13 | }; 14 | class Circle : public Shape 15 | { 16 | public: 17 | virtual Shape* Clone() { return new Circle(*this);} 18 | }; 19 | 20 | int main() 21 | { 22 | Shape* p = new Rect; 23 | 24 | Shape* p2 = p->Clone(); 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | // 37 | -------------------------------------------------------------------------------- /6010/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /6020/Singleton.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_UTILS_SINGLETON_H 18 | #define ANDROID_UTILS_SINGLETON_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | namespace android { 27 | // --------------------------------------------------------------------------- 28 | 29 | // Singleton may be used in multiple libraries, only one of which should 30 | // define the static member variables using ANDROID_SINGLETON_STATIC_INSTANCE. 31 | // Turn off -Wundefined-var-template so other users don't get: 32 | // instantiation of variable 'android::Singleton::sLock' required here, 33 | // but no definition is available 34 | #if defined(__clang__) 35 | #pragma clang diagnostic push 36 | #pragma clang diagnostic ignored "-Wundefined-var-template" 37 | #endif 38 | 39 | template 40 | class ANDROID_API Singleton 41 | { 42 | public: 43 | static TYPE& getInstance() { 44 | Mutex::Autolock _l(sLock); 45 | TYPE* instance = sInstance; 46 | if (instance == 0) { 47 | instance = new TYPE(); 48 | sInstance = instance; 49 | } 50 | return *instance; 51 | } 52 | 53 | static bool hasInstance() { 54 | Mutex::Autolock _l(sLock); 55 | return sInstance != 0; 56 | } 57 | 58 | protected: 59 | ~Singleton() { } 60 | Singleton() { } 61 | 62 | private: 63 | Singleton(const Singleton&); 64 | Singleton& operator = (const Singleton&); 65 | static Mutex sLock; 66 | static TYPE* sInstance; 67 | }; 68 | 69 | #if defined(__clang__) 70 | #pragma clang diagnostic pop 71 | #endif 72 | 73 | /* 74 | * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file 75 | * (eg: .cpp) to create the static instance of Singleton<>'s attributes, 76 | * and avoid to have a copy of them in each compilation units Singleton 77 | * is used. 78 | * NOTE: we use a version of Mutex ctor that takes a parameter, because 79 | * for some unknown reason using the default ctor doesn't emit the variable! 80 | */ 81 | 82 | #define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \ 83 | template<> ::android::Mutex \ 84 | (::android::Singleton< TYPE >::sLock)(::android::Mutex::PRIVATE); \ 85 | template<> TYPE* ::android::Singleton< TYPE >::sInstance(0); \ 86 | template class ::android::Singleton< TYPE >; 87 | 88 | 89 | // --------------------------------------------------------------------------- 90 | }; // namespace android 91 | 92 | #endif // ANDROID_UTILS_SINGLETON_H 93 | 94 | -------------------------------------------------------------------------------- /6020/singleton.hpp: -------------------------------------------------------------------------------- 1 | // singleton.hpp 2 | 3 | #define MAKE_SINGLETON(classname) \ 4 | private: \ 5 | classname() { } \ 6 | classname(const classname&) = delete; \ 7 | void operator=(const classname&) = delete; \ 8 | public: \ 9 | static classname& getInstance() \ 10 | { \ 11 | static classname instance; \ 12 | return instance; \ 13 | } 14 | 15 | 16 | template class Singleton 17 | { 18 | protected: 19 | Singleton() { } 20 | Singleton(const Singleton&) = delete; 21 | void operator=(const Singleton&) = delete; 22 | public: 23 | static TYPE& getInstance() 24 | { 25 | static TYPE instance; 26 | return instance; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /6020/singleton1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Cursor 5 | { 6 | int x, y; 7 | 8 | private: 9 | Cursor() { } 10 | Cursor(const Cursor&) = delete; 11 | void operator=(const Cursor&) = delete; 12 | 13 | public: 14 | static Cursor& getInstance() 15 | { 16 | static Cursor instance; 17 | return instance; 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | //Cursor c1, c2; 24 | Cursor& c1 = Cursor::getInstance(); 25 | Cursor& c2 = Cursor::getInstance(); 26 | cout << &c1 << endl; 27 | cout << &c2 << endl; 28 | 29 | //Cursor c3 = c1; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | // 42 | -------------------------------------------------------------------------------- /6020/singleton2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | class Cursor 6 | { 7 | int x, y; 8 | 9 | private: 10 | Cursor() { cout << "Cursor()" << endl; } 11 | Cursor(const Cursor&) = delete; 12 | void operator=(const Cursor&) = delete; 13 | 14 | static Cursor instance; 15 | public: 16 | static Cursor& getInstance() 17 | { 18 | return instance; 19 | } 20 | }; 21 | Cursor Cursor::instance; 22 | */ 23 | /* 24 | class Cursor 25 | { 26 | int x, y; 27 | 28 | private: 29 | Cursor() { cout << "Cursor()" << endl; } 30 | Cursor(const Cursor&) = delete; 31 | void operator=(const Cursor&) = delete; 32 | 33 | public: 34 | // lazy intialization 35 | static Cursor& getInstance() 36 | { 37 | static Cursor instance; 38 | return instance; 39 | } 40 | }; 41 | */ 42 | 43 | class Cursor 44 | { 45 | int x, y; 46 | 47 | private: 48 | Cursor() { cout << "Cursor()" << endl; } 49 | Cursor(const Cursor&) = delete; 50 | void operator=(const Cursor&) = delete; 51 | 52 | static Cursor* pInstance; 53 | public: 54 | static Cursor& getInstance() 55 | { 56 | if ( pInstance == 0 ) 57 | pInstance = new Cursor; 58 | return *pInstance; 59 | } 60 | }; 61 | Cursor* Cursor::pInstance = 0; 62 | 63 | int main() 64 | { 65 | Cursor& c1 = Cursor::getInstance(); 66 | 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | // 79 | -------------------------------------------------------------------------------- /6020/singleton3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Cursor 6 | { 7 | private: 8 | Cursor() { } 9 | Cursor(const Cursor&) = delete; 10 | void operator=(const Cursor&) = delete; 11 | 12 | static mutex m; 13 | static Cursor* pInstance; 14 | public: 15 | 16 | 17 | static Cursor& getInstance() 18 | { 19 | // 단점 : 최초 생성시 if 를 2번 실행 20 | // 장점 : 최초 이외에는 Lock 을 수행하지 않는다. 21 | if ( pInstance == 0 ) 22 | { 23 | m.lock(); 24 | if ( pInstance == 0 ) 25 | pInstance = new Cursor; 26 | m.unlock(); 27 | } 28 | 29 | return *pInstance; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | }; 37 | Cursor* Cursor::pInstance = 0; 38 | mutex Cursor::m; 39 | 40 | 41 | int main() 42 | { 43 | Cursor& c1 = Cursor::getInstance(); 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | // 56 | -------------------------------------------------------------------------------- /6020/singleton4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Cursor 6 | { 7 | private: 8 | Cursor() { } 9 | Cursor(const Cursor&) = delete; 10 | void operator=(const Cursor&) = delete; 11 | 12 | static mutex m; 13 | static Cursor* pInstance; 14 | public: 15 | 16 | 17 | static Cursor& getInstance() 18 | { 19 | // 단점 : 최초 생성시 if 를 2번 실행 20 | // 장점 : 최초 이외에는 Lock 을 수행하지 않는다. 21 | if ( pInstance == 0 ) 22 | { 23 | m.lock(); 24 | if ( pInstance == 0 ) 25 | { 26 | pInstance = new Cursor; 27 | 28 | // 1. temp = sizeof(Cursor) 메모리 할당. 29 | // 2. Cursor::Cursor() 생성자 호출 30 | // 3. pInstance = temp; 31 | 32 | // 1. pInstance = sizeof(Cursor) 메모리 할당. 33 | // 3. pInstance = temp; 34 | // 2. Cursor::Cursor() 생성자 호출 35 | 36 | } 37 | 38 | m.unlock(); 39 | } 40 | 41 | return *pInstance; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | }; 51 | Cursor* Cursor::pInstance = 0; 52 | mutex Cursor::m; 53 | 54 | 55 | int main() 56 | { 57 | Cursor& c1 = Cursor::getInstance(); 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | // 70 | -------------------------------------------------------------------------------- /6020/singleton5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Cursor 7 | { 8 | private: 9 | Cursor() { } 10 | Cursor(const Cursor&) = delete; 11 | void operator=(const Cursor&) = delete; 12 | 13 | static atomic m_instance; 14 | static mutex m_mutex; 15 | public: 16 | static Cursor& getInstance() 17 | { 18 | Cursor* tmp = m_instance.load(); 19 | if (tmp == nullptr) { 20 | lock_guard lock(m_mutex); 21 | tmp = m_instance.load(); 22 | if (tmp == nullptr) { 23 | tmp = new Cursor; 24 | m_instance.store(tmp); 25 | } 26 | } 27 | return *tmp; 28 | 29 | } 30 | }; 31 | atomic Cursor::m_instance; 32 | mutex Cursor::m_mutex; 33 | 34 | int main() 35 | { 36 | Cursor& c1 = Cursor::getInstance(); 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | // 49 | -------------------------------------------------------------------------------- /6020/singleton6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define MAKE_SINGLETON(classname) \ 6 | private: \ 7 | classname() { } \ 8 | classname(const classname&) = delete; \ 9 | void operator=(const classname&) = delete; \ 10 | public: \ 11 | static classname& getInstance() \ 12 | { \ 13 | static classname instance; \ 14 | return instance; \ 15 | } 16 | 17 | 18 | 19 | class Cursor 20 | { 21 | int x, y; 22 | 23 | MAKE_SINGLETON(Cursor) 24 | }; 25 | 26 | int main() 27 | { 28 | Cursor& c1 = Cursor::getInstance(); 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | // 44 | -------------------------------------------------------------------------------- /6020/singleton7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // CRTP : Curiously Recurring Template Pattern 5 | template class Singleton 6 | { 7 | protected: 8 | Singleton() { } 9 | Singleton(const Singleton&) = delete; 10 | void operator=(const Singleton&) = delete; 11 | public: 12 | static TYPE& getInstance() 13 | { 14 | static TYPE instance; 15 | return instance; 16 | } 17 | }; 18 | 19 | class Mouse : public Singleton< Mouse > 20 | { 21 | 22 | }; 23 | 24 | int main() 25 | { 26 | Mouse& c1 = Mouse::getInstance(); 27 | 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | // 40 | -------------------------------------------------------------------------------- /6020/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /6030/factory1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Shape 6 | { 7 | public: 8 | virtual void Draw() { cout << "Draw Shape" << endl;} 9 | }; 10 | 11 | class Rect : public Shape 12 | { 13 | public: 14 | virtual void Draw() { cout << "Draw Rect" << endl;} 15 | }; 16 | 17 | class Circle : public Shape 18 | { 19 | public: 20 | virtual void Draw() { cout << "Circle Rect" << endl;} 21 | }; 22 | 23 | int main() 24 | { 25 | vector v; 26 | 27 | while( 1 ) 28 | { 29 | int cmd; 30 | cin >> cmd; 31 | 32 | // 33 | if ( cmd == 1 ) v.push_back( new Rect ); 34 | else if ( cmd == 2 ) v.push_back( new Circle ); 35 | else if ( cmd == 9 ) 36 | { 37 | for ( auto p : v ) 38 | p->Draw(); // 다형성 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 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | // 75 | -------------------------------------------------------------------------------- /6030/factory2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Singleton.hpp" 4 | using namespace std; 5 | 6 | class Shape 7 | { 8 | public: 9 | virtual void Draw() { cout << "Draw Shape" << endl;} 10 | }; 11 | 12 | class Rect : public Shape 13 | { 14 | public: 15 | virtual void Draw() { cout << "Draw Rect" << endl;} 16 | }; 17 | 18 | class Circle : public Shape 19 | { 20 | public: 21 | virtual void Draw() { cout << "Circle Rect" << endl;} 22 | }; 23 | 24 | class ShapeFactory 25 | { 26 | MAKE_SINGLETON(ShapeFactory) 27 | public: 28 | Shape* CreateShape(int type ) 29 | { 30 | Shape* p = 0; 31 | if ( type == 1 ) p = new Rect; 32 | else if ( type == 2 ) p = new Circle; 33 | 34 | return p; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | ShapeFactory& factory = ShapeFactory::getInstance(); 41 | vector v; 42 | 43 | while( 1 ) 44 | { 45 | int cmd; 46 | cin >> cmd; 47 | 48 | // 49 | if ( cmd >=1 && cmd <= 5 ) 50 | { 51 | Shape* p = factory.CreateShape(cmd); 52 | 53 | if ( p != 0 ) 54 | v.push_back( p ); 55 | } 56 | else if ( cmd == 9 ) 57 | { 58 | for ( auto p : v ) 59 | p->Draw(); // 다형성 60 | } 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | // 96 | -------------------------------------------------------------------------------- /6030/factory3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Singleton.hpp" 5 | using namespace std; 6 | 7 | class Shape 8 | { 9 | public: 10 | virtual void Draw() { cout << "Draw Shape" << endl;} 11 | }; 12 | 13 | class Rect : public Shape 14 | { 15 | public: 16 | virtual void Draw() { cout << "Draw Rect" << endl;} 17 | 18 | static Shape* Create() { return new Rect;} 19 | }; 20 | 21 | class Circle : public Shape 22 | { 23 | public: 24 | virtual void Draw() { cout << "Circle Rect" << endl;} 25 | 26 | static Shape* Create() { return new Circle;} 27 | }; 28 | 29 | 30 | // new Circle; 31 | // Circle::Create() 32 | 33 | 34 | 35 | 36 | class ShapeFactory 37 | { 38 | MAKE_SINGLETON(ShapeFactory) 39 | 40 | typedef Shape* (*CREATOR)(); 41 | 42 | map create_map; 43 | 44 | public: 45 | void Register( int type, CREATOR f ) 46 | { 47 | create_map[type] = f; 48 | } 49 | 50 | Shape* CreateShape(int type ) 51 | { 52 | Shape* p = 0; 53 | auto ret = create_map.find( type ); 54 | if ( ret == create_map.end() ) 55 | return 0; 56 | p = create_map[type](); 57 | 58 | return p; 59 | } 60 | }; 61 | 62 | 63 | 64 | int main() 65 | { 66 | ShapeFactory& factory = ShapeFactory::getInstance(); 67 | 68 | // 공장에 제품을 등록한다. 69 | factory.Register( 1, &Rect::Create); 70 | factory.Register( 2, &Circle::Create); 71 | 72 | //factory.ShowProduct(); 73 | 74 | vector v; 75 | 76 | while( 1 ) 77 | { 78 | int cmd; 79 | cin >> cmd; 80 | 81 | // 82 | if ( cmd >=1 && cmd <= 5 ) 83 | { 84 | Shape* p = factory.CreateShape(cmd); 85 | 86 | if ( p != 0 ) 87 | v.push_back( p ); 88 | } 89 | else if ( cmd == 9 ) 90 | { 91 | for ( auto p : v ) 92 | p->Draw(); // 다형성 93 | } 94 | } 95 | } 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | // 129 | -------------------------------------------------------------------------------- /6030/factory4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Singleton.hpp" 5 | using namespace std; 6 | 7 | class Shape 8 | { 9 | public: 10 | virtual void Draw() { cout << "Draw Shape" << endl;} 11 | }; 12 | 13 | class ShapeFactory 14 | { 15 | MAKE_SINGLETON(ShapeFactory) 16 | typedef Shape* (*CREATOR)(); 17 | map create_map; 18 | public: 19 | void Register( int type, CREATOR f ) 20 | { 21 | create_map[type] = f; 22 | } 23 | 24 | Shape* CreateShape(int type ) 25 | { 26 | Shape* p = 0; 27 | auto ret = create_map.find( type ); 28 | if ( ret == create_map.end() ) 29 | return 0; 30 | p = create_map[type](); 31 | 32 | return p; 33 | } 34 | }; 35 | 36 | struct RegisterShape 37 | { 38 | RegisterShape( int type, Shape*(*f)() ) 39 | { 40 | ShapeFactory& factory = ShapeFactory::getInstance(); 41 | 42 | factory.Register(type, f); 43 | } 44 | }; 45 | //RegisterShape rs(1, &Rect::Create); 46 | 47 | 48 | 49 | class Rect : public Shape 50 | { 51 | public: 52 | virtual void Draw() { cout << "Draw Rect" << endl;} 53 | 54 | static Shape* Create() { return new Rect;} 55 | static RegisterShape rs; 56 | }; 57 | RegisterShape Rect::rs( 1, &Rect::Create); 58 | 59 | 60 | // 모든 도형이 지켜야 하는 규칙을 매크로로 제공 61 | #define DECLARE_SHAPE( classname ) \ 62 | static Shape* Create() { return new classname;} \ 63 | static RegisterShape rs; 64 | 65 | #define IMPLEMENT_SHAPE( type, classname ) \ 66 | RegisterShape classname::rs(type, &classname::Create); 67 | 68 | 69 | class Circle : public Shape 70 | { 71 | public: 72 | virtual void Draw() { cout << "Circle Rect" << endl;} 73 | 74 | DECLARE_SHAPE( Circle ) 75 | }; 76 | IMPLEMENT_SHAPE( 2, Circle ) 77 | 78 | 79 | class Triangle : public Shape 80 | { 81 | public: 82 | virtual void Draw() { cout << "Triangle Rect" << endl;} 83 | 84 | DECLARE_SHAPE( Triangle ) 85 | }; 86 | IMPLEMENT_SHAPE( 3, Triangle ) 87 | 88 | 89 | 90 | 91 | int main() 92 | { 93 | ShapeFactory& factory = ShapeFactory::getInstance(); 94 | 95 | 96 | vector v; 97 | 98 | while( 1 ) 99 | { 100 | int cmd; 101 | cin >> cmd; 102 | 103 | // 104 | if ( cmd >=1 && cmd <= 5 ) 105 | { 106 | Shape* p = factory.CreateShape(cmd); 107 | 108 | if ( p != 0 ) 109 | v.push_back( p ); 110 | } 111 | else if ( cmd == 9 ) 112 | { 113 | for ( auto p : v ) 114 | p->Draw(); // 다형성 115 | } 116 | } 117 | } 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | // 151 | -------------------------------------------------------------------------------- /6030/factory5.cpp: -------------------------------------------------------------------------------- 1 | // Factory3.cpp => Factory5.cpp 로 복사 2 | 3 | #include 4 | #include 5 | #include 6 | #include "Singleton.hpp" 7 | using namespace std; 8 | 9 | class Shape 10 | { 11 | public: 12 | virtual void Draw() { cout << "Draw Shape" << endl;} 13 | 14 | virtual Shape* Clone() = 0; 15 | }; 16 | 17 | class Rect : public Shape 18 | { 19 | public: 20 | virtual void Draw() { cout << "Draw Rect" << endl;} 21 | 22 | static Shape* Create() { return new Rect;} 23 | 24 | virtual Shape* Clone() { return new Rect(*this);} 25 | }; 26 | 27 | class Circle : public Shape 28 | { 29 | public: 30 | virtual void Draw() { cout << "Circle Rect" << endl;} 31 | 32 | static Shape* Create() { return new Circle;} 33 | 34 | virtual Shape* Clone() { return new Circle(*this);} 35 | }; 36 | 37 | 38 | 39 | class ShapeFactory 40 | { 41 | MAKE_SINGLETON(ShapeFactory) 42 | 43 | 44 | map protype_map; 45 | 46 | public: 47 | void Register( int type, Shape* sample ) 48 | { 49 | protype_map[type] = sample; 50 | } 51 | 52 | Shape* CreateShape(int type ) 53 | { 54 | Shape* p = 0; 55 | auto ret = protype_map.find( type ); 56 | if ( ret == protype_map.end() ) 57 | return 0; 58 | 59 | p = protype_map[type]->Clone(); 60 | 61 | return p; 62 | } 63 | }; 64 | 65 | 66 | 67 | int main() 68 | { 69 | ShapeFactory& factory = ShapeFactory::getInstance(); 70 | 71 | // 공장에 제품을 등록한다. 72 | // 클래스 등록 73 | //factory.Register( 1, &Rect::Create); 74 | //factory.Register( 2, &Circle::Create); 75 | 76 | Rect* r1 = new Rect;// 빨간색 크기 5 77 | Rect* r2 = new Rect;// 파란색 크기 10 78 | 79 | // 공장에 객체 등록 80 | factory.Register( 1, r1); 81 | factory.Register( 2, r2); 82 | 83 | //factory.ShowProduct(); 84 | 85 | 86 | 87 | vector v; 88 | 89 | while( 1 ) 90 | { 91 | int cmd; 92 | cin >> cmd; 93 | 94 | // 95 | if ( cmd >=1 && cmd <= 5 ) 96 | { 97 | Shape* p = factory.CreateShape(cmd); 98 | 99 | if ( p != 0 ) 100 | v.push_back( p ); 101 | } 102 | else if ( cmd == 9 ) 103 | { 104 | for ( auto p : v ) 105 | p->Draw(); // 다형성 106 | } 107 | } 108 | } 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | // 142 | -------------------------------------------------------------------------------- /6030/singleton.hpp: -------------------------------------------------------------------------------- 1 | // singleton.hpp 2 | 3 | #define MAKE_SINGLETON(classname) \ 4 | private: \ 5 | classname() { } \ 6 | classname(const classname&) = delete; \ 7 | void operator=(const classname&) = delete; \ 8 | public: \ 9 | static classname& getInstance() \ 10 | { \ 11 | static classname instance; \ 12 | return instance; \ 13 | } 14 | 15 | 16 | template class Singleton 17 | { 18 | protected: 19 | Singleton() { } 20 | Singleton(const Singleton&) = delete; 21 | void operator=(const Singleton&) = delete; 22 | public: 23 | static TYPE& getInstance() 24 | { 25 | static TYPE instance; 26 | return instance; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /6030/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /6040/abfactory1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct IEdit 6 | { 7 | virtual void Draw() = 0; 8 | virtual ~IEdit() {} 9 | }; 10 | struct IButton 11 | { 12 | virtual void Draw() = 0; 13 | virtual ~IButton() {} 14 | }; 15 | 16 | struct WinButton : public IButton { void Draw() { cout << "Draw WinButton" << endl;}}; 17 | struct GTKButton : public IButton{ void Draw() { cout << "Draw GTKButton" << endl;}}; 18 | 19 | struct WinEdit : public IEdit { void Draw() { cout << "Draw WinEdit" << endl;}}; 20 | struct GTKEdit : public IEdit { void Draw() { cout << "Draw GTKEdit" << endl;}}; 21 | 22 | 23 | int main(int argv, char** argc) 24 | { 25 | IButton* pBtn; 26 | if ( strcmp(argc[1], "Windows") == 0) 27 | pBtn = new WinButton; 28 | else 29 | pBtn = new GTKButton; 30 | 31 | pBtn->Draw(); 32 | } 33 | -------------------------------------------------------------------------------- /6040/abfactory2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct IEdit 6 | { 7 | virtual void Draw() = 0; 8 | virtual ~IEdit() {} 9 | }; 10 | 11 | struct IButton 12 | { 13 | virtual void Draw() = 0; 14 | virtual ~IButton() {} 15 | }; 16 | 17 | struct WinButton : public IButton { void Draw() { cout << "Draw WinButton" << endl;}}; 18 | struct GTKButton : public IButton{ void Draw() { cout << "Draw GTKButton" << endl;}}; 19 | 20 | struct WinEdit : public IEdit { void Draw() { cout << "Draw WinEdit" << endl;}}; 21 | struct GTKEdit : public IEdit { void Draw() { cout << "Draw GTKEdit" << endl;}}; 22 | 23 | //------------------------------------- 24 | // Factory 의 공통의 기반 클래스 25 | struct IFactory 26 | { 27 | virtual IButton* CreateButton() = 0; 28 | virtual IEdit* CreateEdit() = 0; 29 | virtual ~IFactory() {} 30 | }; 31 | 32 | struct WinFactory : public IFactory 33 | { 34 | IButton* CreateButton() { return new WinButton;} 35 | IEdit* CreateEdit() { return new WinEdit;} 36 | }; 37 | 38 | struct GTKFactory : public IFactory 39 | { 40 | IButton* CreateButton() { return new GTKButton;} 41 | IEdit* CreateEdit() { return new GTKEdit;} 42 | }; 43 | 44 | int main(int argv, char** argc) 45 | { 46 | IFactory* pFactory; 47 | if ( strcmp(argc[1], "Windows") == 0) 48 | pFactory = new WinFactory; 49 | else 50 | pFactory = new GTKFactory; 51 | 52 | 53 | IButton* pBtn = pFactory->CreateButton(); 54 | pBtn->Draw(); 55 | 56 | 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | // 67 | -------------------------------------------------------------------------------- /6040/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /6050/fbmethod1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct IEdit 5 | { 6 | virtual void Draw() = 0; 7 | virtual ~IEdit() {} 8 | }; 9 | struct IButton 10 | { 11 | virtual void Draw() = 0; 12 | virtual ~IButton() {} 13 | }; 14 | 15 | struct WinButton : public IButton { void Draw() { cout << "Draw WinButton" << endl;}}; 16 | struct GTKButton : public IButton { void Draw() { cout << "Draw GTKButton" << endl;}}; 17 | 18 | struct WinEdit : public IEdit { void Draw() { cout << "Draw WinEdit" << endl;}}; 19 | struct GTKEdit : public IEdit { void Draw() { cout << "Draw GTKEdit" << endl;}}; 20 | 21 | //--------------------------------------------------- 22 | // -style 옵션에 상관없이 무조건 windows 스타일 사용 23 | class WinDialog 24 | { 25 | public: 26 | void Init() 27 | { 28 | WinButton* pBtn = new WinButton; 29 | WinEdit* pEdit = new WinEdit; 30 | 31 | //pBtn->Move(); 32 | //pEdit->Move(); 33 | 34 | pBtn->Draw(); 35 | pEdit->Draw(); 36 | } 37 | }; 38 | 39 | class GTKDialog 40 | { 41 | public: 42 | void Init() 43 | { 44 | GTKButton* pBtn = new GTKButton; 45 | GTKEdit* pEdit = new GTKEdit; 46 | 47 | //pBtn->Move(); 48 | //pEdit->Move(); 49 | 50 | pBtn->Draw(); 51 | pEdit->Draw(); 52 | } 53 | }; 54 | int main() 55 | { 56 | WinDialog dlg; 57 | dlg.Init(); 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | // 80 | -------------------------------------------------------------------------------- /6050/fbmethod2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct IEdit 5 | { 6 | virtual void Draw() = 0; 7 | virtual ~IEdit() {} 8 | }; 9 | struct IButton 10 | { 11 | virtual void Draw() = 0; 12 | virtual ~IButton() {} 13 | }; 14 | 15 | struct WinButton : public IButton { void Draw() { cout << "Draw WinButton" << endl;}}; 16 | struct GTKButton : public IButton { void Draw() { cout << "Draw GTKButton" << endl;}}; 17 | 18 | struct WinEdit : public IEdit { void Draw() { cout << "Draw WinEdit" << endl;}}; 19 | struct GTKEdit : public IEdit { void Draw() { cout << "Draw GTKEdit" << endl;}}; 20 | 21 | //--------------------------------------------------- 22 | 23 | class BaseDialog 24 | { 25 | public: 26 | void Init() 27 | { 28 | IButton* pBtn = CreateButton(); 29 | IEdit* pEdit = CreateEdit(); 30 | 31 | //pBtn->Move(); 32 | //pEdit->Move(); 33 | 34 | pBtn->Draw(); 35 | pEdit->Draw(); 36 | } 37 | virtual IButton* CreateButton() = 0; 38 | virtual IEdit* CreateEdit() = 0; 39 | }; 40 | 41 | 42 | class WinDialog : public BaseDialog 43 | { 44 | public: 45 | virtual IButton* CreateButton() { return new WinButton;} 46 | virtual IEdit* CreateEdit() { return new WinEdit;} 47 | }; 48 | 49 | class GTKDialog : public BaseDialog 50 | { 51 | public: 52 | virtual IButton* CreateButton() { return new GTKButton;} 53 | virtual IEdit* CreateEdit() { return new GTKEdit;} 54 | }; 55 | 56 | int main() 57 | { 58 | WinDialog dlg; 59 | dlg.Init(); 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | // 82 | -------------------------------------------------------------------------------- /6050/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /6060/builder1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 입학 지원서 6 | using Application = string; // class Application {} 7 | 8 | 9 | // 지원서 만드는 클래스 10 | class Director 11 | { 12 | string name = "HONG"; 13 | string phone = "010-111-1111"; 14 | string address = "SEOUL KANGNAMGU"; 15 | public: 16 | Application construct() 17 | { 18 | Application app; 19 | app += name + "\n"; 20 | app += phone + "\n"; 21 | app += address + "\n"; 22 | return app; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | Director d; 29 | // 전화, 이름 입력... 30 | Application app = d.construct(); 31 | cout << app << endl; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | // 40 | -------------------------------------------------------------------------------- /6060/builder2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 입학 지원서 6 | using Application = string; // class Application {} 7 | 8 | 9 | // 지원서 만드는 클래스 10 | class Director 11 | { 12 | string name = "HONG"; 13 | string phone = "010-111-1111"; 14 | string address = "SEOUL KANGNAMGU"; 15 | public: 16 | Application construct() 17 | { 18 | Application app; 19 | app += name + "\n"; 20 | app += phone + "\n"; 21 | //app += address + "\n"; 22 | return app; 23 | } 24 | Application XMLconstruct() 25 | { 26 | Application app; 27 | app += "" + name + "\n"; 28 | app += phone + "\n"; 29 | //app += address + "\n"; 30 | return app; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | Director d; 37 | // 전화, 이름 입력... 38 | Application app = d.construct(); 39 | cout << app << endl; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | // 48 | -------------------------------------------------------------------------------- /6060/builder3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 입학 지원서 6 | using Application = string; // class Application {} 7 | 8 | 9 | // 지원서의 각 단계의 표현을 만드는 빌더 인터페이스 10 | struct IBuilder 11 | { 12 | virtual ~IBuilder() {} 13 | virtual void makeName(string name) = 0; 14 | virtual void makePhone(string phone) = 0; 15 | virtual void makeAddress(string addr) = 0; 16 | 17 | virtual Application getResult() = 0; 18 | }; 19 | 20 | // 지원서 만드는 클래스 21 | class Director 22 | { 23 | string name = "HONG"; 24 | string phone = "010-111-1111"; 25 | string address = "SEOUL KANGNAMGU"; 26 | IBuilder* pBuilder; 27 | public: 28 | void setBuilder( IBuilder* p ) { pBuilder = p;} 29 | 30 | Application construct() 31 | { 32 | pBuilder->makeName(name); 33 | pBuilder->makePhone(phone); 34 | // pBuilder->makeAddress(address); 35 | 36 | return pBuilder->getResult(); 37 | } 38 | }; 39 | 40 | class XMLBuilder : public IBuilder 41 | { 42 | Application app; 43 | public: 44 | virtual void makeName(string name) 45 | { 46 | app += "" + name + "\n"; 47 | } 48 | virtual void makePhone(string phone) 49 | { 50 | app += "" + phone + "\n"; 51 | } 52 | virtual void makeAddress(string addr) 53 | { 54 | app += "
" + addr + "
\n"; 55 | } 56 | 57 | virtual Application getResult() { return app;} 58 | }; 59 | 60 | class TextBuilder : public IBuilder 61 | { 62 | Application app; 63 | public: 64 | virtual void makeName(string name) 65 | { 66 | app += name + "\n"; 67 | } 68 | virtual void makePhone(string phone) 69 | { 70 | app += phone + "\n"; 71 | } 72 | virtual void makeAddress(string addr) 73 | { 74 | app += addr + "\n"; 75 | } 76 | 77 | virtual Application getResult() { return app;} 78 | }; 79 | 80 | int main() 81 | { 82 | Director d; 83 | XMLBuilder xb; 84 | d.setBuilder(&xb); 85 | 86 | Application app = d.construct(); 87 | cout << app << endl; 88 | 89 | TextBuilder tb; 90 | d.setBuilder(&tb); 91 | app = d.construct(); 92 | cout << app << endl; 93 | } 94 | 95 | 96 | 97 | 98 | 99 | 100 | // 101 | -------------------------------------------------------------------------------- /6060/url: -------------------------------------------------------------------------------- 1 | ecourse.co.kr 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DP 2 | --------------------------------------------------------------------------------