2016-08-29 6 views
-2

タイトルは特定のものではないかもしれませんが、私はそれをどのように呼び出すのか分かりません。私は、これらのクラスを持って詳細にクラスのArrayList、別のクラスで作成された特定の値を取得

をあなたに説明します:

public class ChannelComponent { 

    private String name; 
    private String mode; //(1P1C/XPXC/1PXC) 
    private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>(); 
    private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>(); 

    public ChannelComponent(String name, String mode) { 
     this.name = name; 
     this.mode = mode; 
    } 

    public boolean canISubscribe(SinkRequiredPort newPort) { 
     if ((mode.equals("1P1C") || mode.equals("1PXC")) && subscribers.size() < 1) { 
      subscribers.add(newPort); 
      return true; 
     } else if (mode.equals("XPXC")) { 
      subscribers.add(newPort); 
      return true; 
     } 
     return false; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getMode() { 
     return mode; 
    } 

    public void printChannel() { 
     System.out.println("[" + name + "," + mode + "]" + "\n"); 
    } 

} 

TestCentralRegistry

public class TestCentralRegistry { 

    private List<ChannelComponent> channels = new ArrayList<ChannelComponent>(); 

    public void addChannelComponent(ChannelComponent c) { 
     channels.add(c); 
    } 


    public static void main(String... args) { 
     TestCentralRegistry demo = new TestCentralRegistry(); 

     demo.addChannelComponent(new ChannelComponent("channel1", "1P1C")); 
     demo.addChannelComponent(new ChannelComponent("channel2", "XPXC")); 

    } 
} 

を私は2 channelComponentsを作成しTestCentralRegistryクラスでは、私は希望のこれらのチャネルを比較しますそれらのモード方法の値canISubscribeChannelComponentクラスにあります)。しかし、どうすればTestCentralRegistryで作成した値をChannelComponentクラスに読み込むことができますか?

私は何が欠けていますか?

私はChannelComponentの参照を持っているつもりです別のクラスからTestChannel、ので、メソッドを呼び出すcanISubscribe

public class TestChannel { 
    ChannelComponent channelComponent; 

    public void callSubscribe(SinkRequiredPort newPort){ 
     channelComponent.canISubscribe(newPort); 
    } 

    public static void main(String... args) { 
     TestChannel testChannel = new TestChannel(); 
     SinkRequiredPort sinkPort = new SinkRequiredPort(); 
     sinkPort.setWantsUse("channel1"); 

     testChannel.callSubscribe(sinkPort); 

    } 
} 

そして私はTestCentralRegistryで作成し、値を比較する必要がありますTestChannelに一致するかどうかを確認します。私はまだ値を取得するようないくつかの行を追加する必要があることを知っていますnewPort.getWantsUse();とchannelComponent と比較し...それでも、私は私は私の質問を願っていますTestCentralRegistry

で作成された値を必要とするには、明確な 任意の提案ですか? ありがとうございました

+1

ヒント:A)(mode.equals( "A")OR "B"または "C")の場合はビルドを開始しないでください - これはdesasterの道ですB)** canISubscribe ** should **というメソッド基本オブジェクトの**状態**を決して操作することはありません。メソッドの名前は、それが何をするかを示します。 "canISubscribe"は、メソッド内で何が起こるかを教えてくれません。 – GhostCat

+0

@GhostCat小さな例を書いて、どうすれば改善できるか教えてください。 – andreahg

+1

私はすでに言っています。まず、何が起きているのかを示すメソッド名を使用します。したがって、あなたのケースでは、 "canISubscribe"と "addSubscription"を持つことができます。 – GhostCat

答えて

2

ChannelComponentのTestCentralRegistryへの参照を保持してください。

public class ChannelComponent { 

    private String name; 
    private String mode; //(1P1C/XPXC/1PXC) 
    private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>(); 
    private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>(); 
    private TestCentralRegistry testCentralRegistry; 

    public ChannelComponent(String name, String mode) { 
     this.name = name; 
     this.mode = mode; 
    } 

    public void registerTestCentralRegistry(TestCentralRegistry testCentralRegistry) { 
     this.testCentralRegistry = testCentralRegistry; 
    } 
} 

下図のように、あなたのTestCentralRegistryを登録します。

public class TestCentralRegistry { 

    private List<ChannelComponent> channels = new ArrayList<ChannelComponent>(); 

    public void addChannelComponent(ChannelComponent c) { 
     channels.add(c); 
    } 

    public static void main(String... args) { 
     TestCentralRegistry demo = new TestCentralRegistry(); 

     ChannelComponent cc1 = new ChannelComponent("channel1", "1P1C"); 
     cc1.registerTestCentralRegistry(demo); 
     ChannelComponent cc2 = new ChannelComponent("channel2", "XPXC"); 
     cc2.registerTestCentralRegistry(demo); 

     demo.addChannelComponent(cc1); 
     demo.addChannelComponent(cc2); 
    } 
} 

を次に、あなたはChannelComponentからtestCentralRegistry.getX()を呼び出すことによってTestCentralRegistryで作成された値を取得することができます。

+0

ありがとう@uoyilmaz、はいそれを行う意味があります! – andreahg

関連する問題