[Design pattern] Android design pattern-combination pattern self-opinion, android Design Pattern
In Android, the combination mode is the use of the View and ViewGroup classes. Basically, all widgets and layout classes depend on these two classes.
Composite Pattern is a very clever mode. Almost all object-oriented systems are applied to the combination mode.
According to the meaning of the image, I wrote a demo based on my own understanding. What's wrong with it? Please pay attention and give me some advice.
The following picture is from the network
Demo: The function is as follows: to understand the problem, please point out.
An animal, as a base class, inherits all its attributes and has its own unique attributes and methods. (Equivalent to the leaf nodes such as Button \ textview)
They have a common room, door in the room, and manage their access in real time. (Equivalent to the unified management layout of ViewGroup)
Animal. java
Package com. ferris. composite;/*** ferris * @ author 459821731@qq.com * Animal class */public class Animal {public class Attribute {public static final int PEOPLE = 0; // human public static final int COW = 1; // Niu public static final int SLEEP = 2; // Yang public static final int PIG = 3; // pig} public Animal () {} protected int id; // The Special idprotected String name of the Animal; // The Animal 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
package com.ferris.composite;public class PersonAnimal extends Animal {private String zoulu;public String getZoulu() {return 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. 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. Manage the entry and exit * @ author Administrator **/public interface Door {public void addAnimal (Animal animal ); // Add an Animal public void removeAnimal (animal Animal); // remove an animal public void removeAllAnimal (); // remove all animals public int getAnimalCount (); // obtain the number of animals as public ArrayList <Animal> getAnimals ();}
House. java room, implements an interface door
Package com. ferris. composite; import java. util. arrayList;/*** room, the Door manages the entry and exit of animals * @ author Administrator **/public class House implements Door {private ArrayList <Animal> animals = new ArrayList <Animal> (); private static House; public static House getHouseInstance () {if (house = null) {synchronized (House. class) {if (house = null) {return new House () ;}} return house ;}@ Overridepublic void addAnimal (Animal Imal) {// 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 person who enters the room has the PersonAnimal json = new PersonAnimal (); json. setId (1); json. setName ("I am a person"); json. setType (Attribute. PEOPLE); house. addAnimal (json); // a person enters the room with PigAnimal pig = new PigAnimal (); pig. setId (2); pig. setName ("I am a pig"); pig. setType (Attribute. PIG); house. addAnimal (pig); // pig also enters the room Toast. makeText (MainActivity. this, "how many animals are there in the room:" + house. getAnimalCount (), 1 ). show ();}}