Software Design Patterns and Architecture Experiments——3.1-1 Application of Combination Patterns

Link: Software Design Pattern and Architecture Experiment——2.1-1 (2) (Abstract) Application of Factory Pattern
Link: Software Design Pattern and Architecture Experiment——2.2-1 Application of Builder Pattern
Link: Software Design Pattern and System Structural experiment——2.3-1 Application of single-column mode

1. Experimental purpose

  1. Master the characteristics of the combination mode
  2. Analyze specific problems and use patterns for design.

2. Experimental content

[Assignment 3.1-1] In the design of Example 3.3, add a Wing class, which is parallel to the Squadron and Group classes, so it should inherit the AirUnit class. The writing method of this class is similar to that of Squadron or Group, the difference is that a Wing has 216 types of aircraft. For specific requirements and code implementation, refer to the corresponding job part of the CD.

3. Schema UML Diagram

insert image description here

insert image description here
insert image description here
The design class diagram of the design program of this topic, and other diagrams:
insert image description here

4. Mode add code (JAVA language implementation)

(1) Wing class

package com.glut.xusheng;


/*=======================*/
/* Represents an air Wing      */
/*=======================*/

public class Wing extends AirUnit{
    
    

    public static final String FEATURES = "A Wing with 216 aircrafts";
    Airforce[] fighters = new Airforce[162];
    Airforce[] bombers = new Airforce[18];
    Airforce[] transporters= new Airforce[18];
    Airforce[] eAircrafts = new Airforce[18];

    public Wing(){
    
    
       for(int k=0;k<162;k++){
    
    
           // need 162 fighters
      }
       for(int k=0;k<18;k++){
    
    
           // need 18 bombers
      }
       for(int k=0;k<18;k++){
    
    
           // need 18 transporters
      }
       for(int k=0;k<18;k++){
    
    
           // need 18 eAirplanes
      }
    }

    public String getDescription(){
    
    
   return FEATURES;
    }

    public String fight(){
    
    
       return super.fight();
   }
}

(2) GUI class:

private String[] AirForceUnit = {
    
    "SQUADRON", "GROUP", "WING"};
add(1, 6, airCheckBox[13]);
else if ((m==13) && (ckBoxStates[13] == SELECTED)){
    
    
   unit = new Wing();
   airUnits.attach(unit);
   unitInfo = unit.getDescription();
}

5. Overall code

(1) AirforceGUI and Class A

AirforceGUI class

package com.glut.xusheng;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Iterator;

/*===================================================================*/
/* User interface for Testing the Composite design pattern program   */
/*===================================================================*/
public class AirforceGUI extends JFrame implements ItemListener{
    
    
	private JScrollPane checkBoxPane, textPane;
	private JSplitPane  splitPane;
	private JTextArea   txtAirMission;
	private JButton submitBtn, exitBtn;
	private JPanel checkBoxPanel, btnPanel, choicePanel;
	private JCheckBox[] airCheckBox;
	private JCheckBox[] airUnitCheckBox;
	private String[] airPlaneName ={
    
    "F-15E-Strike-Eagle","F-16C/D-Fighting-Falcon","F22A-Rapter",
			"B-1B-Lancer","B-2A-Spirit","B-52H-Stratofortress",
			"C-130E/H-Hercules","C-130J-Super-Hercules","CV-22B-Osprey",
			"E-9A",  "EC-130H/J-Compass", "AirForce","Fighters",
			"Bombers", "Transporters", "E-Planes"};
	//jia
	private String[] AirForceUnit = {
    
    "SQUADRON", "GROUP", "WING"};

	public final int SELECTED = ItemEvent.SELECTED;
	public final int DESELECTED = ItemEvent.DESELECTED;
	private static final String[] OPTION = {
    
    "Solo", "Group"};
	private JComboBox[] optComoBox=new JComboBox[11];
	private int[] ckBoxStates;

	private GridBagLayout gridbag = new GridBagLayout();
	private GridBagConstraints gbc = new GridBagConstraints();

	public AirforceGUI(){
    
    
		super("Composite Pattern - Airforce");
		ckBoxStates = new int[50];
		setUpChoicePanel();
		setUpScrollPanes();
	}
	private void setUpChoicePanel(){
    
    
		submitBtn = new JButton("Submit");
		exitBtn = new JButton("Exit");
		submitBtn.addActionListener( new ButtonActionListener());
		exitBtn.addActionListener( new ButtonActionListener());
		JPanel btnPanel =new JPanel();
		btnPanel.add(submitBtn);
		btnPanel.add(exitBtn);
		int numCheckBox = airPlaneName.length;
		airCheckBox = new JCheckBox[numCheckBox];
		airUnitCheckBox = new JCheckBox[3];
		//Check boxes for selection of the airplanes
		for(int m=0; m<numCheckBox; m++){
    
    
			airCheckBox[m]= new JCheckBox(airPlaneName[m]);
			airCheckBox[m].setMnemonic(KeyEvent.VK_C);
			airCheckBox[m].addItemListener(this);
		}
		//Check boxes for selection of air units
		for(int m=0; m<3; m++){
    
    
			airUnitCheckBox[m]= new JCheckBox(AirForceUnit[m]);
			airUnitCheckBox[m].setMnemonic(KeyEvent.VK_C);
			airUnitCheckBox[m].addItemListener(this);
		}
		//Combobox for deciding on solo flight or not
		for(int i=0;i<11;i++){
    
    
			optComoBox[i]= new JComboBox(OPTION);
			optComoBox[i].addItemListener(this);
		}
		checkBoxPanel = new JPanel();
		checkBoxPanel.setLayout(gridbag);

		for(int m=0; m<numCheckBox; m++)
			checkBoxPanel.add(airCheckBox[m]);
		for(int m=0; m<3; m++)
			checkBoxPanel.add(airUnitCheckBox[m]);
		for(int i=0;i<11;i++)
			checkBoxPanel.add(optComoBox[i]);

		gbc.insets.top = 0;
		gbc.insets.bottom = 0;
		gbc.insets.left = 0;
		gbc.insets.right = 0;
		gbc.anchor = GridBagConstraints.WEST;
		add(0, 0, airCheckBox[11]);
		add(1, 1, airCheckBox[12]);
		add(2, 3, airCheckBox[0]);
		add(2, 4, airCheckBox[1]);
		add(2, 5, airCheckBox[2]);
		//
		add(1, 6, airCheckBox[13]);
		add(2, 7, airCheckBox[3]);
		add(2, 8, airCheckBox[4]);
		add(2, 9, airCheckBox[5]);
		add(1, 10, airCheckBox[14]);
		add(2, 11, airCheckBox[6]);
		add(2, 12, airCheckBox[7]);
		add(2, 13, airCheckBox[8]);
		add(1, 14, airCheckBox[15]);
		add(2, 15, airCheckBox[9]);
		add(2, 16, airCheckBox[10]);
		add(3, 3, optComoBox[0]);
		add(3, 4, optComoBox[1]);
		add(3, 5, optComoBox[2]);
		add(3, 7, optComoBox[3]);
		add(3, 8, optComoBox[4]);
		add(3, 9, optComoBox[5]);
		add(3, 11, optComoBox[6]);
		add(3, 12, optComoBox[7]);
		add(3, 13, optComoBox[8]);
		add(3, 15, optComoBox[9]);
		add(3, 16, optComoBox[10]);
		add(0, 17, airUnitCheckBox[0]);
		add(0, 18, airUnitCheckBox[1]);
		add(0, 19, airUnitCheckBox[2]);

		choicePanel = new JPanel();
		choicePanel.setMinimumSize(new Dimension(500, 300));
		choicePanel.setLayout(new BorderLayout());
		choicePanel.add(checkBoxPanel, "Center");
		choicePanel.add(btnPanel, "South");
	}
	private void add(int m, int n, JComponent com ){
    
    
		gbc.gridx = m;
		gbc.gridy = n;
		gridbag.setConstraints(com, gbc);
	}
	private void setUpScrollPanes(){
    
    
		txtAirMission = new JTextArea(3,10);
		txtAirMission.setBackground(Color.cyan);
		txtAirMission.setText("选择军事单位:空军中队(SQUADRON),空军团(GROUP)或者空军大队(WING)"
				+ "\n组成固定单位的战斗力量。你也可以直接选择各种机型形成临时的编队"
				+"\n飞行(在组合框中选Group)或者单飞(在组合框中选Solo)。");
		checkBoxPane = new JScrollPane(choicePanel);
		textPane = new JScrollPane(txtAirMission);
		textPane.setMinimumSize(new Dimension(500, 100));

		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, checkBoxPane, textPane);
		splitPane.setDividerLocation(470);

		getContentPane().add(splitPane);
		setSize(new Dimension(500, 500));
		setVisible(true);
	}

	//========================================
	// 新增加功能,需要理解本监听器所涉及的方法。
	//========================================
	class ButtonActionListener implements ActionListener{
    
    
		public void actionPerformed(ActionEvent e) {
    
    
			txtAirMission.setText("\n===Airforce New Mission===\n");
			createAirGroup(e);
		}
	}
	public void itemStateChanged(ItemEvent e){
    
    
		Object source = e.getItemSelectable();
		int state = e.getStateChange();

		if (source == airCheckBox[11]) {
    
    
			if(state == SELECTED){
    
    
				for(int m=12; m<16; m++)
					airCheckBox[m].setSelected(true);
			}
			else if (state == DESELECTED){
    
    
				for(int m=12; m<16; m++)
					airCheckBox[m].setSelected(false);
			}
		}
		else if (source ==  airCheckBox[12]) {
    
    
			if(state == SELECTED){
    
    
				for(int m=0; m<3; m++)
					airCheckBox[m].setSelected(true);
			}
			else if (state == DESELECTED){
    
    
				for(int m=0; m<3; m++)
					airCheckBox[m].setSelected(false);
			}
		}
		else if (source == airCheckBox[0])
			ckBoxStates[0]=state;
		else if (source == airCheckBox[1])
			ckBoxStates[1]=state;
		else if (source == airCheckBox[2])
			ckBoxStates[2]=state;
		else if (source == airCheckBox[13]){
    
    
			if(state == SELECTED){
    
    
				for(int m=3; m<6; m++)
					airCheckBox[m].setSelected(true);
			}
			else if (state == DESELECTED){
    
    
				for(int m=3; m<6; m++)
					airCheckBox[m].setSelected(false);
			}
		}
		else if (source == airCheckBox[3])
			ckBoxStates[3]=state;
		else if (source == airCheckBox[4])
			ckBoxStates[4]=state;
		else if (source == airCheckBox[5])
			ckBoxStates[5]=state;
		else if (source == airCheckBox[14]){
    
    
			if(state == SELECTED){
    
    
				for(int m=6; m<9; m++)
					airCheckBox[m].setSelected(true);
			}
			else if (state == DESELECTED){
    
    
				for(int m=6; m<9; m++)
					airCheckBox[m].setSelected(false);
			}
		}
		else if (source == airCheckBox[6])
			ckBoxStates[6]=state;
		else if (source == airCheckBox[7])
			ckBoxStates[7] = state;
		else if (source == airCheckBox[8])
			ckBoxStates[8]=state;
		else if (source == airCheckBox[15]){
    
    
			if(state == SELECTED){
    
    
				airCheckBox[9].setSelected(true);
				airCheckBox[10].setSelected(true);
			}
			else if (state == DESELECTED){
    
    
				airCheckBox[9].setSelected(false);
				airCheckBox[10].setSelected(false);
			}
		}
		else if (source == airCheckBox[9])
			ckBoxStates[9]=state;
		else if (source == airCheckBox[10])
			ckBoxStates[10]=state;
			//== for air units
		else if (source == airUnitCheckBox[0])
			ckBoxStates[11]=state;
		else if (source == airUnitCheckBox[1])
			ckBoxStates[12]=state;
		else if (source == airUnitCheckBox[2])
			ckBoxStates[13]=state;
	}
	private void createAirGroup(ActionEvent e){
    
    
		Airforce airCraft = null;
		Airforce unit = null;
		AirUnit airGroup = new AirUnit();
		AirUnit airUnits = new AirUnit();

		boolean isSolo = false;
		int len = ckBoxStates.length;
		String unitInfo = null;

		if (e.getActionCommand().equals("Submit")) {
    
    
			for(int m = 0; m < len; m++ ){
    
    
				if ((m==0) && (ckBoxStates[0] == SELECTED)) {
    
    
					airCraft = new F15();
					if(optComoBox[0].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==1) && (ckBoxStates[1] == SELECTED)){
    
    
					airCraft = new F16();
					if(optComoBox[1].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==2) && (ckBoxStates[2] == SELECTED)){
    
    
					airCraft = new F22();
					if(optComoBox[2].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==3) && (ckBoxStates[3] == SELECTED)){
    
    
					airCraft = new B1B();
					if(optComoBox[3].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==4) && (ckBoxStates[4] == SELECTED)) {
    
    
					airCraft = new B2A();
					if(optComoBox[4].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==5) && (ckBoxStates[5] == SELECTED)){
    
    
					airCraft = new B52();
					if(optComoBox[5].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==6) && (ckBoxStates[6] == SELECTED)) {
    
    
					airCraft = new C130E();
					if(optComoBox[6].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==7) && (ckBoxStates[7] == SELECTED)) {
    
    
					airCraft = new C130J();
					if(optComoBox[7].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==8) && (ckBoxStates[8] == SELECTED)) {
    
    
					airCraft = new CV22();
					if(optComoBox[8].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==9) && (ckBoxStates[9] == SELECTED)) {
    
    
					airCraft = new E9A();
					if(optComoBox[9].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				else if ((m==10) && (ckBoxStates[10] == SELECTED)) {
    
    
					airCraft = new EC130();
					if(optComoBox[10].getSelectedItem().equals("Solo"))
						isSolo = true;
				}
				//== for air units
				else if ((m==11) && (ckBoxStates[11] == SELECTED)){
    
    
					unit = new Squadron();
					airUnits.attach(unit);
					unitInfo = unit.getDescription();
				}
				else if ((m==12) && (ckBoxStates[12] == SELECTED)){
    
    
					unit = new Group();
					airUnits.attach(unit);
					unitInfo = unit.getDescription();
				}
				else if ((m==13) && (ckBoxStates[13] == SELECTED)){
    
    
					unit = new Wing();
					airUnits.attach(unit);
					unitInfo = unit.getDescription();
				}

				if( airCraft != null){
    
    
					if(isSolo == false)
						airGroup.attach(airCraft);
					else{
    
    
						String f = airCraft.fight();
						txtAirMission.append("Solo Flight Mission: \n" + f + "\n");
					}
					airCraft = null;
					isSolo = false;
				}
			}  //end for loop

			//Display Air Group Actions
			if(airGroup.getSize() > 0){
    
    
				String str = airGroup.fight();
				txtAirMission.append("Mission with newly-formed unit: \n" + str + "\n");
			}
			if(airUnits.getSize() > 0){
    
    
				String str = airUnits.fight();
				txtAirMission.append("Mission with fixed unit: \n" + unitInfo + " \n");
				txtAirMission.append("Aircrafts in this mission: \n" + str + "\n");
			}
		}
		else if (e.getActionCommand().equals("Exit")) {
    
    
			System.exit(1);
		}
	}

	public static void main(String args[]){
    
    
		try {
    
    
			JFrame.setDefaultLookAndFeelDecorated(true);
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception evt) {
    
    }

		AirforceGUI frame = new AirforceGUI();
		frame.addWindowListener(new WindowAdapter(){
    
    
			public void windowClosing(WindowEvent e){
    
    
				System.exit(0);
			}
		});
		frame.setSize(500, 600);
		frame.setVisible(true);
	}
}

Airforce class

package com.glut.xusheng;

/*===================================*/
/* This is the interface of a class  */
/* hierarchy that is to be visited   */
/* by some visitors                  */
/*===================================*/

public abstract interface Airforce {
    
    
   public abstract String fight();
   public abstract String getDescription();
}

AirUnit class

package com.glut.xusheng;

import java.util.ArrayList;
import java.util.Iterator;

/*=====================================*/
/* This is the object structure  class */
/* in the class diagram of the visitor */
/* design pattern                      */
/*=====================================*/

public class AirUnit implements Airforce{
    
    

	private ArrayList<Airforce> parts;
	private String answer="";
	private String FEATURES = "Air Formation of Airforce. ";

	public AirUnit(){
    
    
		parts = new ArrayList<Airforce>();
    }

	public void attach(Airforce equip){
    
    
        if(equip != null)
		parts.add(equip);
    }

    public void detach(Airforce equip){
    
    
	    if(equip != null)
		    parts.remove(equip);
    }

	public Iterator<Airforce>  elements(){
    
    
	    return parts.iterator();
    }
	public String fight(){
    
    
		int len =parts.size();
		for (int i=0; i < len; i++){
    
    
			Airforce part = parts.get(i);
			answer = answer + part.fight()+"\n";
        }
        return answer;
	}

	public int getSize(){
    
    
	    return parts.size();
    }

    public String getDescription(){
    
    
		    return FEATURES;
	}
}

(2)B1B、B2A、B52、Bomber

B1B class

package com.glut.xusheng;/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class B1B extends Bomber{
    
    

	public static final String FEATURES = "B1B Lancer";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

B2A class

package com.glut.xusheng;


/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class B2A extends Bomber{
    
    

	public static final String FEATURES = "B2A Spirit";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

Category B52

package com.glut.xusheng;


public class B52 extends Bomber{
    
    

	public static final String FEATURES = "B52H StratoFortress";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

Bomber class

package com.glut.xusheng;


/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public abstract class Bomber implements Airforce{
    
    

	public static final String FEATURES = "Bombers ";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public abstract String fight();

}

(3)C130E、C130J、CV22

Class C130E

package com.glut.xusheng;


public class C130E extends Transporter{
    
    

	public static final String FEATURES = "C130EH Hercules";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

Class C130J

package com.glut.xusheng;

public class C130J extends Transporter{
    
    

	public static final String FEATURES = "C130J Super Hercules";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

CV22 class

package com.glut.xusheng;


public class CV22 extends Transporter{
    
    

	public static final String FEATURES = "CV22BO Sprey";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";
	}

}

(4)E9A、EC130、EPlane

Class E9A

package com.glut.xusheng;


/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class E9A extends EPlane{
    
    

	public static final String FEATURES = "E9A ";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

EC130 class

package com.glut.xusheng;



/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class EC130 extends EPlane{
    
    

	public static final String FEATURES = "EC130HJ Compass";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

EPlane class

package com.glut.xusheng;

/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public abstract class EPlane implements Airforce{
    
    

	public static final String FEATURES = "EPlanes ";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public abstract String fight();

}

(5)F15、F16、F22、Fighter

F15 category

package com.glut.xusheng;

/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class F15 extends Fighter{
    
    

	public static final String FEATURES = "F15E Strike Eagle";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

F16 category

package com.glut.xusheng;/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class F16 extends Fighter{
    
    

	public static final String FEATURES = "F16CD Fighting Falcon";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

F22 category

package com.glut.xusheng;

/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public class F22 extends Fighter{
    
    

	public static final String FEATURES = "F22A Rapter";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public String fight(){
    
    
		return FEATURES + " Ready to fight!";

	}

}

Fighter class

package com.glut.xusheng;

/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public abstract class Fighter implements Airforce{
    
    

	public static final String FEATURES = "Fighter Airplanes";


    public String getDescription(){
    
    
	    return FEATURES;
	}

	public abstract String fight();

}

(6)Group、Squadron、Transporter

Group class

package com.glut.xusheng;/*=======================*/
/* Represents an air Group    */
/*=======================*/

public class Group extends AirUnit{
    
    

    public static final String FEATURES = "A Group with 72 aircrafts";
    Airforce[] fighters = new Airforce[54];
    Airforce[] bombers = new Airforce[6];
    Airforce[] transporters= new Airforce[6];
    Airforce[] eAircrafts = new Airforce[6];

    public Group(){
    
    
       for(int k=0;k<54;k++){
    
    
           fighters[k] = new F22();
           super.attach(fighters[k]);
	   }
       for(int k=0;k<6;k++){
    
    
           bombers[k] = new B52();
           super.attach(bombers[k] );
	   }
       for(int k=0;k<6;k++){
    
    
           transporters[k] = new C130J();
	       super.attach(transporters[k]);
	   }
       for(int k=0;k<6;k++){
    
    
            eAircrafts[k] = new E9A();
            super.attach(eAircrafts[k]);
	   }
    }

    public String getDescription(){
    
    
	return FEATURES;
    }

    public String fight(){
    
    
	    return super.fight();
	}
}

Squadron class

package com.glut.xusheng;
/*==========================*/
/* Represents an air Squadron    */
/*==========================*/

public class Squadron extends AirUnit{
    
    

    public static final String FEATURES = "A Quadron with 24 aircrafts";
    Airforce[] fighters = new Airforce[18];
    Airforce[] bombers = new Airforce[2];
    Airforce[] transporters= new Airforce[2];
    Airforce[] eAircrafts = new Airforce[2];

    public Squadron(){
    
    
       for(int k=0;k<18;k++){
    
    
           fighters[k] = new F22();
           super.attach(fighters[k]);
	   }
       for(int k=0;k<2;k++){
    
    
           bombers[k] = new B52();
           super.attach(bombers[k] );
	   }
       for(int k=0;k<2;k++){
    
    
           transporters[k] = new C130J();
	       super.attach(transporters[k]);
	   }
       for(int k=0;k<2;k++){
    
    
            eAircrafts[k] = new E9A();
            super.attach(eAircrafts[k]);
	   }
    }

    public String getDescription(){
    
    
	return FEATURES;
    }

    public String fight(){
    
    
	    return super.fight();
	}
}

Transporter class

package com.glut.xusheng;

/*================================*/
/* Represents a kind of airforce  */
/*================================*/

public abstract class Transporter implements Airforce{
    
    

	public static final String FEATURES = "Transporters ";

    public String getDescription(){
    
    
	    return FEATURES;
	}

	public abstract String fight();
}

6. Running results

insert image description here
insert image description here

7. Experiment summary

(1) Definition of Composite Pattern:
sometimes called Part-Whole mode, it is a mode that combines objects into a tree-like hierarchy, used to represent "whole-part" The relationship between the user has consistent access to a single object and a composite object, which belongs to the structural design pattern.
The combination mode is generally used to describe the relationship between the whole and the part. It organizes objects into a tree structure. The top-level node is called the root node. The root node can contain branch nodes and leaf nodes, and the branch nodes can contain branch nodes. and leaf nodes, the tree structure diagram is as follows.
Combination mode is divided into three roles:
insert image description here

1. Component (abstract component): The parent class inherited by the leaf component and the container component or the interface implemented together, this role includes the declaration and implementation of the common methods of all subclasses, and the method of managing sub-components is defined in the abstract component , to add components, delete components, and obtain components.
2. Leaf (leaf component): Indicates a leaf node without child nodes. The method of managing child nodes inherited from the parent class is handled by throwing an exception.
3. Composite (container component): represents a container node, including sub-nodes, sub-nodes can be container nodes or leaf nodes, it provides a set to maintain sub-nodes, and process sub-nodes in an iterative manner.
The key of the combination mode is the abstract component class, which can represent both the leaf node and the container node. For the client, the abstract component is processed uniformly. The abstract component class and the container component class are aggregated and associated. The abstract component The class is a part of the container component class, so that the container component class does not need to distinguish whether it is a leaf node or a container node when processing sub-components, and can handle it uniformly.

(2) Precautions and details of combination mode:

1. Simplify client operations. Clients only need to deal with consistent objects without considering whole parts or node leaves.
2. It has strong scalability. When we want to change the combined object, we only need to adjust the internal hierarchical relationship, and the client does not need to make any changes.
3. It is convenient to create a complex hierarchical structure. The client does not need to care about the composition details in the combination, and can easily add nodes or leaves to create a complex tree structure.
4. When it is necessary to traverse the organization, or when the processed objects have a tree structure, it is very suitable to use the combination mode.
5. High abstraction is required. If there are many differences between nodes and leaves, such as many methods and attributes, it is not suitable to use the combination mode.

Guess you like

Origin blog.csdn.net/m0_52435951/article/details/125070800