Flyweight pattern
Bách khoa toàn thư mở Wikipedia
Flyweight là một mẫu thiết kế phần mềm. Khi nhiều đối tượng (objects) phải được xử lí mà chương trình không thể chịu nổi một lượng dữ liệu khổng lồ, thì cần dùng flyweight.
Trong mẫu flyweight, dữ liệu không có các con trỏ (pointer) đến các phương thức của kiểu dữ liệu đó, vì như thế sẽ tốn rất nhiều bộ nhớ. Thay vào đó, các chương trình con (subroutine) sẽ được gọi trực tiếp. Trong một vài trường hợp, flyweight inheritance được thực hiện bằng cách "shift-in" và "shift-out" các data markers dưới dạng các chu trình tác vụ ở mức cao (higher-level operation cycles) thông qua một mảng các flyweight data.
Một ví dụ cổ điển của mẫu flyweight là các kí tự được lưu trong một bộ xử lí văn bản (word processor). Mỗi kí tự đại diện cho một đối tượng mà có dữ liệu là loại font (font face), kích thước font, và các dữ liệu định dạng khác. Bạn có thể tưởng tượng là, với một tài liệu (document) lơn với cấu trúc dữ liệu như thế này thì sẽ bộ xử lí văn bản sẽ khó mà có thể xử lí được. Hơn nữa, vì hầu hết dữ liệu dạng này là lặp lại, phải có một cách để giảm việc lưu giữ này - đó chính là mẫu Flyweight. Mỗi đối tượng kí tự sẽ chứa một tham khảo đến một đối tượng định dạng riêng rẽ mà chính đối tượng này sẽ chữa các thuộc tính cần thiết. Điều này sẽ giảm một lượng lớn sự lưu giữ bằng cách kết hợp mọi kí tự có định dạng giống nhau trở thành các đối tượng đơn chỉ chứa tham khảo đến cùng một đối tượng đơn chứa định dạng chung đó.
Cũng có một phiên bản của mẫu này dùng cho cấu trúc XML.
[sửa] Ví dụ
[sửa] Java
Chương trình Java sau minh hoạ ví dụ ở trên. Nó sẽ đưa ra kết quả như sau:
CharFactory created only 4 objects for 5 characters! Printing 'H' in 'Arial' at position 0:0. Printing 'e' in 'Arial' at position 1:0. Printing 'l' in 'Arial' at position 2:0. Printing 'l' in 'Arial' at position 3:0. Printing 'o' in 'Times' at position 4:0.
import java.util.*; class GraphicChar { char c; String fontFace; public GraphicChar(char c, String fontFace) { this.c = c; this.fontFace = fontFace; } public void printAtPosition(int x, int y) { System.out.printf("Printing '%c' in '%s' at position %d:%d.\n", c, fontFace, x, y); } } class GraphicCharFactory { HashMap<String, GraphicChar> pool = new HashMap<String, GraphicChar>(); // the Flyweights public int getNum() { return pool.size(); } public GraphicChar get(Character c, String fontFace) { GraphicChar gc; String key = c.toString() + fontFace; if ((gc = pool.get(key)) != null) { return gc; } else { gc = new GraphicChar(c, fontFace); pool.put(key, gc); return gc; } } } class FlyWeightExample { public static void main(String[] args) { GraphicCharFactory cf = new GraphicCharFactory(); // Compose the text by storing the characters as objects. ArrayList<GraphicChar> text = new ArrayList<GraphicChar>(); text.add( cf.get('H', "Arial") ); // 'H' and "Arial" are called intrinsic information text.add( cf.get('e', "Arial") ); // because it is stored in the object itself. text.add( cf.get('l', "Arial") ); text.add( cf.get('l', "Arial") ); text.add( cf.get('o', "Times") ); // See how the Flyweight approach is beginning to save space: System.out.printf("CharFactory created only %d objects for %d characters.\n", cf.getNum(), text.size()); int x=0, y=0; for (GraphicChar c : text) { // Passing position as extrinsic information to the objects, c.printAtPosition(x++, y); // as a top-left 'A' is not different to a top-right one. } } }
[sửa] Các liên kết ngoài
- XML Flyweight Design Pattern
- Article "Make your apps fly - Implement Flyweight to improve performance" by David Geary
- Article "Enhancing Web Application Performance with Caching" by Neal Ford
- Article "The Flyweight Pattern" by Alberto Bar-Noy
- Sample Chapter "C# Design Patterns: The Flyweight Pattern" by James W. Cooper
- Section "Flyweight Text Entry Fields" from the RIDES Reference Manual by Allen Munro and Quentin A. Pizzini
- Examples Design Patterns
- Description from Portland's Pattern Repository
- Overview
- Sourdough Design
- Use sharing to support large numbers of fine grained objects efficiently
- Structural Patterns - Flyweight Pattern
- Class::Flyweight - implement the flyweight pattern in OO perl
- ASP.NET Code Sample
- Citations from CiteSeer