Java design pattern-compound Pattern 2

Source: Internet
Author: User

Java design pattern-compound Pattern 2
Next, the duck Ming example: Java design pattern-one of the composite pattern
In the above article, we have implemented the combination of the modifier mode adapter mode factory mode, and now we need to make changes. above, we can count the sounds of a group of ducks, now we need to be able to observe the behavior of individual ducks.
Introduce the observer mode:
Any Quackable to be observed must implement the following interfacePublic InterfaceQuackObservable {Public VoidRegisterObserver (Observer observer );Public VoidYyobservers ();
}
Then make sure all Quackable implements the above interface. The Quackable in one of the composite modes is not observed.
Public InterfaceQuackableExtendsQuackObservable {Public VoidQuack ();}
To ensure that all the classes that implement Quackable can assume the role of QuackObservable, registration and notification must be implemented in each class. To implement this function, the registration and notification functions are now implemented in an Observerable helper class, then combine it with QuackObservable
Public ClassObservableImplementsQuackObservable {
ArrayListObservers =New ArrayList(); QuackObservable duckObservable;PublicObservable (QuackObservable duck ){This. DuckObservable = duck;} @ OverridePublic VoidRegisterObserver (Observer observer ){//TODOAuto-generated method stubObservers . Add (observer);}
@ OverridePublic VoidYyobservers (){//TODOAuto-generated method stubIteratorIterator = observers. iterator ();While(Iterator. hasNext () {Observer observer = (Observer) iterator. next (); observer. update (duckObservable );}}
}
Integrate the helper class and the specific Quackable class, and implement the specific class MallarDuck in the composite mode.Public ClassMallardDuckImplementsQuackable {
Observable observable;PublicMallardDuck () {observable =NewObservable (This) ;}@ OverridePublic VoidQuack (){//TODOAuto-generated method stub System.Out. Println ("Quack! "); // The Name Of The green duck is Quack policyobservers ();}
@ OverridePublic VoidRegisterObserver (Observer observer ){//TODOAuto-generated method stub observable. registerObserver (observer );}
@ OverridePublic VoidYyobservers (){//TODOAuto-generated method stub observable. policyobservers ();}
} Now we have almost implemented everything required for Observerable. We only need some observers. First, define an interface.
Public InterfaceObserver {Public VoidUpdate (QuackObservable duck );
}
The specific observer is as follows (implementing the observer Interface ):Public ClassQuackologistImplementsObserver {
@ OverridePublic VoidUpdate (QuackObservable duck ){//TODOAuto-generated method stub System.Out. Println ("Quackologist:" + duck + "just qucked .");}
}
The test code, similar to one of the composite modes, defines some ducks. The difference is that a observer needs to register the ducks to the observer.
Quackologist quackologist =NewQuackologist (); flock. registerObserver (quackologist );


With the above foundation, let's look at the MVC mode.
First, use an image to understand what the MVC framework is.


Model: A Model is a part of the application's data logic. Typically, model objects are used to access data in databases. View: View is the part of the application that processes data display. Generally, a view is created based on model data. Contorl: A controller is a part of an application that processes user interaction. Generally, the Controller reads data from the view, controls user input, and sends data to the model.
What design patterns are included in the MVC framework model?

Policy mode: the controller is a policy of the view. You can change the logic of the view by changing the controller.

Observer mode: The view is the observer of the model. When the model changes, the view can be notified.

Combination Mode: The view itself is a tree structure. We only need to operate on the top-level components (such as window objects) of the view, and the combination will process our requests.


Let's look at a specific example:
MP3 PLAYER:
1. the user uses the operation interface to change the song to be played. 2. The controller requests the Player Model to play the song. 3. The model tells the view that the status has changed. 4. The user sees the new status.
View Flowchart



The details are as follows:

1. You are a user-you interact with the view

A view is a model window. When you do something about the view (for example, press the "play" button), the view will tell the Controller what you have done. The Controller is responsible for processing.

2. The Controller requires that the model change status.

The Controller interprets your actions. If you press a button, the Controller will understand the meaning of the action and tell the model how to make the corresponding action.

3. The Controller may also need to change the view.

When the Controller receives an action from the view, the structure may also need to tell the view to change its result. For example, the controller can change some buttons or menu items on the interface to valid or invalid.

4. When the model changes, the model notifies the view.

Whether you have made some actions (for example, pressing the button) or some internal changes (for example, starting from the next song in the playlist), as long as something in the model changes, the model will notify the view that its status has changed.

5. The View queries the model status.

The view directly retrieves the display status from the model. For example, when the model notifies the view that a new song starts playing, the view asks the model for the song name and displays it. When the controller requests a view change, the view may also ask the model about certain States.


In this mp3 player, how does one use strategy, observation, and combination modes?


Next, let's take a look at the relationship between the observer, the view, and the Controller.

Observer:


Policy holder: The Relationship view between the view and the Controller only focuses on the performance. The controller focuses on the behavior of converting user input into a model.


Combination
A view is a combination of GUI components (such as labels, windows, and text input boxes). The top-level components include other components until the leaf node.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.