有些代码需要在大约10个类中进行重构 班级有条件散落在整个班级的绘画中 适当时将文字放到屏幕上。我想添加所有这些代码条件/绘画 代码集成到一个集中位置。 有适合的设计模式吗?
if (fontWidth == 100) {
fontSize = large;
} else {
fontSize = small;
}
if (fontWidth == 100) {
graphics.drawText("100", xVal, yVal);
} else {
xText = Utils.calculateXText(param1, param2);
graphics.drawText("200", xVal, yVal);
}
对于委托是否意味着我需要编写不同的包装器对象,一个用于设置字体大小,另一个用于根据上面的代码绘制文本?
public void paint(Graphics graphics){
super.paint(graphics);
Font font;
if(ScreenDimension.getFontWidth() == 320){
font = Font.getDefault().derive(Font.PLAIN,10,Ui.UNITS_pt);
}
else {
font = Font.getDefault().derive(Font.PLAIN,12,Ui.UNITS_pt);
}
String strScore = String.valueOf(score);
graphics.setFont(font);
int pixelWidth = font.getAdvance(strScore);
if(ScreenDimension.getFontWidth() == 320){
graphics.drawText(strScore, (int) (this.backgroundBitmap.getWidth() * 0.50) - (pixelWidth/2), 10);
}
else {
graphics.drawText(strScore, (int) (this.backgroundBitmap.getWidth() * 0.50) - (pixelWidth/2), 20);
}
if(ScreenDimension.getFontWidth() == 320){
font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt);
}
else {
font = Font.getDefault().derive(Font.PLAIN,8,Ui.UNITS_pt);
}
int namePixelWidth = font.getAdvance(judgeName);
graphics.setFont(font);
if(ScreenDimension.getFontWidth() == 320){
graphics.drawText(judgeName, (int) (this.backgroundBitmap.getWidth() * 0.50) - (namePixelWidth/2), this.backgroundBitmap.getHeight() - font.getHeight() - 2);
}
else {
graphics.drawText(judgeName, (int) (this.backgroundBitmap.getWidth() * 0.50) - (namePixelWidth/2), this.backgroundBitmap.getHeight() - font.getHeight() - 15);
}
}
答案 0 :(得分:4)
您可以使用委托对象来检查您的10个类以正确地绘制文本。
你也可以简单地创建一个静态辅助函数来执行该代码(简单方便)。
事实上,如果没有看到更多代码,很难给出正确的意见。
答案 1 :(得分:1)
您可能想要使用Strategy模式,定义类似
的内容/**
* Strategy to render some text, of two size variants, centered horizontally
* at a specified position.
*/
public interface TextRenderer {
public enum FontSize { LARGE, SMALL };
public void render(String text, int x, int y, FontSize size);
}
/**
* TextRenderer Factory that uses the ScreenDimension to determine sizes of LARGE
* and SMALL fonts to use.
*/
public class ScreenDimensionAwareTextRendererFactory {
public static TextRenderer getTextRenderer(Graphics gc) {
Font baseFont = Font.getDefault();
int fontWidth = ScreenDimension.getFontWidth();
return fontWidth == 320
? new DefaultTextRenderer(gc, baseFont.derive(Font.PLAIN,10,Ui.UNITS_pt), baseFont.derive(Font.PLAIN,6,Ui.UNITS_pt))
: new DefaultTextRenderer(gc, baseFont.derive(Font.PLAIN,12,Ui.UNITS_pt), baseFont.derive(Font.PLAIN,8,Ui.UNITS_pt));
}
}
public class DefaultTextRenderer implements TextRenderer {
protected Map<FontSize,Font> fontSizeMap = new HashMap<FontSize,Font>();
protected Graphics gc;
public DefaultTextRenderer(Graphics gc, Font largeFont, Font smallFont) {
this.gc = gc;
fontSizeMap.put(LARGE, largeFont);
fontSizeMap.put(SMALL, smallFont);
}
public void render(String text, int x, int y, FontSize size) {
Font font = fontSizeMap.get(size)
int pixelWidth = font.getAdvance(text);
gc.setFont(font);
// TODO: note here I'm not dealing with the vertical offsets you're using
// which are dependent upon the font size. It would be possible, but
// I suspect what you really ought to be doing is consistently rendering
// text on a baseline.
// The way you could adjust this to closer match what you appear to be doing
// would be to have arguments to render() which indicate vertical alignment.
// something like
// TextRenderer.VerticalAlignment = enum { BASELINE, TOP, BOTTOM }
// and based on something like that you could compute here the y offset
// based on the selected font.
// I can't do that now because you're "magic numbers" hard coded don't explain
// what they're trying to do
gc.drawText(text, Math.round(x - (pixelWidth / 2)), y);
}
}
// your example
public void paint(Graphics graphics) {
super.paint(graphics);
String strScore = String.valueOf(score);
TextRenderer textRenderer = ScreenDimensionAwareTextRendererFactory.getTextRenderer(graphics);
int middleX = Math.round(this.backgroundBitmap.getWidth() / 2);
textRenderer.render(strScore, middleX, 10, TextRenderer.TextSize.LARGE);
textRenderer.render(judgeName, middleX, this.backgroundBitmap.getHeight(), TextRenderer.TextSize.SMALL);
}
答案 2 :(得分:0)
如果条件不变,则可以使用Servant设计模式。