类似单例模式
public abstract class Web {
abstract void use(String type);
}
public class ConcreteWeb extends Web {
String type;
public ConcreteWeb(String type) {
this.type = type;
}
@Override
void use(String type) {
System.out.println("这个网站的类型是:" + type);
}
}
public class WebFactory {
Map<String,ConcreteWeb> map=new HashMap<>();
public ConcreteWeb getWeb(String web){
if (!map.containsKey(web)){
map.put(web,new ConcreteWeb(web));
return map.get(web);
}else {
return map.get(web);
}
}
public int getSize(){
return map.size();
}
}
测试
public class Test {
public static void main(String[] args) {
WebFactory webFactory = new WebFactory();
webFactory.getWeb("yule");
webFactory.getWeb("wenyi");
System.out.println(webFactory.getSize());
webFactory.getWeb("yule");
System.out.println(webFactory.getSize());
}
}
网友评论