Share to:

Flyweight pattern

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.

Ví dụ

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.
       }
   }
}

Tham khảo

Liên kết ngoài

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia

Kembali kehalaman sebelumnya