發表文章

目前顯示的是 11月, 2017的文章

[Design-Pattern] Strategy

策略模式 - Strategy Pattern 目的 : 將各種可以互換的演算法(策略)包裝成一個類別 排序策略 父類別 先宣告一個父類別 public enum Color { Black, White, Blue, Gray, Red } public class Car { public readonly int Price; public readonly Color CarColor; public Car(int price, Color color) { this.Price = price; this.CarColor = color; } public override string ToString() { return $"Price : {Price}, Color : {CarColor}"; } } 繼承 Comparer 透過繼承範型的 Comparer ,來實作排序演算法。 public class SortByColor : Comparer<Car> { public override int Compare(Car o1, Car o2) { if (o1 == null || o2 == null) throw new ArgumentNullException(); return (o1.CarColor > o2.CarColor) ? 1 : (o1.CarColor == o2.CarColor) ? 0 : -1; } } public class SortCarByPrice : Comparer<Car> { public ove...

[Design-Pattern] Abstract Factory Pattern

抽象工廠 - Abstract Factory 目的 : 用一個工廠介面來生產一系列相關的產物,但實際建立那些物件由實作工廠的子類別來實現 建立抽象工廠(抽象類別) public enum CarColor { White, Black, Blue, Gray } public enum CarType { Sedan, Coupe, Wagon, Suv, Convertible, HatchBack, Limousine, Van } public abstract class Car { private CarColor _carColor; private CarType _carType; public abstract void Display(); public abstract void SetCarColor(CarColor color); public abstract void SetCarType(CarType type); public CarColor Color { get => this._carColor; set => this._carColor = value; } public CarType CTYPE { set => this._carType = value; get => this._carType; } } 實作抽象工廠 public class Honda : Car { private static string Vendor = "Honda"; public Honda() ...

[Design Pattern] Factory

工廠模式 - Factory 提供一個工廠介面,藉由工廠介面,將產生實體的功能,交由子類別各自實現。 設計一個簡易的汽車工廠介面 設計一個汽車工廠介面,該工廠會設定汽車的品牌名與車子的速度 public interface ICar { string Vendor(); int Speed(); } 產物(類別)繼承工廠,並實作其特性 汽車由工廠介面,來分別實作其特性。 public class Honda : ICar { public string Vendor() { return "Honda"; } public int Speed() { return 100; } } ... public class Toyota : ICar { public string Vendor() { return "Toyota"; } public int Speed() { return 80; } } 設計工廠中心介面 設計工廠中心介面,由該工廠來生產汽車模型。 public interface ICarFactory { ICar CarMake(); } 各產線,各自生產 各產線取得車子的模型後,各自生產自己的車子。 public class CarMadeHonda : ICarFactory { public ICar CarMake() { return new Honda(); } } public class CarMadeToyota : ICarFactory { public ICar Ca...

[vs code] 使用 vs core 建置一個方案

vs core 建置一個方案 剛剛看見一篇文章,在講述如何透過 vs code 去產生一個 VS 專案,覺得還蠻有趣的,趕快筆記一下。 新增 sln 方案檔(Solution) 一開始先建立一個方案目錄 test2,然後進入到 test2 目錄,透過 dontent new 產生方案檔。 $ mkdir test2 $ cd test2 $ dotnet new sln 新增專案 接著新增一個專案,再透過 dotnet new 來產生專案。 $ mkdir mymvc $ cd mymvc $ dotnet new mvc 接著將專案加入方案 先將目錄切回 test2,再透過 dotnet sln add xxxx 將專案加入。 $ cd ../test2 $ dotnet sln add mymvc 建置 mymvc 專案 $ dotnet build 執行 mymvc 專案 $ cd mymvc $ dotnet run 瀏覽執行結果 預設 dotnet core 會執行在 port 5000 上。開啟瀏覽器,打 http://localhost:5000 。 發行專案 將建置的專案,發行到特定的資料(此資料夾的內容,就是我們最後將專案發行到 Azure 上的內容) $ cd mymvc $ dotnet publish -o ../mypublish 執行發行的專案 $ cd mypublish $ dotnet mymvc.dll Reference 使用 .Net Core CLI 和 VS Code 開發 ASP.NET Core 2.0

[Design Pattern] Simple Factory

簡單工廠模式 - Simple Factory 可以根據所傳入的不同參數,取得不同的類別物件。 靜態工廠模式 共同的父類別 自訂介面 Interface public Interface IFactory { string GetType(); } 實作繼承的各產物 public class Honda : IFactory { private readonly string _vendor; public Honda(string vendor) { this._vendor = vendor; } public string GetType() { Console.WriteLine($"Vendor : {this._vendor}"); return _vendor; } } public class Toyota : IFactory { private readonly string _vendor; public Toyota(string vendor) { this._vendor = vendor; } public string GetType() { Console.WriteLine($"Vendor : {this._vendor}"); return _vendor; } } 產物 透過簡單工廠方法,將從工廠自動將產物生產出。 public class CarFactory { public static IFactory carFactory(string type) { switch (type) { case ...

[Design Pattern] Singleton

單例模式 - Singleton 保證一個類別只會產生一個物件,而且要提供存取該物件的統一方法。 Greed Singleton - not thread-safe /* Bad Code, Not use */ public class SingletonGreed { private static SingletonGreed instance = new SingletonGreed(); private SingletonGreed() { Console.WriteLine("I am Greed Singleton!"); } public static SingletonGreed getInstance() { return instance; } } Singleton - thread-safe public class SingletonGreedSafeThread { private static SingletonGreedSafeThread instance = null; private static readonly Object _lock = new Object(); private SingletonGreedSafeThread() { Console.WriteLine("I am Safe Thread Greed Singleton!"); } public static SingletonGreedSafeThread Instance { get { lock (_lock) { if (instance == null) { ...

[Design Pattern] SOLID

SOLID S : Single Reponsiblilty Principle (SRP) 一個類別只負責一件事情 O : Open/Close Principle (OCP) 開放封閉原則。 開放表示可以擴充功能,封閉表示可以修改功能,但兩者皆不能影響原始設計的既有功能。 L : Liskov Substitution Principle (LSP) 在一個系統中,子類別應該可以替換掉父類別而不會影響系統的架構。 I : Interface Segregation Principle (ISP) 把不同的功能從介面中,分離出來。 D : Dependency Inversion Principle (DIP) 高階模組不應依賴低階模組,兩個都應該依賴在抽象概念上;抽象概念不依賴細節,而是讓細節依賴在抽象概念。

[Docker] 簡易的 Docker 筆記

Docker Docker 是一個開源專案,提供虛擬化的技術,使應用程式可以直接執行在該容器上。 安裝 $ apt-get install docker.io 下載 Image 透過 docker 指令,下載我們後須想執行的 OS。另外,也可以只單純下載 app 即可。 $ docker pull ubuntu:14.04 $ docker pull registry.hub.docker.com/ubuntu:14.04 $ docker pull url:port/ubuntu:tag /* 從其它 Repository 下載 image */ 運行 Image 可以在 Docker 的 Container 上執行,下載回來的 Image。 $ docker run -t -i ubuntu /bin/bash Container 可以想成是 Docker 執行 Image 的環境。 一個 Image 可以運行在多個 Container 上面,但是,一個 Container 只會運行一個 Image。 Container 間,彼此是各自獨立的。 檢查目前系統(Host)所有的 Image 在我們將 Image pull 回來時,我們可以透過簡單的指令,知道目前系統上有哪些 Image 可以使用。 $ docker images 搜尋 Images 可以透過指令搜尋目前 Docker 上既有的 Image $ docker search ubuntu 檢查詳細的 Image 資訊 $ docker inspect Name-ID 檢查目前系統所有的 Container $ docker ps -a 共享系統目錄 可以在執行 docker 時,將本機(Host)端目錄分享給運行時的 docker container # Linux $ sudo docker run -i -t -v /home/share:/home/share my:ubuntu "/bin/bash" # Windows $ docker docker run -i -t -v /C/share:/home/share my:ubuntu "/bin/bash...