일어나 서 밥 하기 (관찰자 모드)

//   
public class Boy {
	//  
	private String state;
	//     
	public List<Girl> girls = new ArrayList<Girl>();
    
	//  	
	public void notifyGirls(){
		
		for(Girl g : girls ){
			g.update();
		}
		
	}
	//  
	public void attach(Girl g){
		girls.add(g);
	
	}
	//  
	public void detach(Girl g){
		girls.remove(g);
	}
	
	
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	
	public static void main(String[] args){
		Boy ysen = new Boy();
		Girl girlFriend = new Girl();
		girlFriend.setName("hyy");
		//     ,           ,         ,          ,      
		//   Boy   Girl         ,       
		girlFriend.setBoy(ysen);
		ysen.attach(girlFriend);
		
		ysen.setState("     ~~");
		ysen.notifyGirls();
	}
	
}

 
//   
public class Girl {
	
	private String name;
	private Boy boy;
	
	public void update(){
		System.out.println(boy.getState()+name+"     ");
	}

	public Boy getBoy() {
		return boy;
	}
	public void setBoy(Boy boy) {
		this.boy = boy;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	

}

좋은 웹페이지 즐겨찾기