public class Main {
public static void main(String[] args) {
WebSiteFactory factory = new WebSiteFactory();
CloudWebSite companySite1 = factory.getWebSiteByCategory("企业官网");
companySite1.run(new Company("小滴课堂"));
CloudWebSite companySite2 = factory.getWebSiteByCategory("企业官网");
companySite2.run(new Company("旭瑶课堂"));
CloudWebSite byteDanceSite = factory.getWebSiteByCategory("信息流");
byteDanceSite.run(new Company("字节跳动"));
CloudWebSite ucNews = factory.getWebSiteByCategory("信息流");
ucNews.run(new Company("优视科技"));
System.out.println("网站分类总数:" +factory.getWebsiteCategorySize());
}
}
public abstract class CloudWebSite {
public abstract void run(Company company);
}
public class Company {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Company(){}
public Company(String name){
this.name = name;
}
}
public class ConcreteWebSite extends CloudWebSite {
private String category;
public ConcreteWebSite(String category){
this.category = category;
}
@Override
public void run(Company company) {
System.out.println("网站分类:"+category+", 公司:"+company.getName());
}
}
public class WebSiteFactory {
/**
* map里面的key是分类
*/
private Map<String,ConcreteWebSite> map = new HashMap<>();
/**
* 根据key获取分类站点
* @param category
* @return
*/
public CloudWebSite getWebSiteByCategory(String category){
if(map.containsKey(category)){
return map.get(category);
}else {
ConcreteWebSite site = new ConcreteWebSite(category);
map.put(category,site);
return site;
}
}
/**
* 获取分类个数
* @return
*/
public int getWebsiteCategorySize(){
return map.size();
}
}