@TOC

1 show the project requirements of the website

For A small outsourcing project, I will build A product display website for client A. Client A’s friends feel the effect is good and also hope to do such A product display website, but the requirements are different:

1) Some clients require to publish in the form of news

2) Some clients request to publish in the form of blog

3) Some customers want to release it in the form of wechat public account

2 traditional solution website presentation project

1) Directly copy and paste a copy, and then customize and modify according to different requirements of customers

2) Rent space for each site

3) Schematic diagram of scheme design

3. Traditional solution website presentation project – problem analysis

1) The site structure is very similar, and the site is not high traffic, if divided into multiple virtual space to deal with, equivalent to a lot of instances of the same site, resulting in a waste of resources on the server

2) Solution: integrate into a website, share its related code and data, for hard disk, memory, CPU, database space and other server resources can be shared, reduce server resources

3) For code, it is easier to maintain and extend because it is an instance

4) The above solution ideas can be used to solve the yuan model

4 Basic introduction to enjoy Yuan mode

(1) Flyweight Pattern is also called fly quantity Pattern: sharing technology is used to effectively support a large number of fine-grained objects. (2) It is often used in the low-level development of the system to solve the performance problems of the system. Like the database connection pool, there are all created connection objects, in these connection objects we need to directly use, avoid creating, if there is no we need to create a. (3) Share mode can solve the problem of memory waste of repeated objects, when there are a large number of similar objects in the system, the need for buffer pool. Instead of always creating new objects, you can take them from the buffer pool. This reduces system memory and improves efficiency. (4) The classic application scenario of share element mode is pool technology. String constant pool, database connection pool, buffer pool and so on are all applications of share element mode. Share element mode is an important implementation of pool technology.Copy the code

5 Principle class diagram of enjoy mode

(1) FlyWeight is an abstract user role. It is an abstract class of a product that defines the interface or implementation of an object's external state and its internal state (described below). (2) ConcreteFlyWeight is concrete the flyweight role, is a specific product class, realize the abstract role definitions related business. (3) UnSharedConcreteFlyWeight role is not sharing, general won't appear in the flyweight factory. FlyWeightFactory enjoys a meta-factory class that builds a pool container (collection) and provides methods to get objects from the pool.Copy the code

6 Internal status and external status

For example, go, backgammon, checkers, they have a large number of chess objects, go and backgammon only black and white, checkers more color, so the color of the pieces is the internal state of the pieces; And the difference between each piece is the position of the different, when we fall, fall color is fixed, but the position is changed, so the chess coordinates is the external state of the pieces

(1) Share mode puts forward two requirements: fine granularity and shared object. This is where internal state and external state are involved, that is, the object information is divided into two parts: internal state and external state

(2) Internal state refers to the information shared by the object, which is stored inside the shared object and will not change with the change of the environment

(3) The external state refers to a mark on which the object can depend. It is an unshareable state that changes with the environment.

Here’s an example: Go a piece of the 361 slots can put in theory, every game is likely to be a piece of the by the object, because the memory space is limited, a server is very difficult to support more players to play the game, if use the flyweight pattern to handle pieces, then pieces object can be reduced to only have two instances, this is very good solve the problem of the object’s overhead

7 Enjoy yuan mode to solve the website display project

7.1 Train of Thought Analysis and Diagrams (Class Diagram)

7.2 Code Presentation

WebSite class

public abstract class WebSite { public abstract void use(User user); // Abstract method}Copy the code

ConcreteWebSite class

Public class ConcreteWebSite extends WebSite {public class ConcreteWebSite extends WebSite {public class ConcreteWebSite extends WebSite {private String type; public ConcreteWebSite(String type) { this.type = type; } @override public void use(User User) {system.out.println (" User is "+ user.getName()); }}Copy the code

The User class

Public class User {// External state private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

WebSiteFactory class

Public class WebSiteFactory {// collection, Private HashMap<String, ConcreteWebSite> pool = new HashMap<>(); Public WebSite getWebSiteCategory(String type) {if(! Pool.concretekey (type)) {// Create a concreteKey (type, new ConcreteWebSite(type)); } return (WebSite)pool.get(type); } public int getWebSiteCount() {return pool.size(); }}Copy the code

The test class

Public class Client {public static void main(String[] args) {// TODO auto-generated method stub // Creates a factory class WebSiteFactory factory = new WebSiteFactory(); / / customer to a WebSite in the form of news release WebSite webSite1 = factory. GetWebSiteCategory (" news "); webSite1.use(new User("tom")); / / customer want a WebSite released in the form of blog site webSite2 = factory. GetWebSiteCategory (" blog "); webSite2.use(new User("jack")); / / customer want a WebSite released in the form of blog site webSite3 = factory. GetWebSiteCategory (" blog "); webSite3.use(new User("smith")); / / customer want a WebSite released in the form of blog site webSite4 = factory. GetWebSiteCategory (" blog "); webSite4.use(new User("king")); Println (" websitecount =" + factory.getWebsitecount ()); }} Output: The website is published in the form of: News in use.. The user is Tom. The website is published in the form of: blog in use.. The user is Jack. The website is published in the form of: blog in use.. The user is Smith. The website is published in the form of: blog in use.. The categories of users are King sites =2Copy the code

Notes and details of enjoy Mode

1) In the sharing mode, “enjoy” means share, and “yuan” means object.

2) When there are a large number of objects in the system, which consume a large amount of memory, and most of the state of the objects can be externalized, we can consider using the share mode.

3) Judge by unique identifier. If there is one in memory, return the object identified by this unique identifier and store it with HashMap/HashTable.

4) Enjoy the element mode greatly reduces the creation of objects, reduce the program memory occupation, improve efficiency.

5) Sharing mode improves the complexity of the system. We need to separate the internal state from the external state, and the external state has a curing property, should not change with the internal state change, this is the use of free element mode need to pay attention to.

6) When using the free mode, pay attention to the separation of internal state and external state, and need to have a factory class to control.

7) The classic application scenarios of the meta-share mode are those that require buffer pools, such as String constant pools and database connection pools.

9 enjoy yuan mode in JDK-Interger application source code analysis

Example:

public static void main(String[] args) {
	Integer x = Integer.valueOf(127);  
	Integer y = new Integer(127);
	Integer z = Integer.valueOf(127);  
	Integer w = new Integer(127);  
	
	System.out.println(x.equals(y)); //true
	System.out.println(x == y ); //false
	System.out.println(x == z ); //true
	System.out.println(w == x ); //false
	System.out.println(w == y ); //false
}
Copy the code

The source code

public final class Integer extends Number implements Comparable<Integer> { public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } public Integer(int value) { this.value = value; }}Copy the code