Project examples
Background: There are four kinds of webview because the business logic is divided into four types, respectively, as follows:
public static final int Browser_type_inner = 0 ; public static final int Browser_type_simple = 1 ; public static final int Browser_type_outer = 2 ; public static final int Browser_type_third = 3 ;
These four different types of business logic are placed in different Handler classes, so there will be four kinds of handler, in order to unify the interface and facilitate the extension, to provide an interface, this interface is: IWebViewBusiness , and in order to extract the business logic part, further clarify the code logic, using a WebViewManager class to manage.
The specific hierarchy is as follows:
The above inheritance is relatively simple, here no longer repeat, the following focus on the four handler management class
publicclass WebViewManager implements IWebViewBusiness{}
With this management class, a different handler is generated in the constructor
Public Webviewmanager(context context, Iwebviewui Webviewui,intBrowserType) { This. Miwebviewui = Webviewui;Switch(BrowserType) { CaseBrowser_type_inner:mwebviewhandler =NewInnerwebviewhandler (context, Miwebviewui); Break; CaseBrowser_type_outer:mwebviewhandler =NewOuterwebviewhandler (context, Miwebviewui); Break; CaseBrowser_type_simple:mwebviewhandler =NewSimplewebviewhandler (context, Miwebviewui); Break; CaseBrowser_type_third:mwebviewhandler =NewBrowserthirdhandler (context, Miwebviewui); Break; }}
Then take a look at WebViewFragment how to use it in specific or WebViewActivity similar UI classes.
publicclass SuperFanliFragmentWebview extends BaseFragmentWebview {}
The manager instance is generated in the OnCreate () life cycle, and the manager constructs a different handler through the type parameter.
@OverridepublicvoidonCreate (Bundle savedInstanceState ) { super.onCreate(savedInstanceState ); newthis, WebViewManager.BROWSER_TYPE_INNER ); mWebViewManager.onCreate(savedInstanceState );}
At this point the structure abstraction is completed, and a closer look at the above example is not quite like the following common factory pattern:
General Factory Example
A simple example is as follows:
/************************************************* Send Interface *********************** **************************/ Public interface Sender { Public void Send();}/************************************************* Mail Implementation class ************************ *************************/ Public class mailsender implements Sender { @Override Public void Send() {System.out.println ("This is mailsender!"); }}/************************************************* SMS Implementation class ************************ *************************/ Public class smssender implements Sender { @Override Public void Send() {System.out.println ("This is SMS sender!"); }}/************************************************* General Factory class ************************** ***********************/ Public class sendfactory { PublicSenderProduce(String type) {if("Mail". Equals (type)) {return NewMailSender (); }Else if("SMS". Equals (type)) {return NewSmssender (); }Else{System.out.println ("Please enter the correct type!");return NULL; } }}
Test case
publicclass FactoryTest { publicstaticvoidmain(String[] args) { new SendFactory(); Sender sender = factory.produce("sms"); sender.Send(); }}
General Factory Summary
Common Factory Method Mode There is a problem is that the creation of the class depends on the factory class, that is, if you want to extend the program, the factory class must be modified, which violates the closure principle, so from the design point of view, there are some problems, how to solve? Use the abstract factory pattern to create multiple factory classes so that once you need to add new functionality, you can add new factory classes directly, without having to modify the previous code.
Abstract Factory
A simple example is as follows:
/************************************************* Send Interface *********************** **************************/ Public interface Sender { Public void Send();}/************************************************* Mail Implementation class ************************ *************************/ Public class mailsender implements Sender { @Override Public void Send() {System.out.println ("This is mailsender!"); }}/************************************************* SMS Implementation class ************************ *************************/ Public class smssender implements Sender { @Override Public void Send() {System.out.println ("This is SMS sender!"); }}/************************************************* Factory Interface ************************* ************************/ Public interface Provider { PublicSenderProduce();}/************************************************* Mail Factory implementation class ************************** ***********************/ Public class sendmailfactory implements Provider { @Override PublicSenderProduce(){return NewMailSender (); }}/************************************************* SMS Factory implementation Class ************************* ************************/ Public class sendsmsfactory implements Provider{ @Override PublicSenderProduce() {return NewSmssender (); }}
Test case
publicclass Test { publicstaticvoidmain(String[] args) { new SendMailFactory(); Sender sender = provider.produce(); sender.Send(); }}
Abstract Factory Summary
In fact, the advantage of this model is that if you want to add a function now: Send timely information, you just need to do an implementation class, to implement the sender interface, at the same time do a factory class, implement Provider interface, OK, no need to change the ready-made code. To do so, expand better!
Retrofit Project Examples
In accordance with the abstract factory above, the transformation of the extended better abstract factory is as follows:
First define the above so-called Provider interface:
publicinterface WebViewHandlerProvider { publicproducefinalfinal IWebViewUI webviewBusiness );}
Then define the factory classes of several other types of handler and implement this interface:
/****************************************************************************** Innerwebview Workers Plant Realization Class ******************************************************************************/ Public class innerwebviewhandlerfactory implements webviewhandlerprovider { @Override PublicIwebviewbusinessProduce(context context, Iwebviewui webviewbusiness) {if(Context = =NULL|| Webviewbusiness = =NULL) {return NULL; }return NewInnerwebviewhandler (context, webviewbusiness); }}/****************************************************************************** Outwebview Factory Implementing Class ******************************************************************************/ Public class outwebviewhandlerfactory implements webviewhandlerprovider { @Override PublicIwebviewbusinessProduce(context context, Iwebviewui webviewbusiness) {if(Context = =NULL|| Webviewbusiness = =NULL) {return NULL; }return NewOuterwebviewhandler (context, webviewbusiness); }}/****************************************************************************** Other factory implementations class omitted ******************************************************************************/
The structure is as follows:
After the modification is complete, let's look at how to use it, or go back to the UI class to see:
publicclass SuperFanliFragmentWebview extends BaseFragmentWebview {}
Or generate a manager instance in the OnCreate () life cycle
@OverridepublicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState ); finalnew InnerWebViewHandlerFactory(); this); mProvider.onCreate(savedInstanceState );}
So the modification is done, if you want to add a type of handler, only need to add a factory, and a specific handler can not modify the other classes at all, this extensibility is better.
Of course, if you do not want to do so, you can also directly without the abstract factory model, straightforward and simple rude good:
@OverridepublicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState ); newthis); mProvider.onCreate(savedInstanceState); }
In the end, if it's so simple and rude, what is the difference between these two implementations compared to the above abstract factory implementations?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java design mode-Factory mode