【设计模式】命令模式 实验报告

实 验 报 告

题    目     命令模式
课    程     设计模式
专    业     软件工程
班    级     软件1814班
姓    名     你猜
学    号     20188888888

一、实验名称

创建型设计模式实验

二、实验目的

1、熟悉命令模式的概念。
2、掌握命令模式的类图。
3、根据命令模式类图编写代码。

三、实验内容与结果分析

1、程序UML类图
在这里插入图片描述
2、运行效果
在这里插入图片描述
3、Order.java程序代码

package CommandPattern_Book;

public interface Order {
    void doExecute();
    void undoExecute();
}

4、Light.java程序代码

package CommandPattern_Book;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

class Light extends ImageView{
    private Image image1 = new Image("/CommandPattern_Book/ex_01.png");
    private Image image2 = new Image("/CommandPattern_Book/ex_02.png");

    void on() {
        this.setImage(image1);
        this.setSmooth(true);
        this.setCache(true);
        this.setFitHeight(200);
        this.setFitWidth(200);
    }

    void off() {
        this.setImage(image2);
        this.setSmooth(true);
        this.setCache(true);
        this.setFitHeight(200);
        this.setFitWidth(200);
    }

}

5、Camera.java程序代码

package CommandPattern_Book;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

class Camera extends ImageView {
    private Image image1 = new Image("/CommandPattern_Book/ex_03.png");
    private Image image2 = new Image("/CommandPattern_Book/ex_04.png");

    void on() {
        this.setImage(image1);
        this.setSmooth(true);
        this.setCache(true);
        this.setFitHeight(200);
        this.setFitWidth(200);
    }

    void off() {
        this.setImage(image2);
        this.setSmooth(true);
        this.setCache(true);
        this.setFitHeight(200);
        this.setFitWidth(200);
    }
}

6、Operation.java程序代码

package CommandPattern_Book;

public enum Operation {
    OnLight,
    OffLight,
    OnCamera,
    OffCamera
}

7、OffCameraOrder.java程序代码

package CommandPattern_Book;

public class OffCameraOrder implements  Order {
    private Camera camera;

    OffCameraOrder(Camera camera) {
        this.camera = camera;
    }

    @Override
    public void doExecute() {
        camera.off();
    }

    @Override
    public void undoExecute() {
        camera.on();
    }
}

8、OffLightOrder.java程序代码

package CommandPattern_Book;

public class OffLightOrder implements Order {
    private Light light;

    OffLightOrder(Light light) {
        this.light = light;
    }

    @Override
    public void doExecute() {
        light.off();
    }

    @Override
    public void undoExecute() {
        light.on();
    }
}

9、OnCameraOrder.java程序代码

package CommandPattern_Book;

public class OnCameraOrder implements Order {
    private Camera camera;

    OnCameraOrder(Camera camera) {
        this.camera = camera;
    }

    @Override
    public void doExecute() {
        camera.on();
    }

    @Override
    public void undoExecute() {
        camera.off();
    }
}

10、OnLightOrder.java程序代码

package CommandPattern_Book;

public class OnLightOrder implements Order {
    private Light light;

    OnLightOrder(Light light) {
        this.light = light;
    }

    @Override
    public void doExecute() {
        light.on();
    }

    @Override
    public void undoExecute() {
        light.off();
    }
}

11、Pane.java程序代码

package CommandPattern_Book;

import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class Pane implements Initializable {
    public AnchorPane LightView;
    public AnchorPane CameraView;
    public AnchorPane Undo;

    private ArrayList<Operation> arrayList = new ArrayList<>();
    private Operation LightStatus = Operation.OffLight;
    private Operation CameraStatus = Operation.OffCamera;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        CameraView.getChildren().add(camera);
        LightView.getChildren().add(light);

        ImageView imageView = new ImageView(new Image("/CommandPattern_Book/myUndo.png"));
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        imageView.setSmooth(true);
        imageView.setCache(true);
        Undo.getChildren().add(imageView);
        isShow();
    }

    private void isShow(){
        if (arrayList == null){
            Undo.setVisible(false);
            return;
        }
        if (arrayList.size() == 0) {
            Undo.setVisible(false);
            return;
        }
        if (!Undo.isVisible()) {
            Undo.setVisible(true);
        }
    }

    private Light light = new Light();
    private Camera camera = new Camera();

    private Order onLightOrder = new OnLightOrder(light);
    private Order offLightOrder = new OffLightOrder(light);
    private Order onCameraOrder = new OnCameraOrder(camera);
    private Order offCameraOrder = new OffCameraOrder(camera);

    public void CameraOn(ActionEvent actionEvent) {
        if (CameraStatus == Operation.OnCamera) {
            return;
        }
        onCameraOrder.doExecute();
        arrayList.add(Operation.OnCamera);
        CameraStatus = Operation.OnCamera;
        isShow();
    }

    public void CameraOff(ActionEvent actionEvent) {
        if (CameraStatus == Operation.OffCamera) {
            return;
        }
        offCameraOrder.doExecute();
        arrayList.add(Operation.OffCamera);
        CameraStatus = Operation.OffCamera;
        isShow();
    }

    public void LightOn(ActionEvent actionEvent) {

        if (LightStatus == Operation.OnLight) {
            return;
        }
        onLightOrder.doExecute();
        arrayList.add(Operation.OnLight);
        LightStatus = Operation.OnLight;
        isShow();
    }

    public void LightOff(ActionEvent actionEvent) {
        if (LightStatus == Operation.OffLight) {
            return;
        }
        offLightOrder.doExecute();
        arrayList.add(Operation.OffLight);
        LightStatus = Operation.OffLight;
        isShow();
    }

    public void UndoClicked(MouseEvent mouseEvent) {
        Operation operation = arrayList.get(arrayList.size() - 1);
        switch (operation){
            case OnLight: onLightOrder.undoExecute(); LightStatus = Operation.OffLight; break;
            case OffLight: offLightOrder.undoExecute(); LightStatus = Operation.OnLight; break;
            case OnCamera: onCameraOrder.undoExecute(); CameraStatus = Operation.OffCamera; break;
            case OffCamera: offCameraOrder.undoExecute(); CameraStatus = Operation.OnCamera; break;
        }
        arrayList.remove(arrayList.size() - 1);
        isShow();
    }

}

12、Main.java程序代码

package CommandPattern_Book;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent parent = FXMLLoader.load(Main.class.getResource("./Pane.fxml"));
        primaryStage.setScene(new Scene(parent, 400, 400));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

13、Pane.fxml 程序代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="500.0" maxWidth="500.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.131" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CommandPattern_Book.Pane">
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <AnchorPane prefHeight="200.0" prefWidth="200.0">
               <children>
                  <HBox prefHeight="100.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <AnchorPane fx:id="LightView" HBox.hgrow="ALWAYS" />
                        <AnchorPane fx:id="CameraView" HBox.hgrow="ALWAYS" />
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
            <AnchorPane VBox.vgrow="ALWAYS">
               <children>
                  <GridPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Button mnemonicParsing="false" onAction="#LightOff" prefHeight="500.0" prefWidth="500.0" styleClass="bto" stylesheets="/CommandPattern_Book/tpye.css" text="LightOff" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS" />
                        <Button mnemonicParsing="false" onAction="#CameraOn" prefHeight="500.0" prefWidth="500.0" styleClass="bto" stylesheets="/CommandPattern_Book/tpye.css" text="CameraOn" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS" />
                        <Button mnemonicParsing="false" onAction="#CameraOff" prefHeight="500.0" prefWidth="500.0" styleClass="bto" stylesheets="/CommandPattern_Book/tpye.css" text="CameraOff" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS" />
                        <Button mnemonicParsing="false" onAction="#LightOn" prefHeight="500.0" prefWidth="500.0" styleClass="bto" stylesheets="/CommandPattern_Book/tpye.css" text="LightOn" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS"/>
                     </children>
                  </GridPane>
               </children>
            </AnchorPane>
         </children>
      </VBox>
      <AnchorPane fx:id="Undo" onMouseClicked="#UndoClicked" prefHeight="68.0" prefWidth="71.0" />
   </children>
</AnchorPane>

14、tpye.css 程序代码

.bto {
    -fx-font-size: 25;
    /*-fx-base: lightgreen;*/
    -fx-font-weight: lighter;
    -fx-text-fill: rgb(50, 50, 50);
    /*-fx-effect: innershadow(three-pass-box, #72b9da, 20, 0.2, 0, 0);*/
    /*-fx-effect: innershadow( three-pass-box , rgba(195,255,0,0.5) , 50, 0.1 , 0 , 2 );*/
}
.bto :hover {
    -fx-font-size: 25;
    /*-fx-base: lightgreen;*/
    -fx-font-weight: bold;
    /*-fx-text-fill: rgb(50, 50, 50);*/
    /*-fx-effect: innershadow(three-pass-box, #72b9da, 20, 0.2, 0, 0);*/
    -fx-effect: innershadow( three-pass-box , rgba(195,255,0,0.5) , 50, 0.1 , 0 , 2 );

}

15、ex_01.png资源
在这里插入图片描述
16、ex_02.png资源
在这里插入图片描述
17、ex_03.png资源
在这里插入图片描述
18、ex_04.png资源
在这里插入图片描述
19、myUndo.png资源
在这里插入图片描述
20、github
https://github.com/Rsslosc/javafx-team

四、心得体会

通过这次实验,我学会了如何使用命令模式,如何通过命令模式来进行一系列的操作,不用深入程序内部,只需要调用相关的代码就可以实现灯泡的开关,摄像头的开关,对于面向对象思想有了更深入的理解。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页