Application 1:
1. Code:
In brosweractivity. Java, you have created a webviewclient. The Code is as follows:
Private Final webviewclient mwebviewclient = new webviewclient (){
......
/**
* Displays SSL error (s) dialog to the user.
*/
@ Override
Public void onreceivedsslerror (
Final webview view, final sslerrorhandler handler, final sslerror error ){
......
New alertdialog. Builder (browseractivity. This)
. Settitle (R. String. security_warning)
. Seticon (Android. R. drawable. ic_dialog_alert)
. Setview (warningsview)
. Setpositivebutton (R. String. ssl_continue,
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
Handler. Proceed ();
}
})
. Setneutralbutton (R. String. view_certificate,
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
Showsslcertificateonerror (view, handler, error );
}
})
. Setnegativebutton (R. String. Cancel,
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
Handler. Cancel ();
Browseractivity. This. resettitleandrevertlockicon ();
}
})
. Setoncancellistener (
New dialoginterface. oncancellistener (){
Public void oncancel (dialoginterface DIALOG ){
Handler. Cancel ();
Browseractivity. This. resettitleandrevertlockicon ();
}
})
. Show ();
......
2. Analysis:
(1) function: As shown in the comment, create an error dialog and display
(2) mode Description: here, the simplified builder mode is used to omit the abstract builder, only one specific builder is used, and the instructor role is also omitted. Alertdialog. builder is the specific builder. In addition, it is created in the form of an anonymous class, and the builder class is the internal class of alertdialog. In this way, the coupling is relatively low, this is one of the design intent to be achieved in object-oriented systems. Finally, call the show function. Its return type is exactly the product we want to create, that is, alertdialog. Therefore, builder is an internal Anonymous class of alertdialog (product) to create and display a dialog.
(3) revelation: we can simplify the builder mode and omit the abstract builder or even the mentor role. The specific creator and product can be implemented using internal classes to achieve low coupling.
Application 2: Stringbuilder, stringbuffer
1. Code:
Public class defaulthttpclientconnection extends sockethttpclientconnection {
......
Public String tostring (){
Stringbuffer buffer = new stringbuffer ();
Buffer. append ("[");
If (isopen ()){
Buffer. append (getremoteport ());
} Else {
Buffer. append ("closed ");
}
Buffer. append ("]");
Return buffer. tostring ();
}
}
2. Analysis:
(1) function: stringbuffer and stringbuilder are included in the Java. lang package. The implementation in Android is different from that in standard Java2, as shown below:
(2) mode description:
• Here, the simplified builder mode is used to omit the instructor role.
• The Builder mode is used to implement the member function tostring of defaulthttpclientconnection.
• The final return buffer. tostring () actually returns the product string.
(3) inspiration:
• You can use the builder mode to implement a class member function.