import java.util.HashMap;
abstract class Buhin{
int value;
public void setValue(int value){
this.
value=(int)Math.
round(value
*1.05); }
abstract void test();
}
class Buhin1 extends Buhin{
public void test(){
System.
out.
println("this is buhin1"); System.
out.
println("This value is "+value
); }
}
class Buhin2 extends Buhin{
public void test(){
System.
out.
println("this is buhin2"); System.
out.
println("This value is "+value
); }
}
class BuhinFactory{
//部品をCache
private HashMap
<String, Buhin
> pool
= new HashMap
<String, Buhin
>(); public Buhin getBuhin
(String s
){ Buhin b = (Buhin) pool.get(s);
if(b==null){
try{
b=(Buhin)Class.forName(s).newInstance();
pool.put(s,b);
}
}
return b;
}
}
class Main{
public static void main
(String[] args
){ BuhinFactory bf =new BuhinFactory();
Buhin buhin=bf.getBuhin("Buhin2");
buhin.setValue(100);
buhin.test();
buhin =bf.getBuhin("Buhin1");
buhin.setValue(200);
buhin=bf.getBuhin("Buhin2");
buhin.test();
}
}
aW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwphYnN0cmFjdCBjbGFzcyBCdWhpbnsKCWludCB2YWx1ZTsKCXB1YmxpYyB2b2lkCXNldFZhbHVlKGludCB2YWx1ZSl7CgkJdGhpcy52YWx1ZT0oaW50KU1hdGgucm91bmQodmFsdWUqMS4wNSk7Cgl9CglhYnN0cmFjdCB2b2lkIHRlc3QoKTsKfQoKY2xhc3MgQnVoaW4xIGV4dGVuZHMgQnVoaW57CglwdWJsaWMgIHZvaWQgdGVzdCgpewoJCVN5c3RlbS5vdXQucHJpbnRsbigidGhpcyBpcyBidWhpbjEiKTsJCgkJU3lzdGVtLm91dC5wcmludGxuKCJUaGlzIHZhbHVlIGlzICIrdmFsdWUpOwkKCX0KfQoKY2xhc3MgQnVoaW4yIGV4dGVuZHMgQnVoaW57CglwdWJsaWMgdm9pZCB0ZXN0KCl7CgkJU3lzdGVtLm91dC5wcmludGxuKCJ0aGlzIGlzIGJ1aGluMiIpOwkKCQlTeXN0ZW0ub3V0LnByaW50bG4oIlRoaXMgdmFsdWUgaXMgIit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