iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
1
自我挑戰組

大四資工人生,快畢業了,然後呢系列 第 20

#資工人生─Day20-OOPSE物件導向軟體工程

  • 分享至 

  • twitterImage
  •  

前言

  我覺得這堂課很有趣有很多設計方法XDD
  這次的程式,讓我重頭又複習了一次JAVA這個程式語言的語法,我發現自己對於繼承以及介面的相關應用還不太熟識,因此我會讓自己在這方面進行練習,我期望自己可以透過練習,學會抽象、封裝、多型、改寫、繼承、重用等相關概念,並在其中得到精華。
  我期許自己在學習軟體設計樣式,可以在物件導向中有著卓越的成長,一來可以理解物件導向的精華,二來在設計程式時可以設計出相對漂亮的系統,給自己的目標先再把JAVA程式語言的基本語法打穩,正如老師所說;「對於軟體設計這一行,不論是哪一種方法論,都應該紮好馬步,對底層的程式設計熟悉。」短期目標就是要多複習。第二目標就是透過練習,理解自己何處不熟,再去加強其方面的不足。

重用reuse

  利用抽象的概念以及運用繼承的觀念去設計一套 NNEntity,讓數字進行相乘、字串進行相連、顏色可以相混,這三種需求都可以用到相同的架構,亦即,在這些元素的「相互作用」上可以各自定義,但相互作用後的呈現是固定的,具有重用的。

設計方法

  在這個程式中,我們設計一個抽象類別: NNEntity,,並且將整數 (NNInteger)、字串 (NNString)、顏色 (NNColor) 設為此類別的子類別。
  抽象類別NNEntity中所宣告的方法 multiply() ,使得子類別可以根據需求進行擴充,並茄可以限制與要求子類別必須定義 multiply() 中的意義。在此,我們自行定義三個子類別:

  • NNInteger:multiply() 為數字的相乘
  • NNString: multiply() 為字串的相連
  • NNColor:multiply() 為顏色的相混
      我們透過抽象類別,定義子類別multiply(),我們有相同的行為(兩個元素相互作用),再根據子類別的需求不同(相乘、相連、相混)而作特定實作,這就是一種程式碼重用機制。因此我們在這個程式重用了multiply這個方法。
      下列簡述設計特點,分別為:
  • NNInteger:如同九九乘法表,將兩數相乘後,最後輸出結果。
  • NNString:以列為首,將相兩個字串進行相加,即為相連。
  • NNColor:首先我們透過equals判別兩相混的顏色是否相同,如果相同,則回傳相同的顏色,再來如果不同,那我們透過類似字串的方式,先將兩個顏色利用字串相連的方式相連,再來判斷其相連字串,去判別為哪種混色。若該相連字串不在判別內,則以列為首輸出兩顏色的相連字串。
      最後利用NNEntity型態宣告陣列,並在其中分別加入NNString、NNInteger、NNColor,三種類別的元素,並使用multiplyAndShow這個函式進行融合以及印出的動作。

程式碼

public class NNMultiplication {
	public static void main(String[] args) {
		NNEntity[] xListA = { new NNInteger(2), new NNInteger(3), new NNInteger(5), new NNInteger(6), new NNInteger(10) };
		NNEntity[] yListA = { new NNInteger(7), new NNInteger(2), 	new NNInteger(3), new NNInteger(4), new NNInteger(8) };
		TableDisplayer.multiplyAndShow(xListA, yListA);
		
		NNEntity[] xListB = { new NNString("Q"), new NNString("D"), new NNString("T"), new NNString("H"), new NNString("Z") };
		NNEntity[] yListB = { new NNString("Y"), new NNString("D"), new NNString("Z"), new NNString("V"), new NNString("B") };
		TableDisplayer.multiplyAndShow(xListB, yListB);
		
		NNEntity[] xListC = { new NNColor("Red"), new NNColor("Red"), new NNColor("Red"), new NNColor("Green"), new NNColor("Blue") };
		NNEntity[] yListC = { new NNColor("Green"), new NNColor("Blue"), new NNColor("Red"), new NNColor("Blue"), new NNColor("Green") };
		TableDisplayer.multiplyAndShow(xListC, yListC);
	}
}

abstract class NNEntity {
	public abstract NNEntity multiply(NNEntity otherone);
}

class NNInteger extends NNEntity {
	private int number;

	public NNInteger(int number) {
		this.number = number;
	}

	public NNInteger(NNInteger copy) {
		this(copy.number);
	}

	// 數字相乘
	public NNEntity multiply(NNEntity otherone) {
		if (otherone == null) {
			return null;
		} else if (getClass() != otherone.getClass()) {
			return null;
		} else {
			NNInteger otherone2 = (NNInteger) otherone;
			return new NNInteger(this.number * otherone2.number);
		}
	}

	public String toString() {
		return Integer.toString(number);
	}
}

class NNString extends NNEntity {
	private String words;

	public NNString(String words) {
		this.words = words;
	}

	public NNString(NNString copy) {
		this(copy.words);
	}

	// 字串相連
	public NNEntity multiply(NNEntity otherone) {
		if (otherone == null) {
			return null;
		} else if (getClass() != otherone.getClass()) {
			return null;
		} else {
			NNString otherone2 = (NNString) otherone;
			return new NNString(this.words + otherone2.words);
		}
	}

	public String toString() {
		return words;
	}
}

class NNColor extends NNEntity {
	private String color;

	public NNColor(String color) {
		this.color = color;
	}

	public NNColor(NNColor copy) {
		this(copy.color);
	}

	// 顏色相混
	public NNEntity multiply(NNEntity otherone) {
		if (otherone == null) {
			return null;
		} else if (getClass() != otherone.getClass()) {
			return null;
		} else {
			NNColor othercolor = (NNColor) otherone;
			if(this.color.equals(othercolor.color)){
				return new NNColor(this.color);
			}else{
				NNColor combinecolor = new NNColor(this.color+othercolor.color);
				if(combinecolor.color.equals("RedGreen")||combinecolor.color.equals("GreenRed")){
					return new NNColor("Yellow");
				}else if(combinecolor.color.equals("RedBlue")||combinecolor.color.equals("BlueRed")){
					return new NNColor("Purple");
				}else if(combinecolor.color.equals("BlueGreen")||combinecolor.color.equals("GreenBlue")){
					return new NNColor("Cyan");
				}
				return new NNColor(this.color+othercolor.color);
			}
			
		}
	}

	public String toString() {
		return color;
	}
}


class TableDisplayer {
	public static void multiplyAndShow(NNEntity[] xList, NNEntity[] yList) {
		/* Multiply */
		NNEntity[][] table = new NNEntity[yList.length][xList.length];
		for (int i = 0; i < yList.length; i++) {
			for (int j = 0; j < xList.length; j++) {
				table[i][j] = xList[j].multiply(yList[i]);
			}
		}
		/* Show */
		System.out.printf("%7s", "");
		for (int i = 0; i < xList.length; i++) {
			System.out.printf("%7s", xList[i]);
		}
		System.out.println();
		for (int i = 0; i < yList.length; i++) {
			System.out.printf("%7s", yList[i]);
			for (int j = 0; j < xList.length; j++) {
				System.out.printf("%7s", table[i][j]);
			}
			System.out.println();
		}
		System.out.println();
	}
}

執行畫面

測試資料:
數字
2、3、5、6、10
7、2、3、4、8
字元
Q、D、T、H、Z
T、D、Z、V、B
顏色
Red、Red、Red、Green、Blue
Green、Blue、Red、Blue、Green
測試結果:


上一篇
#資工人生─Day19-資訊研討會
下一篇
#資工人生─Day21-軟體工程
系列文
大四資工人生,快畢業了,然後呢31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言