ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 11.06.2019

Просмотров: 152

Скачиваний: 5

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Лабораторная работа №6

Рефакторинг программного кода

Студента ИТ14-1 Красовского Абхая


Цель работы: научиться выполнять реорганизацию программного кода на основании шаблонов рефакторинга.

Ход выполнения работы

  1. Выполнить анализ программного кода разрабатываемого ПО и модульных тестов с целью выявления плохо организованного кода.

  2. Используя шаблоны рефакторинга, выполнить реорганизацию программного кода разрабатываемого ПО.

  3. Выполнить описание произведенных операций рефакторинга (было-стало-шаблон рефакторинга).

  4. В случае необходимости скорректировать проектную документацию (диаграммы классов, последовательностей).

  5. Сделать выводы по результатам выполнения работы.


Класс Main

Было:

package game;

import game.Characters.*;

import game.Characters.Character;

import game.Energetics.Energetic;

import game.Energetics.Lightning;

import game.Levels.Block;

import game.Levels.Level;

import game.Levels.Level_data;

import game.Weapon.Bullet;

import game.Weapon.Weapon;

import javafx.animation.AnimationTimer;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.input.KeyCode;

import javafx.scene.layout.Pane;

import javafx.stage.Stage;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

public class Main extends Application {

public static ArrayList<Block> blocks = new ArrayList<>();

public static ArrayList<Bullet> bullets = new ArrayList<>();

public static ArrayList<Bullet> enemyBullets = new ArrayList<>();

public static ArrayList<EnemyBase> enemies = new ArrayList<>();

static HashMap<KeyCode, Boolean> keys = new HashMap<>();

public static Stage stage;

public static Scene scene;

public static Pane gameRoot = new Pane();

public static Pane appRoot = new Pane();

public static Menu menu;

public static Character booker;

public static HUD hud;

public static Weapon weapon;

public static Elizabeth elizabeth;

static VendingMachine vendingMachine;

static Tutorial tutorial;

private static CutScenes cutScene;

public static Energetic energetic;

public static Lightning lightning;

public static int levelNumber;

static Level level;

public static AnimationTimer timer = new AnimationTimer() {

@Override

public void handle(long now) {

update();

}

};

private static void update() {

for (EnemyBase enemy : enemies) {

enemy.update();

if (enemy.getDelete()) {

enemies.remove(enemy);

break;

}

}

Bullet.update();

Controller.update();

booker.update();

if (!energetic.getName().equals(""))

energetic.update();

if (levelNumber > 0)

elizabeth.update();

if (lightning != null) {

lightning.update();

if (lightning.getDelete())

lightning = null;

}

menu.update();

hud.update();

weapon.update();

if (booker.getTranslateX() > Level_data.BLOCK_SIZE * 295)

cutScene = new CutScenes(levelNumber);

}

@Override

public void start(Stage primaryStage) throws Exception {

stage = primaryStage;

scene = new Scene(appRoot, 1280, 720);

appRoot.getChildren().add(gameRoot);

level = new Level();

try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("C:/DeadShock/saves/data.dat"))) {

levelNumber = dataInputStream.readInt();

level.createLevels(levelNumber);

level.changeImageView(levelNumber);

vendingMachine = new VendingMachine();

booker = new Character();

booker.setMoney(dataInputStream.readInt());

booker.setSalt(dataInputStream.readByte());

booker.setCountLives(2);

weapon = new Weapon();

weapon.setWeaponClip(dataInputStream.readInt());

weapon.setBullets(dataInputStream.readInt());

hud = new HUD();

elizabeth = new Elizabeth();

energetic = new Energetic();

} catch (IOException e) {

levelNumber = 0;

level.createLevels(levelNumber);

vendingMachine = new VendingMachine();

booker = new Character();

weapon = new Weapon();

hud = new HUD();

energetic = new Energetic();

tutorial = new Tutorial(levelNumber);

}

switch (levelNumber) {

case 0:

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 117, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 127, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 148, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 161, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 171, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 185, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 204, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 215, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 228, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 233, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 243, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 252, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 262, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 277, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 280, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 286, 200));

break;

case 1:

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 57, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 67, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 74, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 87, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 104, 150));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 117, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 133, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 156, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 177, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 193, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 201, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 216, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 224, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 246, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 260, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 277, 100));


Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 34, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 36, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 60, Level_data.BLOCK_SIZE * 9));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 61, Level_data.BLOCK_SIZE * 9));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 106, Level_data.BLOCK_SIZE * 7));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 107, Level_data.BLOCK_SIZE * 7));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 168, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 170, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 196, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 197, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 232, Level_data.BLOCK_SIZE * 8));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 233, Level_data.BLOCK_SIZE * 8));

break;

}

menu = new Menu();

appRoot.getChildren().add(menu.menuBox);

booker.translateXProperty().addListener( ((observable, oldValue, newValue) -> {

int offset = newValue.intValue();

if (offset > 600 && offset < gameRoot.getWidth() - 680) {

gameRoot.setLayoutX( - (offset - 600) );

level.getBackground().setLayoutX((offset - 600) / 1.5);

}

if (offset <= 100)

level.getBackground().setLayoutX(0);

}));

vendingMachine.createButtons();

stage.getIcons().add(new Image("file:/C:/DeadShock/images/icon.jpg"));

stage.setTitle("DeadShock");

stage.setResizable(false);

stage.setWidth(scene.getWidth());

stage.setHeight(scene.getHeight());

stage.setScene(scene);

stage.show();

timer.start();

}

public static void main(String[] args) {

launch(args);

}

}


Стало:

package game;


import game.Characters.*;

import game.Characters.Character;

import game.Energetics.Energetic;

import game.Energetics.Lightning;

import game.Levels.Block;

import game.Levels.Level;

import game.Levels.Level_data;

import game.Weapon.Bullet;

import game.Weapon.Weapon;

import javafx.animation.AnimationTimer;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.input.KeyCode;

import javafx.scene.layout.Pane;

import javafx.stage.Stage;


import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;



public class Main extends Application {

public static ArrayList<Block> blocks = new ArrayList<>();

public static ArrayList<Bullet> bullets = new ArrayList<>();

public static ArrayList<Bullet> enemyBullets = new ArrayList<>();

public static ArrayList<EnemyBase> enemies = new ArrayList<>();

static HashMap<KeyCode, Boolean> keys = new HashMap<>();


public static Stage stage;

public static Scene scene;


public static Pane gameRoot = new Pane();

public static Pane appRoot = new Pane();


public static Menu menu;

public static Character booker;

public static HUD hud;

public static Weapon weapon;

public static Elizabeth elizabeth;

static VendingMachine vendingMachine;


static Tutorial tutorial;

private static CutScenes cutScene;

public static Energetic energetic;

public static Lightning lightning;


public static int levelNumber;


static Level level;


public static AnimationTimer timer = new AnimationTimer() {

@Override

public void handle(long now) {

update();

}

};






private void initContent() {

appRoot.getChildren().add(gameRoot);


level = new Level();


try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("C:/DeadShock/saves/data.dat"))) {

levelNumber = dataInputStream.readInt();

level.createLevels(levelNumber);

level.changeImageView(levelNumber);


vendingMachine = new VendingMachine();


booker = new Character();

booker.setMoney(dataInputStream.readInt());

booker.setSalt(dataInputStream.readByte());

booker.setCountLives(2);


weapon = new Weapon();

weapon.setWeaponClip(dataInputStream.readInt());

weapon.setBullets(dataInputStream.readInt());


hud = new HUD();

elizabeth = new Elizabeth();

energetic = new Energetic();

} catch (IOException e) {

levelNumber = 0;

level.createLevels(levelNumber);

vendingMachine = new VendingMachine();

booker = new Character();

weapon = new Weapon();

hud = new HUD();

energetic = new Energetic();

tutorial = new Tutorial(levelNumber);

}


createEnemies();


menu = new Menu();

appRoot.getChildren().add(menu.menuBox);


booker.translateXProperty().addListener( ((observable, oldValue, newValue) -> {

int offset = newValue.intValue();

if (offset > 600 && offset < gameRoot.getWidth() - 680) {

gameRoot.setLayoutX( - (offset - 600) );

level.getBackground().setLayoutX((offset - 600) / 1.5);

}

if (offset <= 100)

level.getBackground().setLayoutX(0);

}));


vendingMachine.createButtons();

}



public static void createEnemies() {

switch (levelNumber) {

case 0:

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 117, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 127, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 148, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 161, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 171, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 185, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 204, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 215, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 228, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 233, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 243, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 252, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 262, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 277, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 280, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 286, 200));

break;

case 1:

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 57, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 67, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 74, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 87, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 104, 150));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 117, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 133, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 156, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 177, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 193, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 201, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 216, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 224, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 246, 200));

enemies.add(new EnemyRedEye(Level_data.BLOCK_SIZE * 260, 200));

enemies.add(new EnemyComstock(Level_data.BLOCK_SIZE * 277, 100));


Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 34, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 36, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 60, Level_data.BLOCK_SIZE * 9));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 61, Level_data.BLOCK_SIZE * 9));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 106, Level_data.BLOCK_SIZE * 7));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 107, Level_data.BLOCK_SIZE * 7));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 168, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 170, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 196, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 197, Level_data.BLOCK_SIZE * 13));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 232, Level_data.BLOCK_SIZE * 8));

Level_data.enemyBlocks.add(new Block("invisible", Level_data.BLOCK_SIZE * 233, Level_data.BLOCK_SIZE * 8));

break;

}

}



private static void update() {

for (EnemyBase enemy : enemies) {

enemy.update();

if (enemy.getDelete()) {

enemies.remove(enemy);

break;

}

}


Bullet.update();

Controller.update();

booker.update();


if (!energetic.getName().equals(""))

energetic.update();

if (levelNumber > 0)

elizabeth.update();

if (lightning != null) {

lightning.update();

if (lightning.getDelete())

lightning = null;

}


menu.update();

hud.update();

weapon.update();


if (booker.getTranslateX() > Level_data.BLOCK_SIZE * 295)

cutScene = new CutScenes(levelNumber);

}


@Override

public void start(Stage primaryStage) throws Exception {

stage = primaryStage;

scene = new Scene(appRoot, 1280, 720);


initContent();

stage.getIcons().add(new Image("file:/C:/DeadShock/images/icon.jpg"));

stage.setTitle("DeadShock");

stage.setResizable(false);

stage.setWidth(scene.getWidth());

stage.setHeight(scene.getHeight());

stage.setScene(scene);

stage.show();

timer.start();

}



public static void main(String[] args) {

launch(args);

}

}



Шаблон рефакторинга: Выделение метода(Extract Method)


Вывод: научился выполнять реорганизацию программного кода на основании шаблонов рефакторинга.


Смотрите также файлы