[Android] GOF23 Design patterns & Design Patterns in Android

Source: Internet
Author: User

GOF23 Type design pattern

Design principles:

1. Single responsibility Principle (SRP): In the case of a class, there should be only one cause for it to change

2. Open-Close principle (OCP): Software entities (classes, modules, functions, etc.) should be extensible, but not modifiable. That is, the extension is open, and the modification is closed.

3. Dependency reversal principle: a. High-level modules should not rely on low-layer modules, and two should rely on abstraction. B. Abstractions should not be dependent on detail, and detail should be dependent on abstraction. To be blunt, it is to program for the interface, not to implement programming.

4. Dimitri rule (LoD): If two classes do not have to communicate directly with each other, then these two classes should not have a direct interaction. If one of the classes needs to invoke a method of another class, the call can be forwarded through a third party. The basic idea is to emphasize loose coupling between classes.

23 Different design modes
1. Create mode:
(1). Factory method Mode
(2). Abstract Factory mode
(3). Builder mode
(4). Prototype mode
(5). Single Case mode
2. Structural mode:
(6). Adapter Mode
(7). Bridge mode
(8). Combination Mode
(9). Decoration mode
(10). Appearance mode
(11). Enjoy meta mode
(12). Proxy mode
3. Behavioral Mode
(13). Interpreter mode
(14). Template Method Mode
(15). Responsibility Chain Mode
(16). Command mode
(17). Iterator mode
(18). Broker Mode
(19). Memo Mode
(20). Observer mode
(21). State mode
(22). Policy mode
(23). Visitor Mode

Common design Patterns in Android

1. Combination mode

View and ViewGroup. The combined mode makes the user consistent with the use of single object view and composite object ViewGroup. Can simplify customer code. Basic objects and composite objects are consistent, and users do not have to differentiate between them.

2. Template Method mode

Defining the skeleton of an algorithm in an operation, and delaying the steps of some algorithms into subclasses, the template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

such as the Draw method in view, OnDraw (), Dispatchdraw () are defined as abstract methods, custom view, General rewrite Ondraw,dispatchdraw method can be

if (!verticaledges &&!horizontaledges) {
Step 3, Draw the content
if (!dirtyopaque) OnDraw (canvas);

Step 4, Draw the children
Dispatchdraw (canvas);

Step 6, Draw decorations (scrollbars)
Ondrawscrollbars (canvas);

We ' re done ...
Return
}

3. Observer mode

Listener are the equivalent of an observer, observing the response of a number of events and handling them when they are found to have occurred.

4. Single-Case mode

Application class

5. Adapter Mode

Common adapter classes in Android are: Baseadapter, simpleadapter, etc.

6. Factory mode

Bitmap Bitmap=bitmapfactory.decoderesource (Getresources (), R.drawable.ic_action_search)

Bitmapfactory, as the name implies, is the bitmap factory, specifically used to convert the specified image to the specified bitmap bitmap. Because the different picture source may cause the picture size, the format type and so on the variety, thus causes the generation target object The complexity, therefore through the factory unified processing to the same size, the type "standard", greatly simplifies the code complexity and the workload.

7. Proxy mode

Using Aidl to define a remote service in an Android system requires proxy mode.

8. Enjoy meta mode

9. Builder Mode Builders

such as Uri.builder,alertdialog.builder. Builder constructs a Alertdialog object in Create () by means of Settitle (), Setmessage (), SetIcon (), and so on.
The client can then show out the Alertdialog object.

10. Policy mode

Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other.

android.animation.Interpolator  Animation The realization of different animation, mainly depends on the different realization of interpolator change.

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.