Java: Mediator Pattern
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Command.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.commands; /** * * */ public interface Command { String getName(); void execute(); } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc ColorCommand.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.commands; import java.awt.*; import com.javapatterns.mediator.editor.*; import com.javapatterns.mediator.shapes.*; import com.javapatterns.mediator.shapes.*; import com.javapatterns.mediator.commands.*; /** * * */ public class ColorCommand implements Command { private Editor editor; private Color color; public ColorCommand(Editor editor, Color color) { this .editor = editor; this .color = color; } @Override public String getName() { return "Colorize: " + color.toString(); } @Override public void execute() { for (com.javapatterns.mediator.shapes.Shape child : editor.getShapes().getSelected()) { child.setColor(color); } } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc MoveCommand.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.commands; import com.javapatterns.mediator.editor.*; import com.javapatterns.mediator.shapes.*; /** * * */ public class MoveCommand implements Command { private Editor editor; private int startX, startY; private int endX, endY; public MoveCommand(Editor editor) { this .editor = editor; } @Override public String getName() { return "Move by X:" + (endX - startX) + " Y:" + (endY - startY); } public void start( int x, int y) { startX = x; startY = y; for (Shape child : editor.getShapes().getSelected()) { child.drag(); } } public void move( int x, int y) { for (Shape child : editor.getShapes().getSelected()) { child.moveTo(x - startX, y - startY); } } public void stop( int x, int y) { endX = x; endY = y; for (Shape child : editor.getShapes().getSelected()) { child.drop(); } } @Override public void execute() { for (Shape child : editor.getShapes().getSelected()) { child.moveBy(endX - startX, endY - startY); } } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Editor.java * Interface * Record * Annotation * Enum * */ /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Editor.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.editor; import javax.swing.*; import java.io.*; import java.util.Base64; import com.javapatterns.mediator.history.*; import com.javapatterns.mediator.shapes.*; import com.javapatterns.mediator.commands.*; /** * * */ public class Editor extends JComponent{ private Canvas canvas; private CompoundShape allShapes = new CompoundShape(); private History history; public Editor() { canvas = new Canvas( this ); history = new History(); } public void loadShapes(Shape... shapes) { allShapes.clear(); allShapes.add(shapes); canvas.refresh(); } public CompoundShape getShapes() { return allShapes; } public void execute(Command c) { history.push(c, new Memento( this )); c.execute(); } public void undo() { if (history.undo()) canvas.repaint(); } public void redo() { if (history.redo()) canvas.repaint(); } public String backup() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject( this .allShapes); oos.close(); return Base64.getEncoder().encodeToString(baos.toByteArray()); } catch (IOException e) { return "" ; } } public void restore(String state) { try { byte [] data = Base64.getDecoder().decode(state); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(data)); this .allShapes = (CompoundShape) ois.readObject(); ois.close(); } catch (ClassNotFoundException e) { System.out.print( "ClassNotFoundException occurred." ); } catch (IOException e) { System.out.print( "IOException occurred." ); } } } |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Canvas.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.editor; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import com.javapatterns.mediator.shapes.*; import com.javapatterns.mediator.commands.*; /** * * */ public class Canvas extends java.awt.Canvas{ private Editor editor; private JFrame frame; private static final int PADDING = 10 ; Canvas(Editor editor) { this .editor = editor; createFrame(); attachKeyboardListeners(); attachMouseListeners(); refresh(); } private void createFrame() { frame = new JFrame(); frame.setTitle( "调停者模式窗口--涂聚文" ); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setLocationRelativeTo( null ); JPanel contentPanel = new JPanel(); Border padding = BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING); contentPanel.setBorder(padding); contentPanel.setLayout( new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); frame.setContentPane(contentPanel); contentPanel.add( new JLabel( "Select and drag to move." ), BorderLayout.PAGE_END); contentPanel.add( new JLabel( "Right click to change color." ), BorderLayout.PAGE_END); contentPanel.add( new JLabel( "Undo: Ctrl+Z, Redo: Ctrl+R" ), BorderLayout.PAGE_END); contentPanel.add( this ); frame.setVisible( true ); contentPanel.setBackground(Color.LIGHT_GRAY); } private void attachKeyboardListeners() { addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 ) { switch (e.getKeyCode()) { case KeyEvent.VK_Z: editor.undo(); break ; case KeyEvent.VK_R: editor.redo(); break ; } } } }); } private void attachMouseListeners() { MouseAdapter colorizer = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (e.getButton() != MouseEvent.BUTTON3) { return ; } com.javapatterns.mediator.shapes.Shape target = editor.getShapes().getChildAt(e.getX(), e.getY()); if (target != null ) { editor.execute( new ColorCommand(editor, new Color(( int ) (Math.random() * 0x1000000 )))); repaint(); } } }; addMouseListener(colorizer); MouseAdapter selector = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (e.getButton() != MouseEvent.BUTTON1) { return ; } com.javapatterns.mediator.shapes.Shape target = editor.getShapes().getChildAt(e.getX(), e.getY()); boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK; if (target == null ) { if (!ctrl) { editor.getShapes().unSelect(); } } else { if (ctrl) { if (target.isSelected()) { target.unSelect(); } else { target.select(); } } else { if (!target.isSelected()) { editor.getShapes().unSelect(); } target.select(); } } repaint(); } }; addMouseListener(selector); MouseAdapter dragger = new MouseAdapter() { MoveCommand moveCommand; @Override public void mouseDragged(MouseEvent e) { if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != MouseEvent.BUTTON1_DOWN_MASK) { return ; } if (moveCommand == null ) { moveCommand = new MoveCommand(editor); moveCommand.start(e.getX(), e.getY()); } moveCommand.move(e.getX(), e.getY()); repaint(); } @Override public void mouseReleased(MouseEvent e) { if (e.getButton() != MouseEvent.BUTTON1 || moveCommand == null ) { return ; } moveCommand.stop(e.getX(), e.getY()); editor.execute(moveCommand); this .moveCommand = null ; repaint(); } }; addMouseListener(dragger); addMouseMotionListener(dragger); } public int getWidth() { return editor.getShapes().getX() + editor.getShapes().getWidth() + PADDING; } public int getHeight() { return editor.getShapes().getY() + editor.getShapes().getHeight() + PADDING; } void refresh() { this .setSize(getWidth(), getHeight()); frame.pack(); } public void update(Graphics g) { paint(g); } public void paint(Graphics graphics) { BufferedImage buffer = new BufferedImage( this .getWidth(), this .getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D ig2 = buffer.createGraphics(); ig2.setBackground(Color.WHITE); ig2.clearRect( 0 , 0 , this .getWidth(), this .getHeight()); editor.getShapes().paint(buffer.getGraphics()); graphics.drawImage(buffer, 0 , 0 , null ); } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc History.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.history; import java.util.ArrayList; import java.util.List; import com.javapatterns.mediator.commands.*; /** * * */ public class History { private List<Pair> history = new ArrayList<Pair>(); private int virtualSize = 0 ; private class Pair { Command command; Memento memento; Pair(Command c, Memento m) { command = c; memento = m; } private Command getCommand() { return command; } private Memento getMemento() { return memento; } } public void push(Command c, Memento m) { if (virtualSize != history.size() && virtualSize > 0 ) { history = history.subList( 0 , virtualSize - 1 ); } history.add( new Pair(c, m)); virtualSize = history.size(); } public boolean undo() { Pair pair = getUndo(); if (pair == null ) { return false ; } System.out.println( "Undoing: " + pair.getCommand().getName()); pair.getMemento().restore(); return true ; } public boolean redo() { Pair pair = getRedo(); if (pair == null ) { return false ; } System.out.println( "Redoing: " + pair.getCommand().getName()); pair.getMemento().restore(); pair.getCommand().execute(); return true ; } private Pair getUndo() { if (virtualSize == 0 ) { return null ; } virtualSize = Math.max( 0 , virtualSize - 1 ); return history.get(virtualSize); } private Pair getRedo() { if (virtualSize == history.size()) { return null ; } virtualSize = Math.min(history.size(), virtualSize + 1 ); return history.get(virtualSize - 1 ); } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Memento.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.history; import com.javapatterns.mediator.editor.*; /** * * */ public class Memento { private String backup; private Editor editor; public Memento(Editor editor) { this .editor = editor; this .backup = editor.backup(); } public void restore() { editor.restore(backup); } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BaseShape.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; /** * * */ public abstract class BaseShape implements Shape { int x, y; private int dx = 0 , dy = 0 ; private Color color; private boolean selected = false ; BaseShape( int x, int y, Color color) { this .x = x; this .y = y; this .color = color; } @Override public int getX() { return x; } @Override public int getY() { return y; } @Override public int getWidth() { return 0 ; } @Override public int getHeight() { return 0 ; } @Override public void drag() { dx = x; dy = y; } @Override public void moveTo( int x, int y) { this .x = dx + x; this .y = dy + y; } @Override public void moveBy( int x, int y) { this .x += x; this .y += y; } @Override public void drop() { this .x = dx; this .y = dy; } @Override public boolean isInsideBounds( int x, int y) { return x > getX() && x < (getX() + getWidth()) && y > getY() && y < (getY() + getHeight()); } @Override public Color getColor() { return color; } @Override public void setColor(Color color) { this .color = color; } @Override public void select() { selected = true ; } @Override public void unSelect() { selected = false ; } @Override public boolean isSelected() { return selected; } void enableSelectionStyle(Graphics graphics) { graphics.setColor(Color.LIGHT_GRAY); Graphics2D g2 = (Graphics2D) graphics; float dash1[] = { 2 .0f}; g2.setStroke( new BasicStroke( 1 .0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2 .0f, dash1, 0 .0f)); } void disableSelectionStyle(Graphics graphics) { graphics.setColor(color); Graphics2D g2 = (Graphics2D) graphics; g2.setStroke( new BasicStroke()); } @Override public void paint(Graphics graphics) { if (isSelected()) { enableSelectionStyle(graphics); } else { disableSelectionStyle(graphics); } // ... } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Circle.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; /** * * */ public class Circle extends BaseShape { private int radius; public Circle( int x, int y, int radius, Color color) { super (x, y, color); this .radius = radius; } @Override public int getWidth() { return radius * 2 ; } @Override public int getHeight() { return radius * 2 ; } @Override public void paint(Graphics graphics) { super .paint(graphics); graphics.drawOval(x, y, getWidth() - 1 , getHeight() - 1 ); } } |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc CompoundShape.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * * */ public class CompoundShape extends BaseShape { private List<Shape> children = new ArrayList<>(); public CompoundShape(Shape... components) { super ( 0 , 0 , Color.BLACK); add(components); } public void add(Shape component) { children.add(component); } public void add(Shape... components) { children.addAll(Arrays.asList(components)); } public void remove(Shape child) { children.remove(child); } public void remove(Shape... components) { children.removeAll(Arrays.asList(components)); } public void clear() { children.clear(); } @Override public int getX() { if (children.size() == 0 ) { return 0 ; } int x = children.get( 0 ).getX(); for (Shape child : children) { if (child.getX() < x) { x = child.getX(); } } return x; } @Override public int getY() { if (children.size() == 0 ) { return 0 ; } int y = children.get( 0 ).getY(); for (Shape child : children) { if (child.getY() < y) { y = child.getY(); } } return y; } @Override public int getWidth() { int maxWidth = 0 ; int x = getX(); for (Shape child : children) { int childsRelativeX = child.getX() - x; int childWidth = childsRelativeX + child.getWidth(); if (childWidth > maxWidth) { maxWidth = childWidth; } } return maxWidth; } @Override public int getHeight() { int maxHeight = 0 ; int y = getY(); for (Shape child : children) { int childsRelativeY = child.getY() - y; int childHeight = childsRelativeY + child.getHeight(); if (childHeight > maxHeight) { maxHeight = childHeight; } } return maxHeight; } @Override public void drag() { for (Shape child : children) { child.drag(); } } @Override public void drop() { for (Shape child : children) { child.drop(); } } @Override public void moveTo( int x, int y) { for (Shape child : children) { child.moveTo(x, y); } } @Override public void moveBy( int x, int y) { for (Shape child : children) { child.moveBy(x, y); } } @Override public boolean isInsideBounds( int x, int y) { for (Shape child : children) { if (child.isInsideBounds(x, y)) { return true ; } } return false ; } @Override public void setColor(Color color) { super .setColor(color); for (Shape child : children) { child.setColor(color); } } @Override public void unSelect() { super .unSelect(); for (Shape child : children) { child.unSelect(); } } public Shape getChildAt( int x, int y) { for (Shape child : children) { if (child.isInsideBounds(x, y)) { return child; } } return null ; } public boolean selectChildAt( int x, int y) { Shape child = getChildAt(x,y); if (child != null ) { child.select(); return true ; } return false ; } public List<Shape> getSelected() { List<Shape> selected = new ArrayList<>(); for (Shape child : children) { if (child.isSelected()) { selected.add(child); } } return selected; } @Override public void paint(Graphics graphics) { if (isSelected()) { enableSelectionStyle(graphics); graphics.drawRect(getX() - 1 , getY() - 1 , getWidth() + 1 , getHeight() + 1 ); disableSelectionStyle(graphics); } for (Shape child : children) { child.paint(graphics); } } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Dot.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; /** * * */ public class Dot extends BaseShape { private final int DOT_SIZE = 3 ; public Dot( int x, int y, Color color) { super (x, y, color); } @Override public int getWidth() { return DOT_SIZE; } @Override public int getHeight() { return DOT_SIZE; } @Override public void paint(Graphics graphics) { super .paint(graphics); graphics.fillRect(x - 1 , y - 1 , getWidth(), getHeight()); } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Rectangle.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; /** * * */ public class Rectangle extends BaseShape { private int width; private int height; public Rectangle( int x, int y, int width, int height, Color color) { super (x, y, color); this .width = width; this .height = height; } @Override public int getWidth() { return width; } @Override public int getHeight() { return height; } @Override public void paint(Graphics graphics) { super .paint(graphics); graphics.drawRect(x, y, getWidth() - 1 , getHeight() - 1 ); } } |
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 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 中介者模式/调停者模式 Mediator Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Shape.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.mediator.shapes; import java.awt.*; import java.io.Serializable; /** * * */ public interface Shape extends Serializable { int getX(); int getY(); int getWidth(); int getHeight(); void drag(); void drop(); void moveTo( int x, int y); void moveBy( int x, int y); boolean isInsideBounds( int x, int y); Color getColor(); void setColor(Color color); void select(); void unSelect(); boolean isSelected(); void paint(Graphics graphics); } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //中介者模式/调停者模式 Editor Dueditor = new Editor(); Dueditor.loadShapes( new com.javapatterns.mediator.shapes.Circle( 10 , 10 , 10 , Color.BLUE), new com.javapatterns.mediator.shapes.CompoundShape( new com.javapatterns.mediator.shapes.Circle( 110 , 110 , 50 , Color.RED), new com.javapatterns.mediator.shapes.Dot( 160 , 160 , Color.RED) ), new com.javapatterns.mediator.shapes.CompoundShape( new com.javapatterns.mediator.shapes.Rectangle( 250 , 250 , 100 , 100 , Color.GREEN), new com.javapatterns.mediator.shapes.Dot( 240 , 240 , Color.GREEN), new com.javapatterns.mediator.shapes.Dot( 240 , 360 , Color.GREEN), new com.javapatterns.mediator.shapes.Dot( 360 , 360 , Color.GREEN), new com.javapatterns.mediator.shapes.Dot( 360 , 240 , Color.GREEN) ) ); |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!