Rails for Java Developers phần 4

Quan điểm chính trong việc thiết kế của Matz là nhằm giảm thiểu các công việc nhàm chán mà họ, các nhà lập trình, buộc phải làm; tiếp đến là nguyên tắc thiết kế giao diện người dùng (user interface) hiệu quả.[3] Ông nhấn mạnh rằng việc thiết kế hệ thống cần phải tập trung vào con người, hơn là vào máy tính | MIXINS 91 def add_employee employee employee if self .employees employee self end def remove_employee employee employee nil end end Classes such as BusinessPerson can then pick up Employer functionality by calling include Employer Download code rails_xt samples class BusinessPerson Person include Employer Employee end Now the BusinessPerson class can call any Employer methods irb main 001 0 require business_person true irb main 002 0 boss Justin Gehtland BusinessPerson 0x54394 @first_name Justin @last_name Gehtland irb main 003 0 drone Stu Halloway BusinessPerson 0x4f9d4 @first_name Stu @last_name Halloway irb main 004 0 drone etc. The fact that include is a method call has interesting implications. The object model is not static and you could choose to have BusinessPerson include Employer under some circumstances and not others. In fact you can make object model decisions per instance instead of per class. The extend method works like include but on a specific instance. So a specific person could become an Employer at runtime irb main 001 0 require business_person true irb main 002 0 p Stu Halloway Person 0x5490c @first_name Stu @last_name Halloway irb main 003 0 class p ancestors end Person Object Kernel irb main 004 0 Employer Person 0x5490c @first_name Stu @last_name Halloway irb main 005 0 class p ancestors end Employer Person Object Kernel The variable p starts life as a plain old Person with class ancestors Person Object Kernel . The extend Employer call turns p into an Employer as well and the ancestor list changes appropriately. The odd-looking Functions 92 statement class p accesses the singleton class of p. A singleton class singleton class might better be known as an instance-specific class. You have modified the inheritance hierarchy of p so it is not

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.