Design JAVA mode (mode combinations)

Combined mode    

Combining objects in a tree structure to represent "part - whole" hierarchy. Composite having a consistency such that a single user objects and combinations of objects.
 
Composite Pattern       

Compose objects into tree structures to represent partwhole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

I. Overview  

    Combined mode is about how the objects form a tree structure to represent the overall maturity model and hierarchy section. Using a combination of modes, allowing users in a consistent manner individual objects and combinations of objects, the key lies in the combination mode, whether an individual object or combination of objects all implement the same interface or an abstract class are the same subclass. 

Second, the structure and mode combination 

The structural model includes three roles:
• abstract component (the Component) 
• Composite node (the Node Composite) 
• Leaf node (Leaf Node)

UML class diagrams

achieve

1. Abstract component (Component): MilitaryPerson.java

import java.util.*; 
public interface MilitaryPerson{ 
      public void add(MilitaryPerson person) ; 
      public void remove(MilitaryPerson person) ; 
      public MilitaryPerson getChild(int index);  
      public Iterator<MilitaryPerson>  getAllChildren() ; 
      public boolean isLeaf(); 
      public double getSalary(); 
      public void setSalary(double salary); 
  }  

2. Composite节点(Composite Node): MilitaryOfficer.java

import java.util.*; 
public class MilitaryOfficer implements MilitaryPerson{ 
      LinkedList<MilitaryPerson> list;
       String name;
       double salary; 
      MilitaryOfficer(String name,double salary){
             this.name=name;
             this.salary=salary;
             list=new LinkedList<MilitaryPerson>();
       }
        public void add(MilitaryPerson person) {
             list.add(person);
       }
       public void remove(MilitaryPerson person){
             list.remove(person);
       }
       public MilitaryPerson getChild(int index) {
             return list.get(index);
        }
       public Iterator<MilitaryPerson>  getAllChildren() {
             return list.iterator();
        }
       public boolean isLeaf(){
            return false;
       }
        public double getSalary(){
             return salary;
       } 
      public void setSalary(double salary){
            this.salary=salary;
       }
 } 

 3. Leaf node (Leaf Node): MilitarySoldier.java

import java.util.*; 
public class MilitarySoldier implements MilitaryPerson{ 
 double salary; 
 String name; 
 MilitarySoldier(String name,double salary){ 
 this.name=name; 
 this.salary=salary; 
 } 
 public void add(MilitaryPerson person) {} 
 public void remove (MilitaryPerson person){} 
 public MilitaryPerson getChild(int index) { 
 return null; 
 } 
 public Iterator<MilitaryPerson> getAllChildren() { 
 return null; 
 } 
 public boolean isLeaf(){ 
 return true; 
 } 
 public double getSalary(){ 
 return salary; 
 } 
 public void setSalary(double salary){ 
 this.salary=salary; 
 } 
}

4. Application _1: ComputerSalary.java

import java.util.*; 
public class ComputerSalary{ 
 public static double computerSalary(MilitaryPerson person){ 
 double sum=0; 
 if(person.isLeaf()==true){ 
 sum=sum+person.getSalary(); 
 } 
 if(person.isLeaf()==false){ 
 sum=sum+person.getSalary(); 
 Iterator<MilitaryPerson> iterator=person.getAllChildren(); 
 while(iterator.hasNext()){ 
 MilitaryPerson p= iterator.next(); 
 sum=sum+computerSalary(p);; 
 } 
 } 
 return sum; 
 } 
}

 

Guess you like

Origin www.cnblogs.com/sunupo/p/11222255.html