Android [advanced tutorial] Eight bridges in Design Mode

Source: Internet
Author: User

Now let's talk about the bridge mode. For example, the bridge mode is like building blocks when I was a child. Each block has its own shape, however, they can be combined to implement different styles. We still use the main character in Journey to the West to explain this model. Wukong uses a golden hoop, the eight rings use a nine-tooth dingtalk, and the sand monk uses a Demon Rod. Each weapon is different, let's take a look at the example:


Well, we will first define a weapon interface:

public interface Weapon{public String getWeapon();}

Then we define a person's interface. Here there is a method to use weapons. here we need to make it clear that people use weapons rather than weapons, so we put the interface class in person:

public interface Person {public void setWeapon(Weapon weapon);}

After the interface is defined, it is the specific implementation method of each interface:

Let's first define three weapons:

Golden hoop

Public class jingubang implements weapon {private string weapon; Public jingubang () {This. Weapon = "golden hoop" ;}@ overridepublic string getweapon () {return weapon ;}}

Nine-tooth dingtalk

Public class jiuchidingpa implements weapon {private string weapon; Public jiuchidingpa () {This. Weapon = "Nine teeth dingtalk" ;}@ overridepublic string getweapon () {return weapon ;}}

The last thing is to fight the demon

Public class xiangyaobaozhang implements weapon {private string weapon; Public xiangyaobaozhang () {This. Weapon = "Demon promotion" ;}@ overridepublic string getweapon () {return weapon ;}}

After the three weapons are defined, the implementation of the three apprentice classes will be:

Public class Wukong implements person {private string name; private Weapon weapon; Public Wukong () {This. name = "Wukong" ;}@ overridepublic void setweapon (Weapon weapon) {This. weapon = weapon;} @ overridepublic string tostring () {return name + "use" + weapon. getweapon ();}}

The definition defines what kind of weapon Wukong needs to use to fight monsters, but which one does not have a specific description. The same is true for the eight rings and the swordsmanship. Here we will not list them one by one. Finally, let's take a look at the implementation in Android:

Public class xiyoujiactivity extends activity {private listview;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) findviewbyid (R. id. listview1); // three apprentice classes: person Wukong = new Wukong (); person Bajie = new Bajie (); person shasheng = new shaseng (); // weapon jingubang = new jingubang (); weapon jiuchidingpa = new jiuchidingpa (); weapon xiangyaobaozhang = new xiangyaobaozhang (); // set Sun Wukong's weapon to "golden hoop" Wukong. setweapon (jingubang); // set the eight-ring weapon to "nine-tooth dingtalk" Bajie. setweapon (jiuchidingpa); // set shasheng as the weapon of Sha Seng. setweapon (xiangyaobaozhang); arraylist <person> Persons = new arraylist <person> (); persons. add (Wukong); persons. add (Bajie); persons. add (shasheng); arrayadapter <person> adapter = new arrayadapter <person> (this, android. r. layout. simple_list_item_1, persons); listview. setadapter (adapter );}}

The comments in it are clearly written. You should be able to understand them. Of course, you can also use other Wukong weapons. OK. The result is as follows:

As in the past, we will rebuild it here, because the variables and methods in the specific class are the same. We will change the person Method to the image class so that Wukong, Bajie, and sashan can inherit it, in this way, we only need to set the name for each specific class, and we do not need to write any other code. Let's take a look at the modified Code: person class.

Public abstract class person {private string name; private Weapon weapon; Public void setname (string name) {This. name = Name;} public void setweapon (Weapon weapon) {This. weapon = weapon;} @ overridepublic string tostring () {return name + "use" + weapon. getweapon ();}}

The following describes how to implement specific classes, and then the classes of Wukong, Bajie, and sashan. You must have been shocked. They have only one line of code:

Public class Wukong extends person {public Wukong () {This. setname ("Wukong ");}}

That is, you can set the name for each person. Others are implemented by dependency inheritance. The other two are the same as those of Wukong. The final activity is the above activity. There is no need to modify any code in the activity, and the final implementation is the same as the figure above. Here we will introduce the bridge mode, and restructured our code. Thank you again for your attention.


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.