Java - How to check if an object is an instance of a class in the class definition

时间:2015-05-12 23:26:52

标签: java iterator composite iterable

Code:

<cfquery name="InsertList" datasource="HHSCIntra">    
<cfloop list=#session.DistListID# index="recids">    
INSERT INTO tbl_DistListJunction (DistListID, userID)    
VALUES (<cfqueryparam value="#recids#" cfsqltype="cf_sql_int">, <cfqueryparam value="#session.listuser#" cfsqltype="cf_sql_varchar">)    
</cfloop>    
</cfquery>

I'm trying to do a breadth first print of a tree of the Composite design pattern. I have declared a method that is supposed to be called by the "root" of the tree. In the tree, there will be objects of the classes public class Composite extends Component implements Iterable<Component>{ private List<Component> caseComponents = new ArrayList<Component>(); @Override public Iterator<Component> iterator(){ return new CompIterator(); } private class CompIterator implements Iterator{ int index = 0; @Override public boolean hasNext(){ if(index<caseComponents.size()){ return true; } return false; } @Override public Object next() { if(this.hasNext()){ return caseComponents.get(index++); } return null; } } public String breadthFirst() { Queue<Component> q = new LinkedList<Component>(); StringBuilder stringRep = new StringBuilder(); q.add(this); while(q.element()!=null){ Component first = q.remove(); System.out.println(first.name); for(Component c : first.caseComponents){ q.add(c); } } } and Composite. These both inherit from Leaf, but are of course different. Component is a container objects as well as an object itself, while Composite is just one object. I want to change my code so that the line Leaf is only executed if for(Component c : first.caseComponents) is an instance of first, otherwise it would try to iterate over a Composite which is not possible. How can I make it so that the for statement is only entered if Leaf is a first?

3 个答案:

答案 0 :(得分:3)

Use > m_t AB-2000 AB-2600 AB-3500 AC-0100 AD-0100 AF-0200 AB-2000 6.50 NA NA 3.65 NA NA AB-2600 NA 7.18 NA NA NA NA AB-3500 NA NA 5.4 NA NA NA AC-0100 3.65 NA NA 4.22 NA NA AD-0100 NA NA NA NA 5.9 NA AF-0200 NA NA NA NA NA 4.28 to check if instanceof is an instance of comp.

eg:

Composite

答案 1 :(得分:0)

You can restructure your code to apply the visitor pattern:

if(my_pet == NULL)
{
    // Pet doesn't exist...
}

The visitor pattern is a compile-time safe alternative to runtime checks and casts and can also help static analysis of your code. The program code will look like

public interface ComponentVisitor {
  void visitComposite(Composite composite);
  void visitLeaf(Leaf leaf);
}

public interface Component {
  void acceptVisitor(ComponentVisitor visitor);
}

public class Composite implements Component {
  @Override public void acceptVisitor(ComponentVisitor visitor) {
    visitor.visitComposite(this);
  }
}

public class Leaf implements Component {
  @Override public void acceptVisitor(ComponentVisitor visitor) {
    visitor.visitLeaf(this);
  }
}

Incidentally, the demo program does a depth-first print, but you can of course use a queue to accumulate components and then print breadth-first

答案 2 :(得分:0)

I would get rid of both Composite and Leaf altogether, and introduce a Node class that can have children. If it does, it's an intermediate node; if not, it's a leaf. As this property can change any time, it is violently inappropriate to use different classes to express it.