Java flyweight)

Source: Internet
Author: User

When a large number of objects are used in an application, these objects cause great storage overhead, and most of the States or parameters of the objects are the same (internal state, you can consider using the metadata mode. Using the metadata mode, these object references share the same instance to reduce storage overhead, and different state parameters (external States) between objects) the external parameters are passed in.

package flyweight;public abstract class WebSite {    protected String type;        public WebSite(String type) {        this.type = type;    }        public String getType() {        return type;    }    public abstract void use(User user);}package flyweight;public class ConcurrentWebSite extends WebSite {    public ConcurrentWebSite(String type) {        super(type);    }    @Override    public void use(User user) {        System.out.println("The web site's type is: " + type);        System.out.println("User: " + user.getUserName());        System.out.println("Passwd: " + user.getPassWd());        System.out.println();    }}package flyweight;import java.util.HashMap;import java.util.Map;public class WebSiteFactory {    private static Map<String, WebSite> webSites = new HashMap<String, WebSite>();        private WebSiteFactory() {}        public static WebSite createWebSite(String type) {        WebSite webSite = webSites.get(type);        if (webSite == null) {            webSite = new ConcurrentWebSite(type);            webSites.put(type, webSite);        }        return webSite;    }        public static int webSitesCount() {        return webSites.size();    }}package flyweight;public class User {    private String userName;    private String passWd;        public User(String userName, String passWd) {        this.userName = userName;        this.passWd = passWd;    }        public String getUserName() {        return userName;    }        public String getPassWd() {        return passWd;    }}package flyweight;public class Test {    public static void main(String[] args) {        WebSite wb1 = WebSiteFactory.createWebSite("BusinessWebSite");        User user1 = new User("root", "root");        wb1.use(user1);                WebSite wb2 = WebSiteFactory.createWebSite("BusinessWebSite");        User user2 = new User("admin", "admin");        wb2.use(user2);                WebSite wb3 = WebSiteFactory.createWebSite("BusinessWebSite");        User user3 = new User("slave", "slave");        wb3.use(user3);                WebSite wb4 = WebSiteFactory.createWebSite("PersonalWebSite");        User user4 = new User("person", "person");        wb4.use(user4);                WebSite wb5 = WebSiteFactory.createWebSite("PersonalWebSite");        User user5 = new User("alexis", "alexis");        wb5.use(user5);                WebSite wb6 = WebSiteFactory.createWebSite("PersonalWebSite");        User user6 = new User("shadow", "shadow");        wb6.use(user6);                System.out.println("WebSites Instances Count: " + WebSiteFactory.webSitesCount());    }}

Output

The web site's type is: businesswebsite
User: Root
Passwd: Root

The web site's type is: businesswebsite
User: Admin
Passwd: Admin

The web site's type is: businesswebsite
User: slave
Passwd: slave

The web site's type is: personalwebsite
User: person
Passwd: person

The web site's type is: personalwebsite
User: Alexis
Passwd: Alexis

The web site's type is: personalwebsite
User: Shadow
Passwd: Shadow

Websites instances count: 2

Here, the shared instance is website, type is an internal parameter, and user is passed as an external parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.