RunPattern chứng minh làm thế nào các proxy có thể làm việc trong thực tế. Đầu tiên, nó tạo ra một AddressBookProxy và bổ sung thêm một số đối tượng Địa chỉ mới cho Proxy. Những địa chỉ mới này ban đầu sẽ được lưu trữ tại địa phương. | 39. 40. RunPattern demonstrates how the proxy could work in practice. First it creates an AddressBookProxy and adds several new Address objects to the Proxy. These new addresses will initially be stored locally. It is only when the example calls the method getAllAddresses that the Proxy will create an AddressBookImpl object and retrieve addresses stored in the file. Example 1. import 2. import 3. import 4. public class RunPattern 5. public static void main String arguments 6. Example for the Proxy pattern 7. 8. This code will demonstrate the use of a Proxy to 9. provide functionality in place of its underlying 10. class. 11. 12. 13. Initially an AddressBookProxy object will provide 14. address book support without requiring that the 15. AddressBookImpl be created. This could potentially 16. make the application run much faster since the 17. AddressBookImpl would need to read in all addresses 18. from a file when it is first created. 19. 20. 21. if new File .exists 22. 23. 24. Creating the AddressBookProxy 25. AddressBookProxy proxy new AddressBookProxy 26. Adding entries to the AddressBookProxy 27. this operation can be done by the Proxy without 28. creating an AddressBookImpl object 29. new AddressImpl Sun Education CO 500 El Dorado Blvd. Broomfield CO 80020 30. new AddressImpl Apple Inc. 1 Infinite Loop Redwood City CA 93741 31. Addresses created. Retrieving an address 32. since the address is stored by the Proxy there is 33. still no need to create