Skip to content

Commit 2217fbc

Browse files
Azureyjtiluwatar
authored andcommitted
Issue#550: double buffer pattern (iluwatar#1024)
* Basic implementation * implement double buffer * add unit test * add unit test * Add Readme * Change local value declaration to var * Remove unused fields
1 parent 82f9a6c commit 2217fbc

File tree

11 files changed

+623
-0
lines changed

11 files changed

+623
-0
lines changed

double-buffer/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
---
3+
layout: pattern
4+
title: Double Buffer
5+
folder: double-buffer
6+
permalink: /patterns/double-buffer/
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Intent
14+
Double buffering is a term used to describe a device that has two buffers. The usage of multiple buffers increases the overall throughput of a device and helps prevents bottlenecks. This example shows using double buffer pattern on graphics. It is used to show one image or frame while a separate frame is being buffered to be shown next. This method makes animations and games look more realistic than the same done in a single buffer mode.
15+
16+
## Applicability
17+
This pattern is one of those ones where you’ll know when you need it. If you have a system that lacks double buffering, it will probably look visibly wrong (tearing, etc.) or will behave incorrectly. But saying, “you’ll know when you need it” doesn’t give you much to go on. More specifically, this pattern is appropriate when all of these are true:
18+
19+
- We have some state that is being modified incrementally.
20+
21+
- That same state may be accessed in the middle of modification.
22+
23+
- We want to prevent the code that’s accessing the state from seeing the work in progress.
24+
25+
- We want to be able to read the state and we don’t want to have to wait while it’s being written.
26+
27+
## Credits
28+
29+
* [Game Programming Patterns - Double Buffer]([http://gameprogrammingpatterns.com/double-buffer.html](http://gameprogrammingpatterns.com/double-buffer.html))

double-buffer/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.22.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>double-buffer</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.commons</groupId>
45+
<artifactId>commons-lang3</artifactId>
46+
</dependency>
47+
</dependencies>
48+
49+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.doublebuffer;
25+
26+
import org.apache.commons.lang3.tuple.MutablePair;
27+
import org.apache.commons.lang3.tuple.Pair;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
/**
35+
* Double buffering is a term used to describe a device that has two buffers.
36+
* The usage of multiple buffers increases the overall throughput of a device
37+
* and helps prevents bottlenecks. This example shows using double buffer pattern
38+
* on graphics. It is used to show one image or frame while a separate frame
39+
* is being buffered to be shown next. This method makes animations and games
40+
* look more realistic than the same done in a single buffer mode.
41+
*/
42+
public class App {
43+
44+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
45+
46+
/**
47+
* Program main entry point.
48+
* @param args runtime arguments
49+
*/
50+
public static void main(String[] args) {
51+
var scene = new Scene();
52+
List<Pair<Integer, Integer>> drawPixels = new ArrayList<>();
53+
Pair<Integer, Integer> pixel1 = new MutablePair<>(1, 1);
54+
Pair<Integer, Integer> pixel2 = new MutablePair<>(5, 6);
55+
Pair<Integer, Integer> pixel3 = new MutablePair<>(3, 2);
56+
drawPixels.add(pixel1);
57+
drawPixels.add(pixel2);
58+
drawPixels.add(pixel3);
59+
scene.draw(drawPixels);
60+
var buffer1 = scene.getBuffer();
61+
printBlackPixelCoordinate(buffer1);
62+
63+
drawPixels.clear();
64+
Pair<Integer, Integer> pixel4 = new MutablePair<>(3, 7);
65+
Pair<Integer, Integer> pixel5 = new MutablePair<>(6, 1);
66+
drawPixels.add(pixel4);
67+
drawPixels.add(pixel5);
68+
scene.draw(drawPixels);
69+
Buffer buffer2 = scene.getBuffer();
70+
printBlackPixelCoordinate(buffer2);
71+
}
72+
73+
private static void printBlackPixelCoordinate(Buffer buffer) {
74+
var log = "Black Pixels: ";
75+
Pixel[] pixels = buffer.getPixels();
76+
for (var i = 0; i < pixels.length; ++i) {
77+
if (pixels[i] == Pixel.BLACK) {
78+
var y = i / FrameBuffer.WIDTH;
79+
var x = i % FrameBuffer.WIDTH;
80+
log += " (" + x + ", " + y + ")";
81+
}
82+
}
83+
LOGGER.info(log);
84+
}
85+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.doublebuffer;
25+
26+
/**
27+
* Buffer interface.
28+
*/
29+
public interface Buffer {
30+
31+
/**
32+
* Clear the pixel in (x, y).
33+
* @param x X coordinate
34+
* @param y Y coordinate
35+
*/
36+
void clear(int x, int y);
37+
38+
/**
39+
* Draw the pixel in (x, y).
40+
* @param x X coordinate
41+
* @param y Y coordinate
42+
*/
43+
void draw(int x, int y);
44+
45+
/**
46+
* Clear all the pixels.
47+
*/
48+
void clearAll();
49+
50+
/**
51+
* Get all the pixels.
52+
* @return pixel list
53+
*/
54+
Pixel[] getPixels();
55+
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.doublebuffer;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* FrameBuffer implementation class.
31+
*/
32+
public class FrameBuffer implements Buffer {
33+
34+
public static final int WIDTH = 10;
35+
public static final int HEIGHT = 8;
36+
37+
private Pixel[] pixels = new Pixel[WIDTH * HEIGHT];
38+
39+
public FrameBuffer() {
40+
clearAll();
41+
}
42+
43+
@Override
44+
public void clear(int x, int y) {
45+
pixels[getIndex(x, y)] = Pixel.WHITE;
46+
}
47+
48+
@Override
49+
public void draw(int x, int y) {
50+
pixels[getIndex(x, y)] = Pixel.BLACK;
51+
}
52+
53+
@Override
54+
public void clearAll() {
55+
for (var i = 0; i < pixels.length; ++i) {
56+
pixels[i] = Pixel.WHITE;
57+
}
58+
}
59+
60+
@Override
61+
public Pixel[] getPixels() {
62+
return pixels;
63+
}
64+
65+
private int getIndex(int x, int y) {
66+
return x + WIDTH * y;
67+
}
68+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.doublebuffer;
25+
26+
/**
27+
* Pixel enum. Each pixel can be white (not drawn) or black (drawn).
28+
*/
29+
public enum Pixel {
30+
31+
WHITE(0),
32+
BLACK(1);
33+
34+
private int color;
35+
36+
Pixel(int color) {
37+
this.color = color;
38+
}
39+
}

0 commit comments

Comments
 (0)