Flyweight
One of the structural patterns aims to reduce memory usage by sharing as much data as possible with similar objects.
It is particularly useful when dealing with a large number of similar objects, where creating a new instance for each object would be expensive in terms of memory consumption.
key concepts:
Intrinsic state: The state that is shared between multiple objects is independent of the context and remains the same across different objects.
Extrinsic state: The state that is unique to each object and is passed from the client. This state can vary and is not stored in the Flyweight object
Key participants:
Flyweight: Interface that the Flyweight object to receive the Extrinsic state and use it.
ConcreteFlyweight: Implements the Flyweight and stores the intrinsic state.
FlyweightFactory: Manages the Flyweight objects and ensures the sharing of interfaces, it returns an existing Flyweight if it already exists.
Client(like Main class): Maintains a reference to Flyweight and supplies Extrinsic state when it needs to interact with the Flyweight object.
Let's take an example of a Flyweight object of character
Suppose we have a text editor that needs to render a large amount of text. Each character can be represented as an object, but having a separate object for every character would waste a lot of memory. Instead, we can use Flyweights to share the character objects that represent each letter, and store the extrinsic state like the position or formatting outside
Flyweight
public interface Flyweight { public void display(int x, int y);//x, y are the extrinsic state of the Flyweight object }
ConcreteFlyweight
public class CharacterFlyweight implements Flyweight { private char ch; public CharacterFlyweight(char c){ this.ch = c; } @Override public void display(int x ,int y){ System.out.println("[drawing character: "+this.ch+" at co-ordinates:("+x+","+y+")]"); } }
FlyweightFactory
public class FlyweightFactory { private static HashMap<Character,Flyweight> flyweights = new HashMap<>(); public static Flyweight getFlyweight(char c){ Flyweight flyweight = flyweights.getOrDefault(c,null); if(null==flyweight){ flyweight = new CharacterFlyweight(c); flyweights.put(c,flyweight); } return flyweight; } }
Main
public class Main { public static void main(String args[]){ Flyweight flyweight1 = FlyweightFactory.getFlyweight('a'); Flyweight flyweight2 = FlyweightFactory.getFlyweight('b'); Flyweight flyweight3 = FlyweightFactory.getFlyweight('a');// will use the same object that is referenced by flyweight1 flyweight1.display(1, 2);//'a' displayed at 1,2 flyweight2.display(3, 4);//'b' displayed at 3,4 flyweight3.display(5, 7); // 'a'(shared) displayed at 5,7 } }
Output:
[drawing character: a at co-ordinates:(1,2)] [drawing character: b at co-ordinates:(3,4)] [drawing character: a at co-ordinates:(5,7)]
Key Points
- Memory Efficiency: Reduces memory usage by sharing objects, especially when the intrinsic state is large or there are many objects.
- Performance Improvement: Reducing the number of objects created, it can improve the performance of the application when managing large numbers of objects.
Disadvantages
Complexity: The pattern can add complexity to the code, especially in managing the extrinsic and intrinsic states separately.
Overhead: If there are few objects to share, the Flyweight pattern might introduce unnecessary complexity without significant memory savings.
以上是Flyweight的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...
