Tìm hiểu về phương thức khởi tạo [Builder] và phương thức hủy [Destroyer], cách sử dụng hiệu quả thông qua các ví dụ
Constructor và Kẻ hủy diệt là gì?
Phương thức khởi tạo [Builder] hay phương thức hủy [Destroyer] là 2 trong số các phương thức mang lại sự thuận lợi khi lập trình với
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }5
- Người xây dựng. tự động phương thức được gọi sau khi các đối tượng được tạo xong
- Kẻ huỷ diệt. tự động phương thức được gọi trước khi hủy đối tượng tiến trình
Cách sử dụng Constructor và Destructor trong C++
- Người xây dựng. function is dedefine by way to set trùng lặp tên với tên
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }
5 và không có kiểu trả về - Kẻ huỷ diệt. được định nghĩa bằng cách đặt trùng lặp với tên
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }
5 và thêm ký tự#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }
8 vào hàm phía trước
#include class Node { public: // Constructor Node[] { std::cout next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 được tạo ra
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 tự động được gọi
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }1, đối tượng
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 sẽ được thu hồi nhưng trước đó sẽ tự động gọi
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }3
Các trường hợp sử dụng Builder tăng hiệu quả tính toán
Builder and Destroyer không bắt buộc phải định nghĩa. Trong ngôn ngữ những cảnh không cần thiết thì không cần định nghĩa 2 phương thức này
Câu chuyện của Builder
Giả sử
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }4 là 1 phần tử trong danh sách liên kết, nghĩa là
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }5 sẽ lưu giữ vị trí của
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 kế tiếp hoặc đánh dấu là
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 cuối cùng]
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }
Dòng
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }9 được gọi sau khi
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 được tạo ra, để đảm bảo rằng
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }5 luôn là
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 cuối cùng [khi chưa được thêm vào danh sách]
Điều này bất tiện vì
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }9 không đáng tin vì có lúc quên gọi, mặc dù điều này là 1 mong muốn mặc định khi 1
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 ra đều này là 1 ________i thì ph 6. Trong trường hợp này, builder sẽ thể hiện được sức mạnh của nó
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }2
Như vậy không cần phải luôn tự nhắc nhở bản thân chỉ định
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }8 sau khi
#include class Node { private: Node *next; public: void init[] { this->next = nullptr; } } int main[] { Node node; node.init[]; return 0; }0 được tạo ra
Câu chuyện của Kẻ hủy diệt
Với ví dụ về node Trong Danh Sách Liên Kết, thấy hiệu quả tính năng của người xây dựng, nhưng về kẻ hủy diệt không mang nhiều tính chất hữu ích rõ ràng, vì trong thực tế nodec thu hồi 1 cách chủ động, hoặc trở thành 1 node zombie, nên ở phần Kẻ hủy diệt sẽ được sử dụng 1 ví dụ khác