решил повторить паттерны.
Дошёл до flyweight.
По примерам кода понял, что это просто фабричный метод с кешем. Предполагается, что этот фабричный метод возвращает immutable объект.
Вот например пример кода:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
/*
* Интерфейс приспособленца
*/
public interface Primitive {
/*
* Метод отрисовки примитива с передачей заданного контекста рисования
*/
public void draw(Context context);
}
/*
* Окружнсоть - разделяемый приспособленец. Внутреннее состояние - радиус
*/
public class Circle implements Primitive {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public void draw(Context context) { }
}
/*
* Разделяемый приспособленец - Квадрат.
* Внутренее состояние - высота, ширина.
*/
public class Square implements Primitive {
private int height, width;
public Square(int height, int width) {
this.height = height;
this.width = width;
}
@Override
public void draw(Context context) { }
}
/*
* Разделяемый приспособленец - точка
*/
public class Point implements Primitive {
@Override
public void draw(Context context) { }
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/*
* Не разделяемый приспособленец - изображение.
*/
public class Picture implements Primitive {
private List<Primitive> childrens;
public Picture(Primitive ...primitives) {
this.childrens = new LinkedList<Primitive>();
this.childrens.addAll(Arrays.asList(primitives));
}
@Override
public void draw(Context context) {
for (Primitive p: childrens) {
p.draw(context);
}
}
}
import java.awt.Color;
/*
* Контекст рисования, передается клиентом примитиву для отрисовки последнего
*/
public final class Context {
public final int x;
public final int y;
public final Color color;
public Context(int x, int y, Color collor) {
this.x = x;
this.y = y;
this.color = collor;
}
}
import java.util.HashMap;
import java.util.Map;
/*
* Фабрика приспособленцев.
* Реализует разделение оных на основании их внутренних состояний.
*
*/
public abstract class PrimitiveFactory {
private static Point onePoint;
private static Map<Integer, Circle> circles;
private static Map<Integer, Square> squares;
static {
circles = new HashMap<Integer, Circle>();
squares = new HashMap<Integer, Square>();
}
public static synchronized Picture createPicture(Primitive ... childrens) {
return new Picture(childrens);
}
public static synchronized Circle createCircle(int radius) {
if (circles.get(radius) == null) {
circles.put(radius, new Circle(radius));
}
return circles.get(radius);
}
public static synchronized Square createSquare(int height, int width) {
if (squares.get(height*10+width) == null) {
squares.put(height*10+width, new Square(height, width));
}
return squares.get(height*10+width);
}
public static synchronized Point createPoint() {
if (onePoint == null) {
onePoint = new Point();
}
return onePoint;
}
}
import java.awt.Color;
// Использование
public class Main {
public static void main(String[] args) {
Primitive[] primitives = {
PrimitiveFactory.createPoint(),
PrimitiveFactory.createCircle(10),
PrimitiveFactory.createSquare(20, 30),
PrimitiveFactory.createCircle(20),
PrimitiveFactory.createCircle(20),
PrimitiveFactory.createPoint(),
PrimitiveFactory.createSquare(20, 40),
};
Picture picture = PrimitiveFactory.createPicture(primitives);
Context context = new Context(10, 20, Color.BLUE);
picture.draw(context);
}
}
в определении паттерна идёт речь про какие-то внутренние и внешние состояния:
авторFlyweight используется для уменьшения затрат при работе с большим количеством мелких объектов. При проектировании приспособленца необходимо разделить его свойства на внешние и внутренние. Внутренние свойства всегда неизменны, тогда как внешние могут отличаться в зависимости от места и контекста применения и должны быть вынесены за пределы приспособленца.
Или внешнее состоние это то, что передается непосредственно в метод и не меняет внутреннее состояние приспособленца?