OOP Design chinh phục
1. Giới thiệu
Làm sao để chinh phục được OOP, đó chính là design pattern nhưng mà để học design pattern chẳng phải là dễ.
Haizz phải học như nào đây ???
2. Chiến thôi
Finding Appropriate Objects
Determining Object Granularity
Specifying Object Interfaces
Specifying Object implementations
Class vs Interface inheritance
Programing to an Interface, not an implementations
Putting reuse mechanisms to work
Inheritance vs composition
Delegation
Inheritance vs Paramatized type
Relating run-time and compile time structures
Designing for change
Application program
Toolkits
Frameworks
3. Finding Appropriate Objects
Objects : data and operations
Client gửi request đến Objects --> Objects sẽ thực hiện operations : chỉ có cách này mới có thể thay đổi internal data của Objects
4. Determining Objects Granularity
5. Specifying Objects Interfaces
6. Specifying Object Implementations
6.1 Class vs
7. Putting reuse mechanisms to work
7.1 Inheritance and composition
//So sánh giữa composition and dependency
//Dependency là gì? là sự phụ thuộc vào một class khác để thực hiện 1 hành động
//Chẳng hạn như muốn thực hiện phương thức
// void Greeting(string str){
// console.log(str); }
//Chúng ta sử dụng hàm log của class console
//Có 3 dạng dependency
class Dependency{
A f(){
}
void f(A){
}
void f(){
A a;
}
}
//Composition : ngoài tác dụng như dependency, composition còn đóng vai trò là
//attributes của class
class Composition{
A a; //Attributes của class ---> Data
//Function sử dụng A sẽ có 2 mục đích chính
//Thay đổi giá trị của A ---> Controller
//Read-only giá trị của A ---> View
void f(){
}
}
//2 class composition có chung attributes : share chung dữ liệu
void main(){
Table tb = new Table();
StudyPlace sp1 = new StudyPlace();
StudyPlace sp2 = new StudyPlace();
sp1.attach(tb);
sp2.attach(tb);
}
//Composition and Inheritance
//Inner
class Sorter{
bool Condition(int v1, int v2){
return true;
}
void SortArray(int[] A, int N){
for(int i=0;i<N;i++)
for(int j =0;j<N;j++)
if(Condition(A[i], A[j])
HoanVi(A[i], A[j]);//Hoan đổi giá trị
}
}
void SorterA:Sorter{
override bool Condition(int v1, int v2){
return true;
}
}
//External class
class Sorter{
void SortArray(int[] A, int N,Condition condition){
for(int i=0;i<N;i++)
for(int j =0;j<N;j++)
if(condition.Check(A[i], A[j])
HoanVi(A[i], A[j]);//Hoan đổi giá trị
}
}
class Condition{
bool Check(int v1, int v2){
}
}
//Internal class: giao cho người trong nhà
//Extern class : giao cho người ngoài
7.2 Delegation
//Delegation là gì ?
//Delegation là ủy thác
//Khi nào nên delegation: vấn đề quá khó, không có thời gian làm, không chắc chắn
//Ví dụ: A trúng xố số 90 tỷ, sau 1 năm ăn chơi số tiền của A chỉ còn 50 Tỷ. A nhận
//ra là mình nên đầu tư kinh doanh chứ không thì số tiền đó sẽ nhanh hết.
//A dự định mở ra quán cafe
//Quán cafe sẽ có một số công việc như sau:
// + quản lý - nhân viên quản lý
// + bán hàng - nhân viên
// + giữ xe - bác bảo vệ
//Code
class NhanVien{
void UpdateNhanVien(){
//Update
//Cập nhật dữ liệu trên database
db.update(".....")
}
}
8. Relating run-time and compile time structures
Compile time:
Ví dụ : compile của Javascript
JavaScript --> C ++ --> C --> Hợp ngữ ---> Mã máy
Run-time: trong thời gian chạy chương trình sẽ chiếm :
CPU, Multi thread
Bộ nhớ (RAM, cached)
Tài nguyên khác: màn hình, máy in, ...
9. Designing for change
Last updated
Was this helpful?