The use of design patterns in ANDROID--structural patterns

Source: Internet
Author: User

structurethe adapter mode, the appearance mode, the adornment mode, the proxy mode in the mode are all in the packing mode, which is the wrapper of the other class or object, but the different intentions.

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 adornment mode is to add additional functions or responsibilities to the wrapper object without changing the interface.

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

The interface between the adapter mode and the appearance mode for the customer is different from that of the wrapper class, which means that the two modes change the access interface of the wrapper class to the customer. 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

The adapter mode has two types: Class adapter Mode and the object adapter pattern, one uses the inheritance method, one uses the object composition method, the related UML class diagram is as follows:


Android The adapter pattern in the system 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 primarily another, Used to convert the interface of the wrapper object to the required interface.

The class of the adapter pattern most commonly used in Android is the class that inherits from Baseadapter , such as Arrayadapter,cursoradapter to provide an adaptation between the underlying data and the Adapterview view to transform the read interface of the underlying data into the interface required by the view. Class diagrams such as the following:

The view that inherits from the Adapterview (Client) obtains the data that the view needs through the interface function of the adapter(Target) , as The Adapter interface function getItem (int position) obtains the location from the underlying dataset position Data item, thegetcount () function obtains the number of data items in the data set.

The detailed class of Adapter is complete with the adaptation of the read interface for different underlying data types, such as the CursorAdapter class reading data from the underlying database through a Cursor objectarrayadapter used to read data from a random type list array,Historyadapter is used to read data from a vector of type historyentry .

Other adapter modes include the DisplayAdapter class in the display 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 patterns used in the Android system are much more, and it can be said that each system service provides the customer with a management portal class for access to the system service, which is used to facilitate accessibility to the appropriate system services, such as the form Management Service corresponding to the WindowManager, enter the corresponding InputManagerof the Management Service, the corresponding Activitymanager of the Activity Management Service and so on.

another contentresolver ,Log,Context,ServiceManager can also be regarded as the use of portal mode.

Contentresolver is a common interface used to provide access to data patterns for applications, andlog provides a common interface for applications that use the log output system, 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 adornment mode is as follows:



There are also many examples of the use of decorative patterns in Android systems. such as the Contextthemewrapper class and its derived classes Activity and Service. UML class diagrams such as the following:


Contextwrapperis toContextimplPackaging of Classes, ServiceClasses andActivityClasses areContextwrapperthe derived class,Service class derives directly fromContextwrapper,Activityindirectly derived fromContextwrappersub-classContextthemewrapper,ServiceClasses andActivityclass, respectively, inContextwrapperand theContextthemewrapperclass interfaces and implements the respective life cycle callback interfaces (hooks), theActivity class also provides functionality related to the display of forms and views associated with it. the function of the Contextthemewrappe is to add a contextwrapper based on the theme ( Theme) is supported and is returned to the service management object through the getsystemservice interface for Layout_inflater_service The service has been specially processed to return a cloned object of a Layoutinflater object using prototype mode .

4.Proxy mode

The class diagram for 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 visits the corresponding stub object through a Proxy object. such as the corresponding class diagram for management management services such as the following:

Windowmanagerglobal as a client through a proxy object inherited from the interface iwindowmanager IWindowManager.Stub.Proxy Cross-process access to system service Windowmanagerservice.

5.Bridging mode

The intent of the bridging mode is to separate the abstract part from its implementation, so that both can change independently. The abstraction and implementation here refer not to the abstraction and implementation of the class, but to the function abstraction (or interface) and function implementation. No matter what the two independent evolutionary derived classes that have the implementation relationship can form a bridging pattern, the bridging pattern is primarily used on graphics and form systems that need to span multiple platforms. All of the books that introduce bridging mode are almost always implemented with different features of form and form functionality, with different views (or graphs) and views (or graphs) drawn as examples of bridging patterns. For example, the following is the class diagram structure for bridging mode:


bridging mode is also used between the same view tree as the different views in the ANDROID system and the classes that complete the drawing of the view, and between the form class and the form's functional implementation class. Related class diagrams such as the following:

  

The different views in the diagram, such as Button,ImageView, andTextView , form an abstract derived hierarchy view tree, which is viewed in the view tree . is the root view of the full view, the drawing of view is achieved through three different classes that can evolve independently:Canvas(provides drawing surface),hardwarelayer(provides output display layer) ,displaylist(represents a drawing operation).

For example, the following is a class diagram between a form and a form implementation class in an ANDROID system. window,phonewindow forms the abstract part of the form,window is an abstract part of the abstract interface,window Now only provides a detailed class Phonewindow, the implemented partial class ofWindow also forms a derived class diagram,windowmanager the base class for the form implementation section,Windowmanagerimpl for WindowManager 's detailed class,Windowmanagerimpl through windowmanagerglobal and through The Iwindowmanager interface interacts with the form Management Service Windowmanagerservice , and the form Management Service completes the actual form management functionality.

         bridging is similar to adapter two modes, Only the use of the occasion and the angle of observation are different.

       in gof the design pattern in classical classics, the difference between bridging mode and adapter mode is described as: the structure of b R i d g E mode is similar to the object adapter, Span style= "Color:black" > Two modes differ mainly in their respective uses and the starting point: b R i d g e is designed to separate and bridge the interface and implementation parts, Thus they can be more easy and relatively independent to change and evolve, b R i d g e for the user. and a D A p T e r mode by changing an existing object interface to , To help implement incompatible classes that work together, a D A p T e r mode a D A p T e r is usually used after the system has been designed, and b R i d g e mode You must know beforehand that an abstraction will have more than one implementation part, so is used

so from the descriptive narrative above, it can be seen that a bridge or The completed code for the adapter mode can be viewed as a bridging mode from different viewing angles, and also as an adapter mode.

6.Combination mode

the intent of the composition pattern is to combine objects into a tree structure to represent a "partial - overall" 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. For example, the following is a class diagram structure:


the standard samples of the combined pattern are graphs, views, menus, and forms, and the collection classes (such as arrays) and file folders can also be used as a combined pattern. For example, the following is a combined view class diagram of a combined pattern in an ANDROID system:

7 . Enjoy meta mode

Enjoy meta-mode The intent is to effectively support a large number of fine-grained objects using shared technology, such as the following:

              

        in android system, each application component is able to use the many service management objects provided by the system, such as wallpapermanager , accessibilitymanager , captioningmanager , Accountmanager and so on. Therefore, in order to share these objects within a component, the implementation of the context in the application component contextimpl , contextimpl the class first loads a reference, creates a servicefetcher for each of the Management Objects Object (using a static block of code) and place the newly created servicefetcher object into the map collection based on the service name, each servicefetcher object is assigned an index when it is enlisted in the map collection.

         When the application component calls context.getsystemservice to obtain a system service management object, it starts with the service name map collection to obtain the corresponding servicefetcher object, and then call Servicefetcher object getservice function obtains servicefetcher The service management object that the object creates.

In the getService function of the servicefetcher object, first from the list of Cache arrays maintained by the Context servicefetcher whether an index lookup for an object already includes a service management object created by the Servicefetcher object, and if a service management object exists in the cache , the service management object that is saved in the cache is returned directly , otherwise call Servicefetcher 's createservice function to create a service management object and place it in the Cache based on its index for the next use.

All rights reserved, please clearly indicate the source and link when reproduced, thank you!





Use of design patterns in ANDROID-structured mode

Related Article

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.