package com.racoon.util.Design;
import java.util.HashMap;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class FlyweightSample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Random r = new Random();
Client client = new Client();
for(int i = 0;i<10;i++){
FlyObject s = client.get(r.nextInt(10));
s.Work();
}
Log.d("Design", String.valueOf(client.getSize()));
}
class Client{
HashMap<Integer,FlyObject> clients = new HashMap<Integer,FlyObject>();
public FlyObject get(int pid){
FlyObject soc = clients.get(pid);
if(soc == null){
soc = new FlyObject(pid);
clients.put(pid, soc);
}
return soc;
}
public int getSize(){
return clients.size();
}
}
class FlyObject{
int pid;
FlyObject(int pid){
Log.d("Design", "객체을 생성하였습니다.");
this.pid = pid;
}
public void Work(){
Log.d("Design",String.valueOf(pid)+"가 일을 합니다.");
}
}
}
'Program > 디자인패턴' 카테고리의 다른 글
observer 패턴 (0) | 2012.10.05 |
---|---|
FacadeSample (0) | 2012.09.05 |
ProxySample (0) | 2012.09.05 |
디자인패턴 스터디 1주차 - 추상팩토리,빌더,팩토리메소드 (0) | 2012.08.22 |