Design Pattern Analysis exists in Android System Design

Source: Internet
Author: User

1. Open/closed (OCP)

Introduction: to scale, do not modify it. For example, I provide an external interface. If you want to update the API, do not modify it directly. Instead, provide another API and notify you when to switch to the new API.

For example, this design pattern is everywhere. Each Android upgrade will provide some new APIs, and some older APIs will be set to deprecated. For example, the Service is supported from level 1. onStart, in level 5, has been. service. onStartCommand is replaced, but Service. onStart remains unchanged, so as to continue to provide support for the transitional period.

2. Merging and reuse

Introduction: less inheritance (strong coupling), more synthesis relationships (flexible): interfaces, tool classes, etc.

For example, for any development, a main program class, such as an Activity in Android, is used. In development, we need to separate all the functions that can be called in the Activity as much as possible to avoid being too bloated. For example, you need to use the getLayout and getDrawable methods to read resources in an Activity. These functions are resource-related and are independent of the lifecycle control function provided by the Activity, therefore, it can be extracted and put into the Resource. In Activity, only one getResource is provided to obtain and use it. In this way, any module can obtain and use Resource through Context. Resource

Higher availability.

3. Dependency inversion (DIP)

Brief Introduction: describe details and abstract use cases. When passing parameters and combinations, try to use the abstract layer

For example, the ImageView. setImageDrawable method is used as an example. ImageView can display various resources such as Bitmap, Xml, and ninepatch. Based on the dependency reversal principle, we do not need to open a method in the ImageView class for each type, instead, it abstracts them into Drawable, while ImageView can only receive Drawable.

4. Interface isolation

Introduction: each interface does only one thing. Do not include multiple functions in one interface.

For example, the dependency inversion principle can be used to synthesize N methods into a method. However, these N methods must be of the same function but different parameter types. If the functions are different, do not combine them together to keep each method small and flexible. If there is a need to call N small functions consecutively, set another shell above. For details, refer to the mediation mode.

5. Adapter Mode

Introduction: different data providers use an adapter to provide services to the same customers.

For example, the most common examples are ListView and GridView. ListAdapter is used to connect various data and views.

6. Lishi replacement (LSP)

Introduction: In an inheritance tree, all Tree nodes are abstract classes, and all leaf nodes are specific implementation classes. You can use the parent class to input any subclass.

For example, the most classic one is the View inheritance tree in Android. All the methods passed into ViewGroup can be passed into any container inherited from ViewGroup; you can pass in the View method to any ViewGroup or control that inherits the View. For example, setContentView (Viewv). Since the parameter is View, you can pass in any control that inherits the View, such as TextView and LinearLayout.

7. Builder Mode

Introduction: You can construct each part step by step.

For example, the core of this mode is to construct an object step by step. The classic in Android is the use of AlertDialog. Builder.

8. Command)

Brief Introduction: requests are encapsulated into an object for sending and customization, queuing, and cancellation.

For example, if a Message command is sent to MessageQueue through Handler. post, which contains parameters such as arg1, arg2, and what, Handler calls handleMessage to execute the Message command.

9. metadata sharing

Introduction: The sharing technology is used to effectively support a large number of fine-grained objects.

For example, Message. obtain avoids frequent creation and destruction of a large number of Message objects by reusing Message objects.

10. Iterator Mode

Overview: provides a method to access all data in the dataset sequentially without exposing the internal representation of the object.

For example, you can use the Hashtable. elements method to obtain an Enumeration, and then use this Enumeration to access the data in Hashtable without worrying about the data storage method in Hashtable.

11. Mediator)

Introduction: when an object operation needs to call M methods of N objects to complete the call process, it encapsulates these call processes and becomes a mediator.

Example: Resource. the implementation logic of the getDrawable method is as follows: Create a cache to store all loaded files. If the Drawable corresponding to the id passed in getDrawable has not been loaded before, then it calls the XML parser to generate Based on the Resource Type corresponding to the id, or creates a Drawable by reading the image resource file in the package.

However, Resource. getDrawable encapsulates operations involving multiple objects and logic into a method to implement the role of a mediator.

12. Memento)

Introduction: the status of the backup object is convenient for future recovery without knowing the internal structure of the object.

For example, the onSaveInstanceState and onRestoreInstanceState of an Activity are serialized data structures such as Bundle to store the Activity status. As for the Data Structure Stored in the Activity, there is no need to worry about these two methods. This is a classic example of the memorandum mode.

13. Observer mode (Observer)

Introduction: when an object changes, all objects that trust it are automatically changed accordingly.

For example, we can use BaseAdapter. registerDataSetObserver and BaseAdapter. unregisterDataSetObserver to register with BaseAdater and deregister a DataSetObserver. In this process, DataSetObserver is an observer. Once the internal data of the BaseAdapter has variables, the DataSetObserver. onChanged and DataSetObserver. onInvalidated callback methods are used to notify the DataSetObserver implementation class.

14. Prototype Pattern)

Introduction: a large number of objects need to be created in the system. These objects have almost identical functions, but there is a little difference in details.

For example, we need several Bitmap formats: ARGB_8888, RGB_565, ARGB_4444, and ALAPHA_8. Then we can first create a Bitmap of ARGB_8888 as the prototype. On the basis of this, we can call Bitmap. copy (Config) to create Bitmap in other formats.

Another example is that all objects in Java have a method named clone, which is synonymous with the prototype.

15. Proxy mode (Proxy) www.2cto.com

Introduction: provides a proxy for other objects to control access to this object.

For example, I finally found a ready-made example. This is AIDL. All AIDL instances use a proxy mode. Assuming that Activity A binds A Service S, every method in call S of A is actually implemented through the Binder Mechanism of the system and then the corresponding method in call S. The Binder mechanism serves as a proxy.

16. State)

Overview: behavior changes when the status changes.

For example, the View. onVisibilityChanged method provides an implementation of the state mode, allowing the execution of the action in the onVisibilityChanged method when the View's visibility changes.

17. Strategy)

Introduction: defines a series of objects that encapsulate algorithms and behaviors. They can replace each other.

Example: Java. util. list defines an add, remove, set, and indexOf policy. As for the ArrayList and sorted List classes that implement this policy, only different algorithms are used for specific implementation. However, because they have the same policies and do not consider the speed, they can be replaced with each other during use.

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.