Through the previous several blog posts, we analyzed the AUI cache, tool class, display and load these aspects of code, today, we continue to study the source of AUI, learn the core of the auxiliary tools class. I hope you can learn something in it, ha.
Download
To download a picture, we imagine what we need ha: first we have to set up a support agreement, we have to download the link, by the corresponding download link to download the image, and then get the image of the input stream, the rest of the image to the display/loading class processing. Then we can design the following interface:
Public interface imagedownloader {InputStream GetStream (String Imageuri, Object Extra)throwsIOException; Public enumScheme {HTTP ("http"), HTTPS ("https"), FILE ("File"), CONTENT ("Content"), ASSETS ("Assets"), drawable ("Drawable"), UNKNOWN ("");PrivateString scheme;PrivateString uriprefix; Scheme (String scheme) { This. scheme = scheme; Uriprefix = scheme +"://"; } Public StaticSchemeOfuri(String URI) {if(URI! =NULL) { for(Scheme s:values ()) {if(S.belongsto (URI)) {returnS } } }returnUNKNOWN; }Private Boolean Belongsto(String URI) {returnUri.tolowercase (locale.us). StartsWith (Uriprefix); } PublicStringWrap(String Path) {returnUriprefix + path; } PublicStringCrop(String URI) {if(!belongsto (URI)) {Throw NewIllegalArgumentException (String.Format ("URI [%1 $ s] doesn ' t has expected scheme [%2$s]", URI, Scheme)); }returnUri.substring (Uriprefix.length ()); } }}
See here people may be puzzled, we are not design interface, why to do an enumeration type, but also in the inside to engage in so many messy things ... In fact, if you have seen "thinking in Java" You will know that in Java, an enum is actually a class, because enum classes that are defined by enums inherit the Java.lang.Enum class by default at compile time, and the enumeration class is automatically added to the final keyword Modification, which also makes the enumeration class impossible to inherit. In a more detailed explanation, you can make your own Google Kazakhstan.
So why introduce this enumeration class in Imagedownloader? Let's take a look at what's in the enumeration class, in the enumeration class Scheme, there are four main methods, and these four methods are used to handle the URI, such as: Cut URI, add URI prefix, Judge Uri. In other words, the abstract function of Scheme is to modify the Uri. The result of the Scheme's modification to the Uri will be given to Imagedownloader to complete the download operation.
In other words, the abstraction of Scheme is actually inconsistent with the abstraction of Imagedownloader (Imagedownloader abstraction is the download image, and the Uri required to download the image needs to be processed to be imagedownloader, and to complete the download is not really important), in order to reduce the coupling degree of the class, we implemented the Scheme class within the Imagedownloader.
Then one might ask, so do we create a different class? As far as I can see, it must be possible. Because Scheme is also essentially a class, we create a new class, declared as the final class, add the corresponding static constants, methods, in fact, the same effect. So why did the writer do it? A friend who has seen "effective Java" may know that the best way to implement a singleton is to use an enum, specifically explaining that you are going to check it out for yourself, and I'm not here to say more. So the author is actually defining a singleton inside the interface.
What's an efficient-to-implement a singleton pattern in Java?
Baseimagedownloader implementation of the Imagedownloader interface, and according to our needs to achieve the corresponding picture download details, concrete nothing good to explain, we can read the source of their own ha ~
Listener
In AUI, actually need to use Listener not many, after all, picture loading is just a small function module. So what Listener does the AUI contain?
- Image loading listener: Listen for picture loading start, end, fail, cancel
- Picture loading Progress Listener: Monitor the progress of picture loading
- Picture load scrolling Listener: When the picture loading is in progress, if scrolling occurs, stop loading, in other words, only load the current screen display of the picture, picture scrolling process picture is not loaded.
One might find it strange why the image loading listener and the picture loading progress listener are implemented separately. In fact, I also want to not understand, I hope someone can give me an explanation ...
Assist
There are several classes in the assist we have mentioned in the previous blog post, and I will not repeat the pull. Relatively simple class I will also take a stroke, I hope you understand ha.
Deque
Here are some double-ended queues, such as Linkedblockingdeque, Lifolinkedblockingdeque. The corresponding knowledge of the double-ended queue, and the specific implementation I believe I don't have to be here nonsense, after all, the data structure of the course will talk about this knowledge point, which is also a basic, must master data structures.
So what do we need to know here? That is: Why the dual-ended queue as the AUI data structure, the dual-end queues compared to other data structures in this scenario has any advantages.
We may as well first see where Deque is used, search found in the AUI library, Defaultconfigurationfactory called Deque. So what exactly is Defaultconfigurationfactory?
From the name of the class and the internal method name we can know, Defaultconfigurationfactory is AUI default configuration factory class, if the developer does not customize the corresponding configuration options, AUI will use the default options set by this class, complete the corresponding loading, Download, cache, etc...
Consider the following code snippet:
public static Executor createexecutor (int ThreadPoolSize, int threadpriority, Queueprocessingtype tasksprocessingtype) {boolean LIFO = Tasksprocessingtype = Queueprocessingtype.lifo; Blockingqueue<runnable> taskqueue = LIFO? new Lifolinkedblockingdeque<runnable> (): new linkedblockingqueue<runnable > (); return new Threadpoolexecutor (ThreadPoolSize, ThreadPoolSize, 0 L, Timeunit.milliseconds, Taskqueue, Createthreadfactory (threadpriority, "uil-pool-" )); }
In this code, we get the thread pool and use Lifolinkedblockingdeque as the processing queue for the thread. In other words, in the AUI library, the data structure of the double-ended queue is used to complete the AUI thread processing. So why choose a double-ended queue, and why choose Lifolinkedblockingdeque as the default option?
The dual-ended queue is chosen because Threadpoolexecutor uses the producer-consumer model, which is used internally within the Threadpoolexecutor class Blockingqueue As a task processing queue, it can satisfy the requirement of producer-consumer pattern to the task storage data structure. In our assist, Lifolinkedblockingdeque is the subclass of Linkedblockingdeque, and Linkedblockingdeque implements the Blockingdeque interface, The Blockingdeque interface also inherits from the Blockingqueue.
Others
The remaining auxiliary classes in the assist I think are quite simple, are some of the basic API call simplification, we see the source code can read it ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
From design to implementation, step-by-step teaching you to implement android-universal-imageloader-helper classes