Software engineering – Describe View, ViewModel, DataService, BluetoothService relationships by interaction diagram, class diagram

Prerequisite:

  • Interaction diagram描述object之間的互動
  • Class diagram描述class之間構成的結構與關係

Ref:

Interaction diagram

https://zh.wikipedia.org/zh-tw/%E4%BA%A4%E4%BA%92%E6%A6%82%E8%BF%B0%E5%9C%96

Class diagram

https://en.wikipedia.org/wiki/Class_diagram

UML class diagram relationships說明:

link:

https://cacoo.com/diagrams/42iti8NqW7vXxEnX/51F5C

Ref:

UML class diagram Relationships:

https://spicyboyd.blogspot.com/2018/07/umlclass-diagram-relationships.html​

UML之類別圖讀書筆記 – 1

參考網站:

https://misomiso43.medium.com/%E7%B0%A1%E5%96%AE%E7%90%86%E8%A7%A3-uml-%E9%A1%9E%E5%88%A5%E5%9C%96-f0b32a3272c

Difference between association and dependency?

https://stackoverflow.com/questions/1230889/difference-between-association-and-dependency

Aggregation v.s. Composition:

https://en.wikipedia.org/wiki/Object_composition#Aggregation

UML類別圖功能:

1. 釐清各類別的描述、擁有的屬性、可使用的方法與各物件間的相互關聯

2.表示實作的Design pattern

描述類別:

– name

– attributes

– methods

表示封裝層級:

+、-、#、~、/、__

public、private、protected、package、derived、static

描述關係:

分為兩部分:

– 類別間的關係:

inheritance、implementation

– 實體間的關係:

aggregation、composition、dependency、association

Detail:

Aggregation & Composition都表達have的關係,但有差別,以下說明:

– composition:

A is a composition of B.

A be killed, B also be killed.

– aggregation:

A has an aggregation of B.

A be killed, B is not affected.

例子:

大學、科系、教授之間的關係

大學有許多科系,大學關閉後,科系也不在了。

(composition)

一個科系有許多教授,科系關閉了,教授還在。

(aggregation)

(以下未完成)

Dependency和Association都表達use的關係:

Association 表示one object has the other object as field/property/attribute

Dependency表示one object accepts another object as a method parameter、return type

Design pattern學習 – 5 (Factory工廠模式)

目的:

提供一個工廠介面,將產生實體的程式碼交由子類別各自實現。

備註:

簡單工廠模式工廠模式比較:

簡單工廠模式的工廠直接負責所有產品的生產工作,利用if else或switch case來判斷生產哪一個產品。

工廠模式下的工廠提升為一個概念,實際上管理產品生產工作的是實作工廠概念的實體工廠。

Swift sample code:

// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
}
class Warrior: Adventurer {
func getType() -> String {
print("I'm a warrior")
return String(describing: Warrior.self)
}
}
// Factory and Concrete Factory
protocol TrainingCamp {
func trainAdventurer() -> Adventurer
}
class ArcherTrainingCamp: TrainingCamp {
func trainAdventurer() -> Adventurer {
print("Train an archer")
return Archer()
}
}
class WarriorTrainingCamp: TrainingCamp {
func trainAdventurer() -> Adventurer {
print("Train an warrior")
return Warrior()
}
}
let archer = ArcherTrainingCamp().trainAdventurer()
let warrior = WarriorTrainingCamp().trainAdventurer()
// Testing code
assert(archer is Archer, "Didn't get the archer object!!")
assert(warrior is Warrior, "Didn't get the warrior object!!")
view raw gistfile1.txt hosted with ❤ by GitHub

Design pattern學習 – 4 (Simple Factory簡單工廠模式)

目的:

定義一個簡單工廠,傳入不同的參數取得不同的類別物件。

備註:

Simple Factory其實違反了SOLID中的open/close principle,因為每次想要擴充可產生的object種類,都要ㄧ再修改Simple Factory中負責產生object的switch case。

Swift sample code:

// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
}
class Warrior: Adventurer {
func getType() -> String {
print("I'm a warrior")
return String(describing: Warrior.self)
}
}
// Simple Factory
class TrainingCamp {
static func trainAdventurer(type: String) -> Adventurer? {
switch type {
case "archer":
return Archer()
case "warrior":
return Warrior()
default:
return nil
}
}
}
let archer = TrainingCamp.trainAdventurer(type: "archer")
let warrior = TrainingCamp.trainAdventurer(type: "warrior")
assert(archer is Archer, "Didn't get the archer object!!")
assert(warrior is Warrior, "Didn't get the warrior object!!")
view raw gistfile1.txt hosted with ❤ by GitHub

Design pattern學習 – 3 (Singleton單例模式)

目的:

保證一個類別只會產生一個物件,而且要提供存取該物件的統一方法

須考慮的情境:

  • lazy initialization
  • Multithread

Swift sample:

// lazy Singleton: create the singleton object when needed
// Note: Global constants and variables are always computed lazily
// ref: https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID263
class SingletonGreed {
static let shared = SingletonGreed()
private init() {}
}
class SingletonThreadSafe {
private static var shared: SingletonThreadSafe?
private init() {}
static func getInstance() -> SingletonThreadSafe {
if shared == nil {
synced(self) {
if shared == nil {
shared = SingletonThreadSafe()
}
}
}
return shared!
}
static func synced(_ lock: Any, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
}
view raw Singleton.swift hosted with ❤ by GitHub

Java sample: