TAILIEUCHUNG - Design Patterns FOR Dummies phần 3

Hình 1-8 cho thấy nó hoạt động như thế nào, một người quan sát có thể tự đăng ký với chủ đề. Hình 1-8: Các mô hình Observer cho phép các quan sát viên đăng ký với các đối tượng Và một người quan sát, Observer 2, có thể đăng ký chính nó như là tốt, như thể hiện trong hình 1-9. | Chapter 3 The Decorator and Factory Patterns 47 So if you wrap a computer object whose description method returns the text computer in a Disk wrapper when the wrapper s description method adds the text and a disk you end up with the total text computer and a disk. That s what you get from the Disk wrapper s description method at this point. Adding a CD Besides disks you can also add CD drives to your computer purchase orders. Here s what the CD wrapper looks like note that it adds and a CD to the return value from the wrapped object s description method public class CD extends ComponentDecorator Computer computer public CD Computer c computer c public String description return and a CD Adding a monitor To add a monitor to the purchase order you have to make the monitor wrapper add the text and a monitor to the return value of the wrapped object s description method. public class Monitor extends ComponentDecorator Computer computer public Monitor Computer c computer c public String description return and a monitor 48 Part I Getting to Know Patterns OK that gives you all you need to start running some code. How about testing it out Testing it out The best way to test out your computer component is to use a testing class that can be found in the downloadable code for this book. starts by creating a computer like this public class Test public static void main String args Computer computer new Computer Then the code wraps that computer in a wrapper that adds a hard disk. public class Test public static void main String args Computer computer new Computer computer new Disk computer Now let s add a monitor. public class Test public static void main String args Computer computer new Computer computer new Disk computer computer new Monitor computer Chapter 3 The Decorator and Factory Patterns 49 Then you might as well add not just one CD drive but two cost is no consideration here. Finally you can display the .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.