Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Flyweight

Pattern Properties

Type: Structural

Level: Component

Purpose

To reduce the number of very low-level, detailed objects within a system by sharing objects.

Introduction

Object-oriented programming causes many objects to exist during execution, especially if there are several low-level objects. This places a big load on the Java Virtual Machine’s (JVMs) memory.

Many objects in the Personal Information Manager can be edited, so they use the State pattern (see “ State ” on page 104) to determine whether to save the items’ content. Each of these items can have its own collection of State objects.

One way to alleviate the problem of having many objects is to share objects. Many of these low-level objects only differ slightly, while most of their state and behavior is identical. Sharing instances reduces the number dramatically, without losing any functionality.

For a set of objects, the Flyweight pattern separates those parts of the objects that are the same from the parts that are different. The data that distinguishes the different instances (also called the externalized data) is provided to the single generic instance when needed.

Applicability

Use Flyweight when all of the following are true:

The application uses many identical, or nearly identical, objects.

For each nearly identical object, the non-identical parts can be separated from the identical part allowing that identical part to be shared.

Groups of nearly identical objects can be replaced by one shared object once the non-identical parts of the state have been removed.

If the application needs to distinguish among the nearly identical objects in their original state.

Description

The Flyweight pattern is intended to reduce the number of objects within an application, and does so by sharing objects. The objects contain some internal data, but all the data concerning the context within which they operate is supplied by an external source. Each shared object should be as generic as possible and independent of context.

By sharing objects, Flyweight significantly reduces the number of objects. The shared object is used by several clients and is indistinguishable from an object that is not shared.

An example of a Flyweight is a layout manager. When building a GUI you use several components and containers. To determine the layout, you use layout managers. In general, each layout manager is nearly identical; they differ only in the specific components they manage and some set attributes. If you would remove these components and attributes, each instance of that specific layout manager type is identical. When the layout manager functionality is required, the components and attributes are passed to the single shared instance. Having a shared object for each layout manager type and feeding it the specific context reduces the number of objects.

The clients using the shared object are responsible for providing and/or calculating the context information. That information is passed into the shared object when needed.

The Flyweight is shared, so a client should not create a Flyweight directly, but always obtain one through a factory (see “ Abstract Factory ” on page 6). Such a factory ensures the proper sharing of the Flyweights.

125