"Design Patterns"-Android design mode-combination mode self-insight

Source: Internet
Author: User

wirelessly to the combination mode that is the use of the view and the ViewGroup class, all the basic widgets and layout classes rely on these two classes.

The combination mode, Composite pattern, is a very ingenious pattern. Almost all object-oriented systems are applied to the combined mode .

According to the meaning of the picture, I will combine their understanding, wrote a demo, there is nothing wrong place, please pay attention to, pointing under.

The following is a picture from the network



Demo: The function is as follows: Please point out the question of understanding.

Animals as a base class, man, pig, all inherit all his attributes, and have their own unique properties and methods. (equivalent to button\textview these leaf nodes)

They had a common room, a door in the room, and a real-time management operation for their entry and exit. (equivalent to ViewGroup unified management layout)

Animal.java

Package com.ferris.composite;/** * Ferris  * @author [email protected] * Animal */public class Animal {public class Attrib  Ute{public static final int people=0;//human public static final int cow=1;//cow public static final int sleep=2;//sheep public static final int pig=3;//pig}public Animal () {}protected int id; Animal's special idprotected String name;//animal's name protected int type;//animal type public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int GetType () {return type;} public void SetType (int type) {this.type = type;} Animal behavior method protected void operation () {}}

Personanimal.java people

Package Com.ferris.composite;public class Personanimal extends Animal {private string Zoulu;public string Getzoulu () {RET Urn Zoulu;} public void Setzoulu (String zoulu) {this.zoulu = Zoulu;} Public Personanimal () {//TODO auto-generated constructor stub}}

Piganimal.java Pig

Package Com.ferris.composite;public class Piganimal extends Animal {private string zhurou;//unique attribute, can be eaten public String Getzhurou () {return zhurou;} public void Setzhurou (String zhurou) {This.zhurou = Zhurou;} Public Piganimal () {//TODO auto-generated constructor stub}}

Door.java

Package Com.ferris.composite;import java.util.arraylist;/** * This is a door that manages this entry and exit * @author Administrator * */public interface do or {public void Addanimal (Animal Animal);//Add an animal public void Removeanimal (Animal Animal);//Remove an animal public void Removeallanimal ();//Remove all animal public int getanimalcount ();//Get so the number of animals public arraylist<animal> getanimals ();}


House.java room, implements an interface door

Package Com.ferris.composite;import java.util.arraylist;/** * room, door managed so animals in and out * @author Administrator * */public class Ho Use implements Door{private arraylist<animal> animals=new arraylist<animal> ();p rivate static House house; public static house Gethouseinstance () {if (house==null) {synchronized (House.class) {if (house==null) {return new house () ;}}} return house;} @Overridepublic void Addanimal (Animal Animal) {//TODO auto-generated method Stubif (!animals.contains (Animal)) { Animals.add (animal);}} @Overridepublic void Removeanimal (Animal Animal) {//TODO auto-generated method Stubif (Animals.contains (Animal)) { Animals.remove (animal);}} @Overridepublic void Removeallanimal () {//TODO auto-generated method Stubif (Animals.size () >0) {animals.clear ();}} @Overridepublic int Getanimalcount () {//TODO auto-generated method Stubreturn animals==null?0:animals.size ();} @Overridepublic arraylist<animal> getanimals () {//TODO auto-generated method Stubreturn animals;}}



Mainactivity.java

package Com.ferris.androidpattern;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.widget.toast;import Com.ferris.composite.animal.attribute;import Com.ferris.composite.house;import Com.ferris.composite.personanimal;import Com.ferris.composite.piganimal;public class MainActivity extends fragmentactivity {private house house; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); house=house.gethouseinstance ();//Get a room// The man entered the room Personanimal json=new personanimal (); Json.setid (1); Json.setname ("I am Human"); Json.settype (attribute.people); House.addanimal (JSON);//A person enters the room piganimal pig=new piganimal ();p Ig.setid (2);p ig.setname ("I Am a pig");p Ig.settype ( Attribute.pig); House.addanimal (pig);//The Pig also entered the room Toast.maketext (mainactivity.this, "How many animals in the room:" +house.getanimalcount (), 1). Show ();}} 






"Design Patterns"-Android design mode-combination mode self-insight

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.