介绍
UML
总结
实例
package HomeWork4;
import java.awt.Canvas;
import java.awt.Graphics;
public interface Builder {//定义抽象制造者规定制造过程
//第一步装车顶三角形
public void BuildTri(Canvas canvas,int width,int height,Graphics graphics);
//第二步装车身矩形
public void BuildRec(Canvas canvas,int width,int height,Graphics graphics);
//第三步装车轮圆形
public void BuildRou(Canvas canvas,int width,int height,Graphics graphics);
//返回产品的方法:获得组装好的车
public Car GetCar();
}
package HomeWork4;
import java.awt.Canvas;
import java.awt.Graphics;
public interface Builder {//定义抽象制造者规定制造过程
//第一步装车顶三角形
public void BuildTri(Canvas canvas,int width,int height,Graphics graphics);
//第二步装车身矩形
public void BuildRec(Canvas canvas,int width,int height,Graphics graphics);
//第三步装车轮圆形
public void BuildRou(Canvas canvas,int width,int height,Graphics graphics);
//返回产品的方法:获得组装好的车
public Car GetCar();
}
package HomeWork4;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
public class Director {
//指挥建造者组装车
public void Construct(Builder builder,Canvas canvas){
int width = 1200;
int height = width / 16 * 9;
BufferStrategy bufferStrategy;//起到缓冲的作用
//图形类是所有图形上下文的抽象基类,允许应用程序绘制在各种设备上实现的组件上,以及屏幕外的图像上。
Graphics graphics;
bufferStrategy = canvas.getBufferStrategy();//为图形Graphics到画布Canvas的缓冲
graphics = bufferStrategy.getDrawGraphics();
//用当前绘图表面的背景色填充指定的矩形,以清除该矩形。此操作不使用当前的绘制模式。
graphics.clearRect(0, 0, width, height);
builder.BuildTri(canvas,width,height,graphics);
builder.BuildRec(canvas,width,height,graphics);
builder.BuildRou(canvas,width,height,graphics);
bufferStrategy.show();
graphics.dispose();//释放资源
}
}
package HomeWork4;
//定义具体产品
public class Car {
public void show(){
System.out.println("车已组装好,正在进行展示");
}
}
package HomeWork4;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
//客服端调用:客户去找指挥者买车(沟通需求),指挥者了解后划分部件,指挥工人建造者造车并返回产品
public class Client implements ActionListener{
public Canvas canvas= new Canvas();
public void operate(){
final int width;
final int height;
final String title = "Test Window";
width = 1200;
height = width / 16 * 9;
//Creating the frame.
JFrame frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭操作
frame.setLocationRelativeTo(null);//设置窗口打开的初始位置
frame.setResizable(false);//设置用户是否可调整此框架大小
frame.setVisible(true);//设置显示或者隐藏窗口
JToolBar toolbar=new JToolBar(); //工具栏,默认水平方向
frame.getContentPane().add(toolbar, "North");//工具栏添加到JFrame组件的北
//创建三个按钮选择颜色,橡皮擦,新建并为其设置监听事件
JButton rect_button=new JButton("车型展示");//颜色按钮
rect_button.addActionListener(this);
//将这三个按钮添加到工具栏
toolbar.add(rect_button);
//Creating the canvas.
//Canvas canvas = new Canvas();
canvas.setSize(width, height);//画布大小与底层组件frame一致
canvas.setBackground(Color.WHITE);//设置画布的背景颜色
canvas.setVisible(true);//设置画布显示
canvas.setFocusable(false);//设置不可聚焦
//Putting it all together.
frame.add(canvas);//将画布添加到窗口组件中
canvas.createBufferStrategy(3);//为此组件创建新的多缓冲策略
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("车型展示")){
//先找到老板和装车人员
Director director=new Director();
Builder builder=new ConcreteBuilder();
//沟通需求后,老板叫装车人员去装车
director.Construct(builder,canvas);
//装完后,装车人员拿来装好的车
Car car=builder.GetCar();
//车子展示
car.show();
}
}
public static void main(String[] args){
Client client=new Client();
client.operate();
}
}
``
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201203234807464.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ1MDIwMDUx,size_16,color_FFFFFF,t_70)