Wiring collections , from java in action

本文介绍了一个使用表演者模式实现的单乐队类,该类通过集合存储乐器实例并执行演奏行为。通过Spring框架配置文件定义了乐队及其中包含的不同乐器。演示了列表和集合在乐器实例存储上的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The method implements:

public class SingleBand implements Performer{

	public SingleBand(){}
	
	private Collection<Instrument> instruments;
	
	public void setInstrument(Collection<Instrument> instruments){
		this.instruments = instruments;
	}
	
	public void perform() {
		// TODO Auto-generated method stub
		for(Instrument instrument: instruments){
			instrument.play();
		}
	}

}

 the bean settings:

   

    <bean id="band" class="com.performence.SingleBand">
       <property name="instrument">

         <list>
           <ref bean='piano'/>
           <ref bean='voilin'/>
           <ref bean='piano'/>
           <ref bean='voilin'/>
         </list>
       </property>
    </bean>
   <bean id="piano" class="com.performence.Piano"/>
   <bean id="voilin" class="com.performence.Violin"/>
   
</beans>

 

test method :

       

   public static void main(String args[]){
	   BeanFactory factory = new XmlBeanFactory(new FileSystemResource("bean.xml"));
	   Performer per = (Performer)factory.getBean("band");
	   per.perform();
   }
}

 

output:

   piano playing.......
   violin playing.......
   piano playing.......
   violin playing.......

 

if change the <list> to <set> in the bean.xml

output :

   piano playing.......
   violin playing.......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值