Tiến tới tư duy lập trình hướng ₫ối tượng

21 231 0
Tiến tới tư duy lập trình hướng ₫ối tượng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

tài liệu c++

Kỹ thuật lập trình 0101010101010101100001 0101010101010101100001 0101010101010101100001 0101010100101010100101 0101010100101010100101 0101010100101010100101 1010011000110010010010 1010011000110010010010 1010011000110010010010 1100101100100010000010 1100101100100010000010 1100101100100010000010 0101010101010101100001 0101010101010101100001 0101010101010101100001 0101010100101010100101 0101010100101010100101 0101010100101010100101 1010011000110010010010 1010011000110010010010 1010011000110010010010 1100101100100010000010 1100101100100010000010 1100101100100010000010 0101010101010101100001 0101010101010101100001 0101010101010101100001 0101010100101010100101 0101010100101010100101 0101010100101010100101 1010011000110010010010 1010011000110010010010 1010011000110010010010 1100101100100010000010 1100101100100010000010 1100101100100010000010 8/13/2007 y = A*x + B*u; x = C*x + d*u; StateController start() stop() LQGController start() stop() Chương 8: Tiếntớitư duy lậptrình hướng ₫ốitượng 2 Chương 8: Tiếntớitư duy hướng đốitượng Nộidung chương 8 8.1 Đặtvấn ₫ề 8.2 Giớithiệuvídụ chương trình mô phỏng 8.3 duy "rất" cổ₫iển 8.4 duy hướng hàm 8.5 duy dựatrên₫ốitượng (object-based) 8.6 duy thựcsự hướng ₫ốitượng 3 Chương 8: Tiếntớitư duy hướng đốitượng 8.1 Đặtvấn ₫ề „Designing object-oriented software is hard, and designing reusable object-oriented software is even harder .It takes a long time for novices to learn what object-oriented design is all about. Exprienced designers evidently know something inexperienced ones don't . One thing expert designers know not to do is solve every problem from first principles. Rather, they reuse solutions that have worked for them in the past. When they find a good solution, they use it again and again. Such experience is part of what makes them experts. Consequently, you'll find recurring patterns of classes and communicating objects in many object-oriented systems. These patterns solve specific design problems and make object-oriented design more flexible, elegant, and ultimately reusable .“ Erich Gamma et. al.: Design Patterns: Elements of Reusable Object- Oriented Software , Addison-Wesley, 1995. 4 Chương 8: Tiếntớitư duy hướng đốitượng 8.2 Phầnmềmmôphỏng kiểuFBD StaticGain Limiter Integrator Sum Scope 1(t) Nhiệmvụ: Xây dựng phầnmềm ₫ể hỗ trợ mô phỏng thờigianthựcmột cách linh hoạt, mềmdẻo, ₫áp ứng ₫ượccácyêucầucủatừng bài toán cụ thể Trướcmắtchưacầnhỗ trợ tạo ứng dụng kiểukéothả bằng công cụ₫ồhọa 5 Chương 8: Tiếntớitư duy hướng đốitượng 8.3 duy rấtcổ₫iển // SimProg1.cpp #include <iostream.h> #include <conio.h> #include <windows.h> void main() { double K =1,I=0, Ti = 5; double Hi = 10, Lo = -10; double Ts = 0.5; double r =1, y=0, e, u, ub; cout << "u\ty"; while (!kbhit()) { e = r-y; // Sum block u = K*e; // Static Gain ub = max(min(u,Hi),Lo); // Limiter I += ub*Ts/Ti; // Integrator state y = I; // Integrator output cout << '\n' << u << '\t' << y; cout.flush(); Sleep(long(Ts*1000)); } } 6 Chương 8: Tiếntớitư duy hướng đốitượng Vấn ₫ề?  Phầnmềmdướidạng chương trình, không có giá trị sử dụng lại  Rấtkhóthay₫ổihoặcmở rộng theo yêu cầucụ thể củatừng bài toán  Toàn bộ thuật toán ₫ược gói trong mộtchương trình => khótheodõi, dễ gây lỗi, không bảovệ₫ượcchất xám 7 Chương 8: Tiếntớitư duy hướng đốitượng // SimProg2.cpp #include <iostream.h> #include <conio.h> #include <windows.h> #include "SimFun.h" void main() { double K = 5.0, double Ti = 5.0; double Hi = 10, Lo = -10; double Ts = 0.5; double r =1, y=0, e, u, ub; cout << "u\ty"; while (!kbhit()) { e = sum(r,-y); // Sum block u = gain(K,e); // Static Gain ub= limit(Hi,Lo,u); // Limiter y = integrate(Ti,Ts,ub); // Integrator output cout << '\n' << u << '\t' << y; cout.flush(); Sleep(long(Ts*1000)); } } 8.4 duy hướng hàm 8 Chương 8: Tiếntớitư duy hướng đốitượng // SimFun.h inline double sum(double x1, double x2) { return x1 + x2; } inline double gain(double K, double x) { return K * x; } double limit(double Hi, double Lo, double x); double integrate(double Ti, double Ts, double x); // SimFun.cpp double limit(double Hi, double Lo, double x) { if (x > Hi) x = Hi; if (x < Lo) x = Lo; return x; } double integrate(double Ti, double Ts, double x) { static double I = 0; I += x*Ts/Ti; return I; } 9 Chương 8: Tiếntớitư duy hướng đốitượng Vấn ₫ề?  Vẫnchưa ₫ủ tính linh hoạt, mềmdẻocầnthiết  Thay ₫ổi, mở rộng chương trình mô phỏng rấtkhó khăn  Các khâu có trạng thái như khâu tích phân, khâu trễ khó thựchiệnmộtcách"sạch sẽ" (trạng thái lưutrữ dướidạng nào?)  Rấtkhópháttriển thành phầnmềmcóhỗ trợ₫ồhọa kiểukéothả 10 Chương 8: Tiếntớitư duy hướng đốitượng 8.5 duy dựa ₫ốitượng // SimClass.h class Sum { public: double operator()(double x1, double x2) { return x1 + x2; } }; class Gain { double K; public: Gain(double k = 1) : K(k) {} double operator()(double x){ return K * x; } }; class Limiter { double Hi, Lo; public: Limiter(double h=10.0, double l= -10.0); double operator()(double x); };

Ngày đăng: 08/12/2013, 12:47

Tài liệu cùng người dùng

Tài liệu liên quan