Đang chuẩn bị liên kết để tải về tài liệu:
Applied Java Patterns Stephen phần 3
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
- Bất kỳ thông báo đó không phải là một cách rõ ràng xử lý hoặc chuyển tiếp là bỏ đi. Nếu các lớp trong chuỗi sản xuất các sự kiện không được sử dụng trong ứng dụng, điều này có thể là một cách chấp nhận được để giảm bớt nói nhảm. Tuy nhiên, bạn phải cẩn thận trong phương pháp này để tránh vô tình loại bỏ thông báo rằng hệ thống xử lý. | State Also known as Objects for States Pattern Properties Type Behavioral Level Object Purpose To easily change an object s behavior at runtime. Introduction An application often behaves differently depending on the values of its internal variables. For instance when you re working on a text file you need to periodically save your work. Most current text editors allow you to save a document only when something has changed in the text. As soon as you save the content the text is considered to be clean the file content is the same as the content currently on display. At this point the Save option is not available as it serves no purpose. Implementing this decision-making in the individual methods makes the code hard to maintain and read. The result is that these methods contain long if else statements. A common tactic is to store the state of an object in a single variable using constants for a value. With this approach the methods normally contain large switch case statements that are very similar in each method. Objects are state and behavior state is kept in its attributes and the behavior is defined in methods. The State pattern allows you to change the behavior of an object dynamically. This dynamic behavior is achieved by delegating all method calls that rely on certain values to a State object. Such a State object is state and behavior as well so that when you change State objects you also receive a different behavior. The methods in the specific State classes no longer have to use if else or switch statements the State object defines the behavior for one state. Applicability Use the State pattern when The object s behavior depends on its state and the state changes frequently. Methods have large conditional statements that depend on the state of the object. Description Objects that have different behavior based on their current state might be difficult to implement without the State pattern. As mentioned before implementation without using the State pattern .