Detailed Flyweight (java language source code is attached)

Flyweight (the Flyweight Pattern) :

Use sharing to support large numbers of fine-grained objects. (Use sharing to support large numbers offine-grained objects efficiently.)

Flyweight core :

  1. Flyweight pattern to share ways to support efficiently reuse a lot of fine-grained objects.
  2. Flyweight objects can do shared key is to distinguish between the internal state and external state.
    • Internal status: You can share, will not change with changes in the environment
    • External status: No share will change with environmental change

Flyweight pattern of the advantages and disadvantages :
Advantages:

  1. Greatly reducing the number of objects in memory,
  2. The same or similar objects only keep a memory, a great saving resources and improve system performance
  3. External state independent, does not affect the state of the internal

Shortcoming

  1. More complex pattern, so that complicated program logic
  2. To conserve memory, the shared internal state, the external isolated state, and a state that an external read operation time becomes long. With time in exchange for the space.

Flyweight application development scenarios :

  1. Flyweight due to their shared characteristics, can be operated in any of the "cell", such as: thread pool, pool database connections.
  2. String class design is Flyweight

Flyweight pattern to achieve :

  1. FlyweightFactory Flyweight factory class: create and manage Flyweight objects, generally designed to pool Flyweight key-value pairs
  2. Flyweight FlyWeight abstract classes: usually an interface or abstract class declaration public methods that provide object's internal state to the outside world, provided external state.
  3. Flyweight ConcreteFlyWeight specific classes: providing a member store internal state variables
  4. Flyweight UnsharedConcreteFlyWeight unshared categories: can not be shared or non-shared subclass can be designed based Flyweight

Case : design software such as Go, Go each piece is an object, has the following properties: the color of
the shape, size, (which can be shared) is called: the internal state. Go and position (which may not share) called: external state. The process can be divided into the following five steps:

Step 1: Define abstract class Flyweight

/**
 * 享元类
 */
public interface ChessFlyWeight {
	void setColor(String c);
	String getColor();
	void display(Coordinate c);
}

Step 2: Definitions of specific classes Flyweight

/**
 * 具体享元类
 */
class ConcreteChess implements ChessFlyWeight {

	private String color;
	
	public ConcreteChess(String color) {
		super();
		this.color = color;
	}

	@Override
	public void display(Coordinate c) {
		System.out.println("棋子颜色:"+color);
		System.out.println("棋子位置:"+c.getX()+"----"+c.getY());
	}

	@Override
	public String getColor() {
		return color;
	}

	@Override
	public void setColor(String c) {
		this.color = c;
	}
}

Step 3: definition of the non-shared class Flyweight

/**
 * 外部状态 UnSharedConcreteFlyWeight
 */
public class Coordinate {
	private int x,y;

	public Coordinate(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
}

Step 4: definition of a factory class Flyweight

/**
 * 享元工厂类
 */
public class ChessFlyWeightFactory {
	//享元池
	private static Map<String,ChessFlyWeight> map = new HashMap<String, ChessFlyWeight>();
	
	public static ChessFlyWeight getChess(String color){
		if(map.get(color)!=null){
			return map.get(color);
		}else{
			ChessFlyWeight cfw = new ConcreteChess(color);
			map.put(color, cfw);
			return cfw;
		}
	}
}

Step 5: Test

public class Client {
	public static void main(String[] args) {
		ChessFlyWeight chess1 = ChessFlyWeightFactory.getChess("黑色");
		ChessFlyWeight chess2 = ChessFlyWeightFactory.getChess("黑色");
		System.out.println(chess1);
		System.out.println(chess2);
		
		System.out.println("增加外部状态的处理===========");
		chess1.display(new Coordinate(10, 10));
		chess2.display(new Coordinate(20, 20));
	}
}

The test results shown below:
Here Insert Picture Description

If you want to know more design patterns, you can visit: Introduction Overview of design patterns and 23 kinds of design patterns

Guess you like

Origin blog.csdn.net/cui_yonghua/article/details/91886352