Android design mode source code parsing bridge mode

Source: Internet
Author: User

Definition of Pattern Introduction pattern

Separate the abstractions from the implementation, so that they can be changed independently.

Usage Scenarios for patterns
    • If a system needs to add more flexibility between the abstract and materialized roles of the artifacts, avoid establishing a static connection between the two levels.
    • Design requires that any changes to the implementation role should not affect the client, or that changes to the implementation role are completely transparent to the client.
    • Graphics and windowing systems that need to span multiple platforms.
    • There are two independently varying dimensions for a class, and two dimensions need to be expanded.
UML Class Diagram

Role description
    • Abstract (abstraction) Role: Abstract The given definition and save a reference to the implemented object. Fix abstract (refined abstraction) roles: Extend abstract roles, change and modify the definition of the parent class for abstraction.
    • Implementation-Implementor role: This role gives the interface to the implementation of the role, but does not give a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of the abstract role, in fact, the two interfaces can be very different. The implementation role should only give the underlying operation, and the abstract role should give only a higher level of operation based on the underlying operation.
    • Implementation-specific (Concreteimplementor) role: This role gives a concrete implementation of the implementation of the role interface.
Introduction to the simple implementation of the pattern

In fact, Java Virtual machine is a good example, on different platform platforms, with different virtual machines to implement, so that only the Java program compiled to conform to the virtual machine specification files, and only compile once, it can work on different platforms. But this is more abstract, using a simple example to implement bridge mode.

Write a program that uses one of the two drawing's programs to draw a rectangle or prototype, and when instantiating a rectangle, it knows whether to use the Drawing program 1 (DP1) or the Drawing Program 2 (DP2).

(PS: Suppose that DP1 and DP2 are drawn differently, they are drawn in different ways, the sample code does not discuss too much detail)

Implementation of the source code is the first two drawing programs dp1,dp2//specific drawing program class DP1public class DP1 { public void Draw_1_rantanle () { System.out. println ("Draw a rectangle using the DP1 program" ); }public void draw_1_circle () { System.out. println ("Draw a circle using the DP1 program" ); }
}//Specific Drawing program class DP2
public class DP2 { public void Drawrantanle () { System.out. println ("Draw a rectangle using the DP2 program" ); }public void drawcircle () { System.out. println ("Draw a circle using the DP2 program" ); }
}
Then the abstract shapeshape and two derived classes: rectangular Rantanle and Circle CircleAbstract role Abstractionabstract class Shape { //Hold the implemented role Implementor (drawing Class)protected Drawing mydrawing; public Shape (Drawing Drawing) { this. mydrawing = drawing;
    }
abstract public void Draw (); //Protection method DrawRectangleprotected void DrawRectangle () { //this.impl.implmentation ()Mydrawing. Drawrantangle ();
    }
//Protection method Drawcircleprotected void drawcircle () { //this.impl.implmentation ()Mydrawing. Drawcircle ();
    }}//fix abstract role refined abstraction (rectangle)
public class Rantangle extends shape{ public rantangle (Drawing Drawing) { super (Drawing);
}
@Overridepublic void Draw () { DrawRectangle ();
}}//fix abstract role refined abstraction (round)
public class Circle extends Shape { public Circle (Drawing Drawing) { super (Drawing);
}
@Overridepublic void Draw () { Drawcircle ();
}}
Finally, our implementation of the drawingdrawing and the v2drawing of DP1 v1drawing and DP2 respectively Implementing a role implementor//implmentation two ways to draw and draw rectanglespublic Interface Drawing { public void drawrantangle (); public void drawcircle ();
}//Concrete implementation of logic Concreteimplementor
Implements the interface method and uses DP1 to drawpublic class v1drawing implements drawing{ DP1 DP1;public v1drawing () { Dp1= new DP1 (); }@Overridepublic void Drawrantangle () { Dp1. Draw_1_rantanle ();
}
@Overridepublic void drawcircle () { Dp1. Draw_1_circle ();
}
Concrete implementation of Logic Concreteimplementor
}//Implements an interface method to draw with DP2
public class v2drawing implements drawing{ DP2 DP2;public v2drawing () { Dp2= new DP2 (); }@Overridepublic void Drawrantangle () { Dp2. Drawrantanle ();
}
@Overridepublic void drawcircle () { Dp2. Drawcircle ();
}
}

? In this example, the graph shape class has two types, circle and rectangle, in order to draw a graph with different drawing programs, the implementation part is separated, which forms the drawing class hierarchy, including V1drawing and v2drawing. In a specific implementation class, v1drawing controls the DP1 program to draw, v2drawing controls the DP2 program to draw, and the method of protection Drawrantangle,drawcircle (Shape Class).

Mode implementation in Android source code

In Android also used in bridge mode, we use a lot of ListView and Baseadpater is actually bridge mode operation, many people will ask this is not adapter mode, next according to the source code to analyze.

First Listadapter.java:

public Interface listadapter extends adapter{ //Inherit from adapter, extend your own two implementation methodspublic Boolean areallitemsenabled (); boolean isenabled (int position); }

Here, let's look at the parent class Adapterview.

Public Abstract class adapterview<t extends adapter> extends viewgroup { /c13> //A generic adapter is required herepublic abstract T getadapter (); public abstract void setadapter (T adapter); }

Then look at the parent of the ListView Abslistview, inherited from Adapterview

Public Abstract class Abslistview extends adapterview<listadapter> //Inherit from Adapterview, and indicate T is ListAdapter/*** The adapter containing the data to being displayed by this view*/ListAdapter Madapter;//Code omission//The Setadapter method is implemented here, and a reference to the materialized object is given.public void Setadapter (ListAdapter Adapter) {//The adapter is coming in from a subclass, which is the ListView, which gets the concrete realization of the objectif (adapter! = null) { Madapterhasstableids= Madapter. Hasstableids (); if (mchoicemode! = choice_mode_none && madapterhasstableids && Mcheckedidstates= = null) { Mcheckedidstates= new longsparsearray<integer> (); }
        }
if (mcheckstates! = null) { Mcheckstates. Clear ();}if (mcheckedidstates! = null) { Mcheckedidstates. Clear ();}}

As we all know, build a listview,adapter of the two most important methods, GetCount () inform the number, GetView () Tell the specific view type, Let's take a look at how the Abslistview as a collection of views is based on the implementation of the object adapter to achieve the specific view?

protected void Onattachedtowindow () { Super. Onattachedtowindow (); //Omit code,//When joining window, GetCount () determines the number of setsMdatachanged= true; Molditemcount= Mitemcount;Mitemcount= Madapter. GetCount ();
        }
}

Then see

View Obtainview (int position, boolean[] isscrap) { //Code omission?//Here according to the location of the specific View,return child is from the holding of the implementation object Madapter inside the concrete implementation of?//Method GetView to get. final View child = madaptergetView (position, Scrapview, this ); //Code omissionreturn child;}

Next in the ListView, Onmeasure calls the Obtainview to determine the width and height, extending its own method to arrange the view. Got it

After these, let's draw a simple UML diagram to look at:

Compared to the GOF, is not found very much like it? In fact, the beginning of the study of the adapter model, the more you look at the wrong ah, so the structure, drew UML found that it is more like a bridge mode, then the design pattern is also vague, so quiet down to study. Abstract role A collection of views Adapterview, which expands the Abslistview,absspinner, and then expands the listview,gridview,spinner,gallery, respectively, In different ways to show these itemviews, we continue to expand the Pulltorefreshview like the ListView and so on. The implementation of the role adapter extended the Listadpater,spinneradapter, and then the specific implementation of the role Baseadapter realize them, we inherit baseadapter and realize our various itemview.

Zatan

This is where the Android engineer's Bull X is, with a bridge and adapter to solve a big problem. Imagine that the view is arranged in an infinite way, and the view that is developed by each person is endless. If you develop normally, how many classes do you need to complete? Android has encapsulated the most commonly used presentation, while implementing the role through adapter mode to strain an endless view. Abstract a container that uses adapters to add views to the container, the shape of the container (or the way it is understood to be displayed), and how to draw the view inside the container, you can change on your own, without interfering, true decoupling, and you'll start by saying, "separate the abstract from the implementation, So that they can all change independently. ”

From the two cases above, we can see that we use Bridge and adapter mode in two solutions, because we have to use a given drawing program (adapter adapter), the drawing program (adapter adapter) has an existing interface must be followed, So it's necessary to use adapter for adaptation before they can be processed in the same way, they are often used together and similar, but adapter is not part of bridge.

Advantages and Disadvantages

The implementation is decoupled from the use of the implemented object, providing extensibility, and customer objects do not have to worry about the implementation of the operation. If you use bridge mode, it will be very easy to handle the new implementation. You just need to define a new concrete implementation class, and implement it just fine, without having to modify anything else. But if you have a new situation, you need to modify the implementation, you have to modify the abstract interface, and then modify its derived classes, but this change will only exist in the local, and this modification will change the hero to control the local, and reduce the risk of side effects, and the relationship between the class is very clear, How to achieve at a glance.

Android design mode source code parsing bridge mode

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.