前言
选修了《软件设计模式》,下面是建造者模式的作业,设计模式参考的是《软件设计模式》(刘伟 著)。画UML类图的工具是Visual Paradigm,还不是特别熟练..
作业描述

UML类图

效果

Java源代码
import java.awt.*;
import javax.swing.*;
public class BuilderPattern {
public class Circle{
int x,y,width,height;
public Circle(int x,int y,int width,int height){
super();
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
}
public class Rect{
int x,y,width,height;
public Rect(int x,int y,int width,int height){
super();
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
}
public class Triangle{
int x1,y1,x2,y2,x3,y3;
public Triangle(int x1,int y1,int x2,int y2,int x3,int y3){
super();
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public int getX1(){
return x1;
}
public int getY1(){
return y1;
}
public int getX2(){
return x2;
}
public int getY2(){
return y2;
}
public int getX3(){
return x3;
}
public int getY3(){
return y3;
}
public void setX1(int x1){
this.x1=x1;
}
public void setY1(int y1){
this.y1=y1;
}
public void setX2(int x2){
this.x2=x2;
}
public void setY2(int y2){
this.y2=y2;
}
public void setX3(int x3){
this.x3=x3;
}
public void setY3(int y3){
this.y3=y3;
}
}
public class Car{
private Circle frontWheel;
private Circle backWheel;
private Rect body;
private Triangle head;
public Circle getFrontWheel(){
return frontWheel;
}
public Circle getBackWheel(){
return backWheel;
}
public Rect getBody(){
return body;
}
public Triangle getHead(){
return head;
}
public void setFrontWheel(Circle fw){
frontWheel=fw;
}
public void setBackWheel(Circle bw){
backWheel=bw;
}
public void setBody(Rect b){
body=b;
}
public void setHead(Triangle h){
head=h;
}
}
public abstract class Builder{
protected Car car = new Car();
public abstract void buildFrontWheel();
public abstract void buildBackWheel();
public abstract void buildBody();
public abstract void buildHead();
public Car createCar(){
return car;
}
}
public class ConcreateBuilder extends Builder{
@Override
public void buildFrontWheel() {
Circle frontWheel = new Circle(300,200,50,50);
car.setFrontWheel(frontWheel);
}
@Override
public void buildBackWheel() {
Circle backWheel = new Circle(450,200,50,50);
car.setBackWheel(backWheel);
}
@Override
public void buildBody() {
Rect body = new Rect(270,150,260,50);
car.setBody(body);
}
@Override
public void buildHead() {
Triangle head = new Triangle(500,110,470,150,530,150);
car.setHead(head);
}
}
public class Director{
private Builder builder;
public Director(Builder builder){
this.builder=builder;
}
public void setBuilder(Builder builder) {
this.builder = builder;
}
public Car construct(){
builder.buildFrontWheel();
builder.buildBackWheel();
builder.buildBody();
builder.buildHead();
return builder.createCar();
}
}
public class MyPanel extends JPanel{
Director director = new Director(new ConcreateBuilder());
Car car = director.construct();
Circle fW = car.getFrontWheel();
Circle bW = car.getBackWheel();
Rect body = car.getBody();
Triangle head = car.getHead();
@Override
public void paint(Graphics g){
g.drawOval(fW.getX(),fW.getY(),fW.getWidth(),fW.getHeight());
g.drawOval(bW.getX(),bW.getY(),bW.getWidth(),bW.getHeight());
g.drawRect(body.getX(),body.getY(),body.getWidth(),body.getHeight());
g.drawLine(head.getX1(),head.getY1(),head.getX2(),head.getY2());
g.drawLine(head.getX1(),head.getY1(),head.getX3(),head.getY3());
}
}
public class MyFrame extends JFrame {
private MyPanel panel = new MyPanel();
public MyFrame(){
this.setTitle("BuilderPattern");
this.setSize(800,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
}
}
public static void main(String [] args){
BuilderPattern.MyFrame f = new BuilderPattern().new MyFrame();
}
}
网友评论