Transformation JAVA (xi) design pattern design pattern Flyweight

Foreword

      This chapter describes the design mode of knowledge Flyweight

method

1. Concept

Do you still remember Singleton design pattern learned before!

Singleton: to ensure that only one instance of a class, and provides access to the instance of a global access point

Singleton pattern is created to ensure the condition of the class and the complexity, to avoid creating duplicate design pattern employed, the class (attribute) is fixed!

But there are some relatively constant property and some of the properties is always changing between some of the objects of daily life. Go pieces such as:

What about the piece objects, its properties relatively fixed color, only black and white, but we found the location of each piece of it is not the same!

When we do not Flyweight pattern to create objects, the amount of light to create a black pawn is huge, and extremely memory consumption.

To solve this problem, we introduced the Flyweight design pattern.

2. yuan to enjoy the realization of ideas of design patterns 

1) create the appropriate project

2) Create a Flyweight Flyweight class interface and implementation class

package flyWeight;

/**
 * 棋子享元接口
 * @author jwang
 *
 */
public interface IChessFlyWeight {

	public String getColor();
	
	public void display(ChessPosition chessPosition);
}

/**
 * 棋子享元类
 * @author jwang
 *
 */
class ChessFlyWeight implements IChessFlyWeight{
	
	private String color;//享元颜色(黑色、白色)

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

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

	@Override
	public void display(ChessPosition chessPosition) {
		System.out.println("棋子的颜色:"+getColor());
		System.out.println("棋子的位置:"+chessPosition.getX()+"---"+chessPosition.getY());
		
	}
	
}

3) create a non-shared attribute class

package flyWeight;

/**
 * 棋子非共享属性
 * @author jwang
 *
 */
public class ChessPosition {
	private int x;
	private int y;
	public ChessPosition(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;
	}
	
	
}

4) Create a class factory Flyweight

package flyWeight;

import java.util.HashMap;
import java.util.Map;


/**
 * 享元工厂类
 * @author jwang
 *
 */
public class ChessFlyWeightFactory {

	//key为棋子的颜色
	private static Map<String,IChessFlyWeight> map = new HashMap<>();
	
	public static IChessFlyWeight getChess(String color){
		if(map.get(color)!=null){
			return map.get(color);
		}else {
			IChessFlyWeight chessFlyWeight = new ChessFlyWeight(color);
			map.put(color, chessFlyWeight);
			return chessFlyWeight;
		}
	}
}

5) writing test code to test

package flyWeight;

public class Test {

	public static void main(String[] args) {
		IChessFlyWeight chess1 =ChessFlyWeightFactory.getChess("黑色");
		IChessFlyWeight chess2 =ChessFlyWeightFactory.getChess("黑色");
		System.out.println("是否为相同对象:"+(chess1==chess2));
		
		//加入非共享属性
		chess1.display(new ChessPosition(1, 2));
		chess2.display(new ChessPosition(3, 4));
	}
}

Program execution results are as follows:

in conclusion:

 In the case of one hundred black pieces simultaneously created using Flyweight only to create an object in memory, which saves costs memory.

However, due to the need to implement the relevant code when adding non-shared attributes, so relatively speaking, the consumption of time to be longer, so-called "time for space."

Guess you like

Origin blog.csdn.net/qq_21046965/article/details/92374202