package org.ustc.flyweight;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name) {
this.name = name;
}
}
package org.ustc.flyweight;
public interface Website {
void Use(User user);
}
package org.ustc.flyweight;
import java.util.HashMap;
import java.util.Map;
public class WebsiteFactory {
private Map<String,Website> websites;
public WebsiteFactory() {
// TODO Auto-generated constructor stub
websites = new HashMap<String, Website>();
}
public Website getWebsite(String type){
if(!websites.containsKey(type))
websites.put(type, new ConcreteWebsite(type));
return websites.get(type);
}
}
package org.ustc.flyweight;
public class ConcreteWebsite implements Website{
private String type;
public ConcreteWebsite(String type) {
super();
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public void Use(User user) {
// TODO Auto-generated method stub
System.out.println("Type:"+type+"User:"+user.getName());
}
}
package org.ustc.flyweight;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebsiteFactory fact = new WebsiteFactory();
User user1 = new User("令狐冲");
Website website1 = fact.getWebsite("产品展示");
website1.Use(user1);
User user2 = new User("岳不群");
Website website2 = fact.getWebsite("产品展示");
website2.Use(user2);
User user3 = new User("任我行");
Website website3 = fact.getWebsite("博客");
website3.Use(user3);
}
}
package org.ustc.flyweight;public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String na