The adoption of design patterns in ANDROID--structural patterns

Source: Internet
Author: User

The adapter pattern, appearance mode, decoration mode, and proxy mode in the structural mode are all packaging modes, which are the wrapper for the other classes or objects, but the respective intentions are different.

Adapter mode through the packaging of other classes or objects, the interface is converted to the user's desired interface, to achieve the purpose of interface adaptation.

The appearance mode provides a high-level interface to a packaged set of classes or objects, with the intention of simplifying the interface and making the system easier to use.

The intent of the decorating mode is to add additional functions or responsibilities to the wrapper object without changing its interface.

The intent of the proxy mode is to control the access to the wrapper object by wrapping the wrapper object.

Adapter mode, appearance mode the interface between the client and its wrapper class is different, that is, these two modes for the customer to change the wrapper class access interface. While the decorative mode, proxy mode does not change the interface of the wrapper class, the interface provided to the customer is the same as the interface of the wrapper class.

1. Adapter Mode

There are two types of adapter modes: Class Adapter mode and object adapter mode, one using inheritance, one using object composition, and related UML class diagram as follows:

The adapter pattern in Android is heavily used, because Android uses the Java language, and the Java language does not support multiple inheritance, so the adapter pattern used in the system is mainly the second, which is used to convert the interface of the wrapper object to the desired interface.

The most commonly used adapter-mode classes in Android are classes that inherit from Baseadapter, such as Arrayadapter,cursoradapter, etc., to be used in the underlying data and The Adapterview view provides an adaptation between the read interface to the underlying data and the interface required for the view. The class diagram is as follows:

The view that inherits from Adapterview (Client)obtains the data that the view needs through the interface function of the adapter (Target), such as adapter interface function getItem (int position) Get the data item for location position from the underlying dataset , and theGetCount () function Gets the number of data items in the dataset.

The specific class of adapter completes the adaptation to read interfaces with different underlying data types, such as the CursorAdapter class reading data from the underlying database through a cursor object,Arrayadapter is used to read the data in an array of any type list, The Historyadapter is used to read data from a vector of type historyentry.

Other uses of the adapter mode are the display of the DisplayAdapter class in the service and the Printdocumentadapter class in the Print Service . DisplayAdapter is used to adapt different types of display devices to detect and discover different types of devices connected to the system. The Printdocumentadapter is used to adapt different print output target devices, such as printers or files, to output printed content on these devices.

2. Appearance mode

The class diagram for the appearance pattern is as follows:

The appearance mode used in the Android system is more places, it can be said that each system service to the customer provides a access to the system services of the Management portal class, to facilitate access to the corresponding system services, such as window management services corresponding to the WindowManager, Enter the corresponding InputManager for the management Service , the Activitymanager for the Activity Management Service, and so on.

In addition, Contentresolver,Log,Context,ServiceManager can also be regarded as the adoption of portal mode.

The contentresolver is used to provide a common interface for applications to access data patterns, and log provides a common interface for applications touse the log output system, and thecontext provides an interface for applications to access the entire system. ServiceManager provides access to other system services.

3. Decoration Mode

The class diagram for the decorating mode is as follows:

There are also many examples of using decorative patterns in Android systems. such as the Contextthemewrapper class and its derived classes activity and Service. The UML class diagram is as follows:

Contextwrapper is right.The wrapper for the Contextimpl class , the service class, and the activity class are derived classes of Contextwrapper , and theservice class derives directly from contextwrapper ,activity indirectly derives from the subclass Contextthemewrapper of Contextwrapper, and the service class and activity classes are respectively The Contextwrapper and Contextthemewrapper class interfaces are based on the addition and implementation of their respective lifecycle callback interfaces (hooks), and theactivity class provides functionality related to the window and view display associated with it. the function of Contextthemewrappe is to add support for the topic (Theme) on contextwrapper basisand to return the service management object through the Getsystemservice interface for The Layout_inflater_service service has been specially processed to return a cloned object of a Layoutinflater object using prototype mode.

4. Agent Mode

the class diagram for the proxy mode is as follows:

proxy mode is also common in Android, with each system service and local service corresponding to a remote proxy class for cross-process access to system services or local services. Each system service is a stub object, and the client accesses the corresponding stub object through a proxy object . For example, the class diagram for Management Management Services is as follows:

Windowmanagerglobal as a client IWindowManager.Stub.Proxy cross-process access to system services through a proxy object that inherits from the interface Iwindowmanager Windowmanagerservice.

5. Bridging mode

The intent of the bridging mode is to separate the abstract part from its implementation, so that they can vary independently. The abstraction and implementation here refer not to the abstraction and implementation of the class, but to the function abstraction (or interface) and the function implementation. Any of the two independent evolutionary derived classes that have an implementation relationship can form a bridging pattern that is primarily used on graphics and windowing systems that need to span multiple platforms. As with all of the books that introduce bridging patterns, almost all are implemented with different features of Windows and Windows, and different views (or graphs) and views (or graphics) are drawn as examples of bridging patterns. The following is the class diagram structure of the bridging mode:

Bridging mode can also be considered between the view tree of different views in the Android system and the class that completes the drawing function of the view, and between the window class and the function implementation class of the window. The related class diagram is as follows:

  

The different views in the diagram, such as Button,ImageView,TextView, constitute an abstract derived hierarchical view tree in which view is the root view of all views, and the drawing of the view isimplemented by three different classes that can evolve independently: Canvas (provides drawing surface),hardwarelayer (provides output display layer),displaylist (represents a drawing operation).

The following is a class diagram between Windows and Windows implementation classes in Android systems. Window ,phonewindow form the abstract part of the Windows, and window is an abstractpart of the abstract interface, windowcurrently only provides a specific class Phonewindow, The implementation-related class of window also forms a derived class diagram,WindowManager the base class for the Window implementation section,Windowmanagerimpl the concrete class of WindowManager, Windowmanagerimpl interacts with the window Management Service Windowmanagerservice through the Windowmanagerglobal and through the Iwindowmanager interface , The actual window management functionality is completed by the Window Management Service.

The bridge is very similar to the adapter two modes, only the use of the occasion and the viewing angle are different.

The difference between bridging mode and adapter mode in the classic design pattern of GOF is that the structure of theB R i d g pattern is similar to the object adapter, and that the two modes differ mainly in their respective uses and starting points:B r i d g E is to separate and bridge the interface part and the implementation, so that they can be easily and relatively independent of the change and evolution,B R i d g E mode can provide users with a stable abstraction and its implementation interface. While a D a P T e r mode solves the mismatch between two existing interfaces by altering the interface of an existing object, which helps to work together between incompatible classes,a D a P T e r mode does not consider how these interfaces are implemented, nor does it redesign them, nor does it consider how each of them might Evolution. So a D a P T e R is usually used after the system design is complete, whereas the B R i d g e pattern must know beforehand that an abstraction will have multiple implementations, so it is used at the beginning of the system.

Therefore, as can be seen from the above description: for a bridge or adapter mode of the completed code, from different viewing angle can be regarded as bridging mode, can also be seen as the adapter mode.

6. Combination Mode

The intent of the composition pattern is to combine objects into a tree structure to represent a "partial -whole" hierarchy. C o m P o s i t e makes the user consistent with the use of a single object and a composite object. The following is the class diagram structure:

The standard examples of combining patterns are graphs, views, menus, and windows, and collection classes (such as arrays) and file directories can also be used as combination patterns. The following is a combined view class diagram in the Android system using the combined mode:

7. Enjoy meta mode

The intent of the enjoy meta-mode is to effectively support a large number of fine-grained objects using shared technology, with the following class diagram structure:

The enjoy meta model is also a popular model.

        in the android system, each application component can use many of the system-provided service management objects, such as wallpapermanager, accessibilitymanager, Captioningmanager, accountmanager and so on. So in order to share these objects within a component, the implementation of the context of the application component contextimpl, servicefetcher object (with static code blocks) for each management object, and creates a newly created map collection, each servicefetcher object is enlisted in the

When the application component calls Context.getsystemservice to obtain a system service management object, it first obtains the corresponding Servicefetcher object from the Map collection based on the service name , and then calls The GetService function of the Servicefetcher object obtains the service management object created by the Servicefetcher object.

In the getservice function of the Servicefetcher object, first from the list of cache arrays maintained by the context, find if the index of the Servicefetcher object already contains The service management object created by the Servicefetcher object, if a service management object exists in the cache, returns the service management object that is saved in the cache, otherwise calls Servicefetcher's The CreateService function creates a service management object and places it in the cache based on its index for the next use.

Transferred from: http://blog.csdn.net/goohong/article/details/38089037

The adoption of design patterns in ANDROID--structural patterns

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.