Combination Mode: allows you to combine objects into a tree structure to express the "whole/part" hierarchy. The combination allows the customer to process individual objects and object combinations in a consistent manner.
Case Scenario: My friend and I went to the supermarket to shop separately. During the checkout, we traversed our "goods scanning" list (print and print the shopping list, me uses arraylist, and friends use the thing [] array, which causes traversal problems.
The complimentary items were found during the time period and were not printed. Now we have the need to print free items.
Abstractthing. Java
Public abstract class implements actthing {public abstract string tostring (); Public void add (implements actthing thing) {}; public void remove (implements actthing thing ){};}
Thing. Java
Import Java. util. arraylist; import Java. util. iterator; public class thing extends abstractthing {private arraylist <abstractthing> List = new arraylist <abstractthing> (); Private string name; private long price; public thing (string name, long price) {This. name = Name; this. price = Price ;}@ overridepublic string tostring () {string returnstring = "thing [name =" + name + ", price =" + Price + "]"; for (iterator <abstractthing> iterator = List. iterator (); iterator. hasnext ();) {returnstring + = iterator. next () ;}return returnstring ;}@ overridepublic void add (abstractthing thing) {list. add (thing) ;}@ overridepublic void remove (abstractthing thing) {list. remove (thing );}}
Thingitem. Java
Public class thingitem extends actthing {private string name; private long price; Public thingitem (string name, long price) {This. name = Name; this. price = Price ;}@ overridepublic string tostring () {return "Complimentary [name =" + name + ", price =" + Price + "]" ;}}
Arrayiterator. Java
Import Java. util. iterator; public class arrayiterator implements iterator <thing> {private thing [] thingarray; private int position = 0; Public arrayiterator (thing [] array) {thingarray = array ;} @ overridepublic Boolean hasnext () {While (position <thingarray. length & thingarray [position]! = NULL) {return true;} return false;} @ overridepublic thing next () {return thingarray [position ++] ;}@ overridepublic void remove () {// noting to do }}
Me. Java
Import Java. util. arraylist; import Java. util. iterator; public class me {private arraylist <thing> shopping = new arraylist <thing> (); Public me () {shopping. add (new thing ("Chocolate", 35); shopping. add (new thing ("herbal tea", 4); shopping. add (new thing ("so", 9); shopping. add (new thing ("banana", 12); // buy a computer and give a mouse, headset, keyboard thing computer = new thing ("computer", 3000); computer. add (New thingitem ("Mouse", 0); computer. add (New thingitem ("headset", 0); computer. add (New thingitem ("keyboard", 0); shopping. add (Computer);} public iterator <thing> createiterator () {return shopping. iterator ();}}
Friends. Java
Import Java. util. iterator; public class friends {private thing [] Shopping = new thing [100]; Public friends () {shopping [0] = new thing ("milk", 15 ); shopping [1] = new thing ("apple", 10); shopping [2] = new thing ("Potato Chips", 4 ); // buy a mobile phone and send a Bluetooth headset thing phone = new thing ("Mobile Phone", 2000); thingitem incluthheadset = new thingitem ("Bluetooth headset", 0); phone. add (FIG); shopping [3] = phone;} public iterator <thing> createiterator () {return New arrayiterator (shopping );}}
Test. Java
Import Java. security. allpermission; import Java. util. arraylist; import Java. util. iterator; public class test {public static void main (string ARGs []) {arraylist <iterator <thing> alliterator = new arraylist <iterator <thing> (); me me = new me (); friends = new friends (); iterator <thing> meiterator = me. createiterator (); iterator <thing> friendsiterator = friends. createiterator (); alliterator. add (meiterator); alliterator. add (friendsiterator); For (iterator <thing> iterator: alliterator) {While (iterator. hasnext () {system. out. println (iterator. next ();} system. out. println ("---------------------");}}}
Test results:
Thing [name = chocolate, price = 35] thing [name = herbal tea, price = 4] thing [name = so, price = 9] thing [name = banana, price = 12] thing [name = computer, price = 3000] complimentary [name = mouse, price = 0] complimentary [name = headset, price = 0] complimentary [name = keyboard, price = 0] ------------------- thing [name = milk, price = 15] thing [name = apple, price = 10] thing [name = potato chips, price = 4] thing [name = mobile phone, price = 2000] complimentary [name = Bluetooth headset, price = 0] -------------------