├── .DS_Store ├── ._.DS_Store ├── Model └── Design Pattern.eap ├── Source Code └── Design_Patterns_Demo │ ├── WebMVC │ ├── Views │ │ ├── _ViewStart.cshtml │ │ ├── Facade │ │ │ ├── Checkout.cshtml │ │ │ ├── AddToCart.cshtml │ │ │ └── Index.cshtml │ │ ├── Proxy │ │ │ ├── Identify.cshtml │ │ │ └── Index.cshtml │ │ ├── Bridge │ │ │ ├── Notify.cshtml │ │ │ └── Index.cshtml │ │ ├── AbstractFactory │ │ │ ├── Process.cshtml │ │ │ ├── Index.cshtml │ │ │ ├── AddWorkItem.cshtml │ │ │ └── ListWorkItems.cshtml │ │ ├── Mediator │ │ │ ├── Index.cshtml │ │ │ ├── StockIn.cshtml │ │ │ └── Order.cshtml │ │ ├── Prototype │ │ │ ├── Index.cshtml │ │ │ ├── RingleFrameworkAdd.cshtml │ │ │ ├── RingleERPCopy.cshtml │ │ │ ├── RingleERP.cshtml │ │ │ └── RingleFramework.cshtml │ │ ├── FactoryMethod │ │ │ ├── Index.cshtml │ │ │ ├── MaintainCustomer.cshtml │ │ │ ├── MaintainSupplier.cshtml │ │ │ └── MaintainProduct.cshtml │ │ ├── Visitor │ │ │ └── Index.cshtml │ │ ├── State │ │ │ └── Index.cshtml │ │ ├── Builder │ │ │ ├── Index.cshtml │ │ │ ├── NewLeave.cshtml │ │ │ └── NewApplyExpense.cshtml │ │ ├── Adapter │ │ │ └── Index.cshtml │ │ ├── Strategy │ │ │ └── Index.cshtml │ │ ├── Singleton │ │ │ └── Index.cshtml │ │ ├── Interpreter │ │ │ └── Index.cshtml │ │ ├── CoR │ │ │ └── Index.cshtml │ │ ├── Home │ │ │ └── Index.cshtml │ │ ├── Composite │ │ │ └── Index.cshtml │ │ ├── Shared │ │ │ └── _Layout.cshtml │ │ ├── web.config │ │ ├── Flyweight │ │ │ └── Index.cshtml │ │ ├── Memento │ │ │ └── Index.cshtml │ │ ├── Decorator │ │ │ └── Index.cshtml │ │ └── TemplateMethod │ │ │ └── Index.cshtml │ ├── Global.asax │ ├── fonts │ │ ├── glyphicons-halflings-regular.eot │ │ ├── glyphicons-halflings-regular.ttf │ │ └── glyphicons-halflings-regular.woff │ ├── Controllers │ │ ├── HomeController.cs │ │ ├── Creational │ │ │ ├── SingletonController.cs │ │ │ ├── BuilderController.cs │ │ │ ├── PrototypeController.cs │ │ │ └── FactoryMethodController.cs │ │ ├── Structural │ │ │ ├── CompositeController.cs │ │ │ ├── FlyweightController.cs │ │ │ ├── AdapterController.cs │ │ │ ├── ProxyController.cs │ │ │ ├── DecoratorController.cs │ │ │ └── BridgeController.cs │ │ └── Behavioral │ │ │ ├── TemplateMethodController.cs │ │ │ ├── InterpreterController.cs │ │ │ ├── VisitorController.cs │ │ │ ├── MediatorController.cs │ │ │ ├── StrategyController.cs │ │ │ ├── StateController.cs │ │ │ ├── CommandController.cs │ │ │ ├── CoRController.cs │ │ │ └── MementoController.cs │ ├── Content │ │ └── Site.css │ ├── Global.asax.cs │ ├── Startup.cs │ ├── App_Start │ │ └── RouteConfig.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Web.Debug.config │ ├── Web.Release.config │ ├── Hubs.cs │ └── packages.config │ ├── Control │ ├── packages.config │ ├── DTO │ │ ├── Structual │ │ │ ├── Bridge │ │ │ │ ├── Enums.cs │ │ │ │ ├── Message.cs │ │ │ │ ├── BridgeDto.cs │ │ │ │ └── Interface.cs │ │ │ ├── Composite │ │ │ │ └── CompositeDto.cs │ │ │ ├── Flyweight │ │ │ │ ├── Interface.cs │ │ │ │ └── FlyweightDto.cs │ │ │ ├── Decorator │ │ │ │ ├── Interface.cs │ │ │ │ └── DecoratorDto.cs │ │ │ ├── Adapter │ │ │ │ └── 烏龜商品.cs │ │ │ └── Facade │ │ │ │ └── 烏龜商品.cs │ │ ├── Creational │ │ │ ├── Prototype │ │ │ │ ├── RingleERP │ │ │ │ │ ├── Interface.cs │ │ │ │ │ └── PrototypeDto.cs │ │ │ │ └── RingleFramework │ │ │ │ │ └── Interface.cs │ │ │ ├── AbstractFactory │ │ │ │ ├── ParticipantFactoryEnum.cs │ │ │ │ └── WorkItemDTO.cs │ │ │ ├── Builder │ │ │ │ ├── Interface.cs │ │ │ │ └── BuilderDto.cs │ │ │ ├── Singleton │ │ │ │ └── SingletonDto.cs │ │ │ └── FactoryMethod │ │ │ │ └── FactoryMethodDto.cs │ │ └── Behavioral │ │ │ ├── Memento │ │ │ └── 複合彩DTO.cs │ │ │ ├── CoR │ │ │ ├── DonateType.cs │ │ │ ├── Donation.cs │ │ │ └── CustomExceptions.cs │ │ │ ├── Strategy │ │ │ └── StrategyEnum.cs │ │ │ ├── Mediator │ │ │ └── MediatorDto.cs │ │ │ ├── TemplateMethod │ │ │ └── TemplateMethodDto.cs │ │ │ └── Interpreter │ │ │ └── InterpreterDto.cs │ ├── Behavioral │ │ ├── Mediator │ │ │ ├── Interface.cs │ │ │ ├── StockingUco.cs │ │ │ └── Singleton.cs │ │ ├── Observer │ │ │ ├── IObserver.cs │ │ │ ├── IPublisher.cs │ │ │ ├── ConcreteObservers.cs │ │ │ ├── ManageNewsControl.cs │ │ │ └── NewsPublisher.cs │ │ ├── Strategy │ │ │ ├── IStrategy.cs │ │ │ ├── StrategyContext.cs │ │ │ ├── RSI指標Strategy.cs │ │ │ ├── 黃金交叉Strategy.cs │ │ │ └── PlaceOrderControl.cs │ │ ├── Visitor │ │ │ ├── IElement.cs │ │ │ ├── Visitor.cs │ │ │ ├── ConcreteVisitors.cs │ │ │ └── ConcreteElements.cs │ │ ├── State │ │ │ ├── 2_Refactoring │ │ │ │ ├── IOrderState.cs │ │ │ │ ├── OrderStateContext.cs │ │ │ │ └── ConcreteStateClass.cs │ │ │ └── TrackOrderControl.cs │ │ ├── Memento │ │ │ ├── 複合彩Memento.cs │ │ │ ├── 複合彩Caretaker.cs │ │ │ ├── 複合彩.cs │ │ │ └── 管理博彩Control.cs │ │ ├── CoR │ │ │ ├── DonateHandler.cs │ │ │ └── procDonateControl.cs │ │ ├── TemplateMethod │ │ │ ├── PolicyTransactionUco.cs │ │ │ ├── InsurancePolicyTransationTemplateMethodPattern.cs │ │ │ └── Singleton.cs │ │ └── Command │ │ │ └── CaculatorCommand.cs │ ├── Structural │ │ ├── Flyweight │ │ │ ├── Dao │ │ │ │ ├── CodeModel.cs │ │ │ │ ├── CodeModel.Designer.cs │ │ │ │ ├── CodeModel.edmx.diagram │ │ │ │ ├── CodeModel.Context.cs │ │ │ │ ├── Code.cs │ │ │ │ └── CodeDao.cs │ │ │ └── FlyweightPattern.cs │ │ ├── Adapter │ │ │ ├── IGet存貨資訊.cs │ │ │ ├── 查詢商品Control.cs │ │ │ ├── 供應商系統 │ │ │ │ └── OrderSystem.cs │ │ │ └── Get存貨Adapter.cs │ │ ├── Bridge │ │ │ ├── Notifier.cs │ │ │ ├── UserNotifier.cs │ │ │ ├── EmailSender.cs │ │ │ ├── LineSender.cs │ │ │ ├── SystemNotifier.cs │ │ │ ├── IMessageSender.cs │ │ │ └── 傳送訊息DemoControl.cs │ │ ├── Proxy │ │ │ ├── I龜庫接口.cs │ │ │ ├── 龜庫Informer.cs │ │ │ ├── 販售服務Control.cs │ │ │ └── 龜庫Proxy.cs │ │ ├── Facade │ │ │ ├── 烏龜BMO.cs │ │ │ └── 買烏龜Control.cs │ │ └── Decorator │ │ │ └── ConcreteComponent.cs │ ├── Utility │ │ ├── ObjectCreator.cs │ │ └── EnumUtils.cs │ ├── Creational │ │ ├── AbstractoryFactory │ │ │ ├── WorkItem.cs │ │ │ ├── AbstractFactory.cs │ │ │ ├── Participant.cs │ │ │ ├── LDAPParticipant.cs │ │ │ ├── LocalDBParticipant.cs │ │ │ └── ConcreteFactory.cs │ │ ├── Builder │ │ │ ├── SaveControl.cs │ │ │ └── EOfficeBuilderPattern.cs │ │ ├── Singleton │ │ │ └── IDGeneratorSingleton.cs │ │ └── Prototype │ │ │ ├── RingleFramework │ │ │ └── MaintainPurchaseConrol.cs │ │ │ └── RingleERP │ │ │ └── MaintainPurchaseOrderControl.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── App.Config │ ├── ConsoleApplication │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ConsoleApplication.csproj │ └── Observer │ │ └── ObserverConsole.cs │ └── Design Patterns Demo Solution.sln ├── README.md └── .gitattributes /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/.DS_Store -------------------------------------------------------------------------------- /._.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/._.DS_Store -------------------------------------------------------------------------------- /Model/Design Pattern.eap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/Model/Design Pattern.eap -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "~/Views/Shared/_Layout.cshtml"; 3 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Global.asax: -------------------------------------------------------------------------------- 1 | <%@ Application Codebehind="Global.asax.cs" Inherits="WebMVC.MvcApplication" Language="C#" %> 2 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Facade/Checkout.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | ViewBag.Title = "Checkout"; 3 | } 4 | 5 |

訂購結果

6 |

@Html.Encode(ViewData["result"])

-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Proxy/Identify.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Identify"; 4 | } 5 | 6 |

認證查詢結果

7 |

@Html.Encode(ViewData["IdentifyResult"])

8 | 9 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ringlelai/HSDcDesingPatternCourse/HEAD/Source Code/Design_Patterns_Demo/WebMVC/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Bridge/Notify.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Notify"; 4 | } 5 | 6 |

處理結果

7 |

@MvcHtmlString.Create(Html.Encode(ViewData["result"]).Replace("\n", "
"))

8 | 9 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/AbstractFactory/Process.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Process"; 4 | } 5 | 6 |

處理結果

7 |

@MvcHtmlString.Create(Html.Encode(ViewData["result"]).Replace("\n", "
"))

8 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Mediator/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "選擇線上交易功能"; 4 | } 5 | 6 |

選擇線上交易功能

7 | 8 |

線上訂購

9 |

進貨系統

10 | 11 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Prototype/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Prototype範例"; 4 | } 5 | 6 |

Prototype範例

7 | 8 | RingleFramework範例 9 |
10 |
11 | RingleERP範例 -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Bridge/Enums.cs: -------------------------------------------------------------------------------- 1 | namespace hsdc.dpt.Control.DTO.Structual.Bridge 2 | { 3 | // Notifier 的類型 4 | public enum NotifyType 5 | { User, System } 6 | 7 | // MessageSender 的類型 8 | public enum SenderType 9 | { Email, Line} 10 | } 11 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/FactoryMethod/Index.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | ViewBag.Title = "選擇維護功能"; 3 | } 4 | 5 |

選擇維護功能

6 | 7 |

維護客戶資料

8 |

維護廠商資料

9 |

維護產品資料

10 | 11 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Mediator/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Mediator.Intf 8 | { 9 | public interface NotifierProxy 10 | { 11 | void Notify(string prodName); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Composite/CompositeDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Composite 8 | { 9 | public class Employee 10 | { 11 | public string Name { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Prototype/RingleERP/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Prototype.RingleERP.Intf 8 | { 9 | public interface PorotoType 10 | { 11 | T Clone(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Flyweight/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Flyweight.Intf 8 | { 9 | public interface Code 10 | { 11 | List GetCodeList(string parentValue = ""); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/CodeModel.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 這個程式碼是由範本產生。 4 | // 5 | // 對這個檔案進行手動變更可能導致您的應用程式產生未預期的行為。 6 | // 如果重新產生程式碼,將會覆寫對這個檔案的手動變更。 7 | // 8 | //------------------------------------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/AbstractFactory/ParticipantFactoryEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.AbstractFactory 8 | { 9 | public enum ParticipantFactoryEnum 10 | { 11 | LDAP, 12 | LocalDB 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/Memento/複合彩DTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.Memento 8 | { 9 | public class 複合彩DTO 10 | { 11 | public string 彩券號碼 { set; get; } 12 | public string 成語字謎 { set; get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Adapter/IGet存貨資訊.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO.Structual.Adapter; 8 | 9 | namespace hsdc.dpt.Control.Structural.Adapter 10 | { 11 | public interface IGet存貨資訊 12 | { 13 | List<烏龜商品> get存貨清單(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/CoR/DonateType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.CoR 8 | { 9 | // 捐款者類型 10 | public enum DonateType 11 | { 12 | 個人, 13 | 營利事業, 14 | 人民團體, 15 | UNKNOWN 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Visitor/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Visitor Pattern

7 |
8 | @Html.ActionLink("計算本月公司應支付金額", "CalcTotalPrice") 9 |     10 | @Html.ActionLink("統計員工與辦公室總數", "CalcInventory") 11 |

12 |
13 | @MvcHtmlString.Create(Html.Encode(ViewData["ProcssResult"]).Replace("\n", "
")) 14 |
-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/Strategy/StrategyEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.Strategy 8 | { 9 | public enum StrategyType 10 | { 11 | GoldenCross, // 黃金交叉買賣策略 12 | RSI // RSI 指標買賣策略 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Flyweight/FlyweightDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Flyweight 8 | { 9 | public class CodeDto 10 | { 11 | public string CodeValue { get; set; } 12 | public string CodeText { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/HomeController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | 7 | namespace WebMVC.Controllers 8 | { 9 | public class HomeController : Controller 10 | { 11 | // GET: Home 12 | public ActionResult Index() 13 | { 14 | return View(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Builder/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Builder.Intf 8 | { 9 | public interface OfficeFormBuilder 10 | { 11 | void BuildMaster(); 12 | void BuildDetails(); 13 | T GetResult(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Content/Site.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding-top: 50px; 3 | padding-bottom: 20px; 4 | } 5 | 6 | /* Set padding to keep content from hitting the edges */ 7 | .body-content { 8 | padding-left: 15px; 9 | padding-right: 15px; 10 | } 11 | 12 | /* Set width on the form input elements since they're 100% wide by default */ 13 | input, 14 | select, 15 | textarea { 16 | max-width: 280px; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Decorator/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Decorator.intf 8 | { 9 | public interface UserService 10 | { 11 | User Login(string userId, string password); 12 | List GetAclList(string userId); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Observer/IObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Observer 8 | { 9 | /// 10 | /// The 'Observer' interface. 11 | /// 12 | public interface IObserver 13 | { 14 | // 定義自我更新的操作 (operation) 15 | void Update(string news); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Strategy/IStrategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Strategy 8 | { 9 | /// 10 | /// The 'Strategy' Interface 11 | /// 制訂交易策略的共同介面 12 | /// 13 | public interface IStrategy 14 | { 15 | // 執行策略的操作 16 | string execute(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Visitor/IElement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Visitor 8 | { 9 | /// 10 | /// The 'Element' interface. 11 | /// 12 | public interface IElement 13 | { 14 | // 同意讓 Visitor 進入,以進行後續的操作 15 | void Accept(Visitor visitor); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Adapter/烏龜商品.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Adapter 8 | { 9 | public class 烏龜商品 10 | { 11 | public string id { get; set; } 12 | public string name { get; set; } 13 | public int price { get; set; } 14 | public int quantity { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/CodeModel.Designer.cs: -------------------------------------------------------------------------------- 1 | // 已啟用模型 'C:\Users\admin\Documents\Visual Studio 2013\Projects\Design_Patterns_Demo\Control\Structural\Flyweight\Dao\CodeModel.edmx' 的 T4 程式碼產生。 2 | // 若要啟用舊版程式碼產生,請將 [程式碼產生策略] 設計工具屬性的值 3 | //變更為 [舊版 ObjectContext]。當模型在設計工具中開啟時,這個屬性便可 4 | //以在 [屬性] 視窗中使用。 5 | 6 | // 如果尚未產生任何內容和實體類型,可能是因為您建立了空的模型,但 7 | //尚未選擇要使用的 Entity Framework 版本。若要為您的模型產生內容類別和 8 | //實體類型,請在設計工具中開啟模型,以滑鼠右鍵按一下設計工具介面並 9 | //選取 [從資料庫更新模型]、[由模型產生資料庫] 或 [加入程式碼產生 10 | //項目]。 -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/State/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

State Pattern

7 |

追蹤訂單狀態-Demo

8 |
9 |
10 | 11 | 12 |
13 |
14 |

訂單處理結果

15 |
16 | @MvcHtmlString.Create(Html.Encode(ViewData["OrderState"]).Replace("\n", "
")) 17 |
-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Global.asax.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using System.Web.Routing; 7 | 8 | namespace WebMVC 9 | { 10 | public class MvcApplication : System.Web.HttpApplication 11 | { 12 | protected void Application_Start() 13 | { 14 | AreaRegistration.RegisterAllAreas(); 15 | RouteConfig.RegisterRoutes(RouteTable.Routes); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Owin; 2 | using Owin; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Web; 7 | 8 | [assembly: OwinStartup(typeof(WebMVC.Hubs.Startup))] 9 | namespace WebMVC.Hubs 10 | { 11 | public class Startup 12 | { 13 | public void Configuration(IAppBuilder app) 14 | { 15 | // Any connection or hub wire up and configuration should go here 16 | app.MapSignalR(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Bridge/Message.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Bridge 8 | { 9 | public class Message 10 | { 11 | public string ID { get; set; } 12 | public string Recipient { get; set; } // 收件人 13 | public string Subject { get; set; } // 主旨 14 | public string Body { get; set; } // 內容 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Visitor/Visitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Visitor 8 | { 9 | /// 10 | /// The 'Visitor' abstract class. 11 | /// 12 | public abstract class Visitor 13 | { 14 | // 需要針對每一個物件結構內的元素,定義相對應的操作 15 | abstract public void VisitEmployee(Employee emp); 16 | abstract public void VisitOffice(Office office); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Builder/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "辦公室自動化系統"; 4 | } 5 | 6 |

辦公室自動化系統

7 | 8 | 9 |
10 |
11 | 12 | 13 | @section BodyScripts 14 | { 15 | 23 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/CoR/Donation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.CoR 8 | { 9 | // 政治獻金 Data Type 10 | public class Donation 11 | { 12 | public string name { get; set; } 13 | public DonateType type { get; set; } // 個人;營利事業;人民團體;UNKNOWN 14 | public int money { get; set; } // 捐款金額 15 | public string ext_info { get; set; } // 關於捐款相關事宜的備註/摘要 16 | } 17 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Utility/ObjectCreator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Utility 9 | { 10 | internal class ObjectCreator 11 | { 12 | internal static T CreateObject(string className) 13 | { 14 | string assemblyName = Assembly.GetAssembly(typeof(ObjectCreator)).GetName().Name; 15 | return (T)Activator.CreateInstance(assemblyName, className).Unwrap(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Bridge/BridgeDto.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Bridge.Intf; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.DTO.Structual.Bridge 9 | { 10 | public class Order 11 | { 12 | public string OrderID { get; set; } 13 | public string ProductName { get; set; } 14 | public OrderStatus OrderStatus { get; set; } 15 | public string Shipper { get; set; } 16 | public string ShippingID { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/WorkItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Creational.AbstractFactory; 7 | 8 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 9 | { 10 | /// 11 | /// The 'Client' class 12 | /// 工作項目 13 | /// 14 | public class WorkItem 15 | { 16 | public string ID { get; set; } 17 | public string item_title { get; set; } 18 | public Participant assignee { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Observer/IPublisher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Observer 8 | { 9 | /// 10 | /// The 'Subject' interface 11 | /// 12 | public interface IPublisher 13 | { 14 | void Subscribe(IObserver observer); // The 'Attch()' operation 15 | void UnSubscribe(IObserver observer); // The 'Detach()' operation 16 | void Notify(); // call the observer to update itself 17 | } 18 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Prototype/RingleFrameworkAdd.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "新增採購單"; 4 | } 5 | 6 |

新增採購單

7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 |
客戶名稱
15 |
16 |
17 | 18 |
19 | 20 | @section BodyScripts 21 | { 22 | 27 | } 28 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/AbstractFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 8 | { 9 | /// 10 | /// The 'Abstract Factory' class 11 | /// 參與者抽象工廠 12 | /// 13 | public abstract class ParticipantFactory 14 | { 15 | public abstract OrganizationParticipant CreateOrganizationParticipant(); 16 | public abstract RoleParticipant CreateRoleParticipant(); 17 | public abstract HumanParticipant CreateHumanParticipant(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Adapter/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Adapter Pattern

7 | 8 |

列出可訂購的烏龜清單

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | @foreach (var item in Model) 19 | { 20 | 21 | 22 | 23 | 24 | 25 | 26 | } 27 |
編號龜名價錢可訂購數量
@item.id@item.name@item.price@item.quantity
28 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/State/2_Refactoring/IOrderState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.State 8 | { 9 | /// 10 | /// The 'State' Interface 11 | /// 定義 Context 內的 function,在某特定狀態下所需處理的行為 12 | /// 13 | public interface IOrderState 14 | { 15 | /// 16 | /// 定義訂單在各狀態的處理行為 17 | /// 18 | /// 19 | /// 20 | string processSomething(OrderStateContext context); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/AbstractFactory/WorkItemDTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.AbstractFactory 8 | { 9 | public enum AssigneeType 10 | { 11 | ORGANIZATION, 12 | ROLE, 13 | HUMAN 14 | } 15 | public class WorkItemDTO 16 | { 17 | public string ID { get; set; } 18 | public string item_title { get; set; } // 項目名稱 19 | public AssigneeType assign_type { get; set; } // 指派者類型 20 | public string assignee_name { get; set; } // 指派者名稱 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/AbstractFactory/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Abstract Factory Pattern

7 |

選擇目錄服務

8 |
9 |
10 | 11 |
15 |
16 |
17 |
18 | 19 |
20 |
-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Facade/烏龜商品.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO 8 | { 9 | // 購物車 10 | public class ShoppingCart 11 | { 12 | public List<烏龜商品> CartList { get; set; } 13 | public string eCoupon { get; set; } // 折扣代碼 14 | public int TotalPrice { get; set; } // 總價 15 | } 16 | public class 烏龜商品 17 | { 18 | public string id { get; set; } 19 | public string name { get; set; } 20 | public int price { get; set; } 21 | public int quantity { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/App_Start/RouteConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using System.Web.Routing; 7 | 8 | namespace WebMVC 9 | { 10 | public class RouteConfig 11 | { 12 | public static void RegisterRoutes(RouteCollection routes) 13 | { 14 | routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 15 | 16 | routes.MapRoute( 17 | name: "Default", 18 | url: "{controller}/{action}/{id}", 19 | defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 20 | ); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/Notifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | /// 11 | /// The 'Abstraction' class 12 | /// 負責處理通知訊息工作的抽象類別 13 | /// 14 | public abstract class Notifier 15 | { 16 | // 將 send(message) 的實作交由實作 Message Sender 介面的具體物件來負責 17 | // Notifier 不負責 send() 的實作 18 | public IMessageSender sender { get; set; } 19 | 20 | // 定義為抽象方法,交由子類別來實作 21 | public abstract string Notify(Message message); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/CoR/CustomExceptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.CoR 8 | { 9 | // User-defined Exception : 政治獻金超出法定限額 10 | public class DonateExceedException : Exception 11 | { 12 | public DonateExceedException():base() { } 13 | public DonateExceedException (string message): base(message) { } 14 | } 15 | 16 | public class NullHanderException : NullReferenceException 17 | { 18 | public NullHanderException():base() { } 19 | public NullHanderException (string message): base(message) { } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Memento/複合彩Memento.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Memento 8 | { 9 | /// 10 | /// The 'Memento' Class. 11 | /// 12 | public class 複合彩Memento 13 | { 14 | // 實務上是不應該使用 public 操作來存取這些屬性 (properties) 15 | // 這些屬性資訊,還是應該透過公開的操作介面來取得,如此才得以保護 Memento 物件的封裝性 16 | public int 彩券號碼 { get; set; } 17 | public string 字彩 { get; set; } 18 | 19 | public 複合彩Memento(int _彩號, string _字彩) 20 | { 21 | this.彩券號碼 = _彩號; 22 | this.字彩 = _字彩; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Proxy/I龜庫接口.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Structural.Proxy 8 | { 9 | /// 10 | /// The 'Subject' interface 11 | /// 提供所出售烏龜相關服務的介面定義 12 | /// 龜庫 = Tortoise Library Information Provider 13 | /// 14 | public interface I龜庫接口 15 | { 16 | /// 17 | /// 提供烏龜認證的資訊 18 | /// 19 | /// 賣出烏龜的品種 20 | /// /// 賣出烏龜的序號 21 | /// 認證資訊 22 | string Get認證資訊(string 品種, string 序號); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Proxy/龜庫Informer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Structural.Proxy 8 | { 9 | /// 10 | /// The 'RealSubject' class 11 | /// 至總部系統取得遠端資料庫的認證資訊 12 | /// 13 | public class 龜庫Informer : I龜庫接口 14 | { 15 | // 需了解遠端的通訊協定 (如 SOAP, RESTFUL)、介面規格 (參數、回傳值)等相關連線細節 16 | public string Get認證資訊(string m品種, string m序號) 17 | { 18 | // TODO: 需實作連線至遠端系統 19 | string queryResult = "序號:" + m序號 + " 的認證資訊從" + "「總部遠端系統」取得認證結果。"; 20 | 21 | return queryResult; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Creational/SingletonController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Creational.Singleton; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Web; 6 | using System.Web.Mvc; 7 | 8 | namespace WebMVC.Controllers.Creational 9 | { 10 | public class SingletonController : Controller 11 | { 12 | // GET: Singleton 13 | public ActionResult Index() 14 | { 15 | return View(); 16 | } 17 | 18 | public JsonResult GenID(string formType) 19 | { 20 | string id = IDGeneratorSingleton.Instance.GetNextID(formType, DateTime.Today); 21 | return Json(id, JsonRequestBehavior.AllowGet); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Strategy/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Strategy Pattern

7 |
8 |
9 | 10 | 14 |
15 |
16 |
17 |
18 |
19 |

策略執行結果:

20 |
21 | @MvcHtmlString.Create(Html.Encode(ViewData["ProcssResult"]).Replace("\n", "
")) 22 |
23 | 24 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Prototype/RingleERPCopy.cshtml: -------------------------------------------------------------------------------- 1 | @model hsdc.dpt.Control.DTO.Creational.Prototype.RingleERP.InternalDto.PurchaseOrderDto 2 | 3 | @{ 4 | ViewBag.Title = "新增採購單"; 5 | } 6 | 7 |

新增採購單

8 | 9 | @using (Html.BeginForm("REAddPo", "Prototype", FormMethod.Post, new { id = "frmSave" })) 10 | { 11 | 12 | 13 | 14 | 15 | 16 |
客戶名稱@Html.TextBoxFor(m => m.CustomerName)
17 |
18 |
19 | 20 | } 21 | 22 | @section BodyScripts 23 | { 24 | 29 | } 30 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Proxy/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Proxy Pattern

7 |

烏龜認證資訊查詢表單

8 |
9 |
10 | 11 | 16 |
17 |
18 |
19 | 20 | @Html.TextBox("txtSerialNo") 21 |
22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/CoR/DonateHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Behavioral.CoR; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.CoR 9 | { 10 | /// 11 | /// The 'Handler' abstract class. 12 | /// 13 | public abstract class DonateHandler 14 | { 15 | public abstract string handleDonate(Donation donate); // abstract method, 關於捐贈的邏輯處理,由 extend 的 Handler 實現 16 | public DonateHandler Successor { get; set; } // 指向下一個 Handler 17 | public void SetHandlers(DonateHandler _successor) 18 | { 19 | 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/Mediator/MediatorDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Behavioral.Mediator 8 | { 9 | public class Product 10 | { 11 | public string ProductName { get; set; } 12 | } 13 | 14 | public class Stocking 15 | { 16 | public Product Product { get; set; } 17 | public int Quantity { get; set; } 18 | } 19 | 20 | public class Order 21 | { 22 | public Product Product { get; set; } 23 | } 24 | 25 | public class StockIn 26 | { 27 | public Product Product { get; set; } 28 | public int Quantity { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/TemplateMethod/PolicyTransactionUco.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.TemplateMethod.InsurancePolicy; 2 | using hsdc.dpt.Control.DTO.Behavioral.TemplateMethod; 3 | using hsdc.dpt.Control.Utility; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace hsdc.dpt.Control.Behavioral.TemplateMethod 11 | { 12 | public class PolicyTransactionUco 13 | { 14 | public List GetTransactionList(string className) 15 | { 16 | AbstractPolicyChangeTemplate policyChange = ObjectCreator.CreateObject(className); 17 | return policyChange.GetTransactionList(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Memento/複合彩Caretaker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Memento 8 | { 9 | /// 10 | /// The 'Caretaker' Class. 11 | /// 12 | public class 複合彩Caretaker 13 | { 14 | private 複合彩Memento memento; 15 | 16 | // 將 originator 的狀態儲存至 memento 物件內 17 | public void SaveState(複合彩 originator) 18 | { 19 | this.memento = originator.CreateMemento(); 20 | } 21 | 22 | // 從 memento 物件內還原狀態回 originator 物件 23 | public void RestoreState(複合彩 originator) 24 | { 25 | originator.SetMemento(memento); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/CodeModel.edmx.diagram: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Proxy/販售服務Control.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Structural.Proxy 8 | { 9 | /// 10 | /// The 'Client' class 11 | /// 提供烏龜販售服務 Controller 12 | /// 13 | public class 販售服務Control 14 | { 15 | // 取得來自 Form Controller 所輸入欲查詢烏龜認證的參數 16 | public string get認證資訊(string m品種, string m序號) 17 | { 18 | // new a proxy class which realize the subject interface 19 | I龜庫接口 subject = new 龜庫Proxy(); 20 | 21 | // via the proxy object to get the result. 22 | string 認證結果 = subject.Get認證資訊(m品種, m序號); 23 | 24 | return 認證結果; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/UserNotifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | public class UserNotifier : Notifier 11 | { 12 | /// 13 | /// 實現 Notify 處理的程序 14 | /// 15 | /// Message 物件 16 | /// 處理結果 17 | public override string Notify(Message message) 18 | { 19 | string procResult; // 處理結果 20 | 21 | // 1. 將訊息傳送給所指定的收件人 22 | procResult = sender.SendMessage(message); 23 | 24 | return procResult; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/Builder/SaveControl.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Creational.Builder; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Creational.Builder 9 | { 10 | public class SaveLeaveControl 11 | { 12 | public Leave Save(Leave leave) 13 | { 14 | if (leave != null) 15 | leave.LeaveID = "LE010001"; 16 | return leave; 17 | } 18 | } 19 | 20 | public class SaveApplyExpenseControl 21 | { 22 | public ApplyExpense Save(ApplyExpense expense) 23 | { 24 | if (expense != null) 25 | expense.ApplyExpenseID = "AE010001"; 26 | return expense; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/CompositeController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Structural.Composite; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Web; 6 | using System.Web.Mvc; 7 | 8 | namespace WebMVC.Controllers.Structural 9 | { 10 | public class CompositeController : Controller 11 | { 12 | // GET: Composite 13 | public ActionResult Index() 14 | { 15 | RingelOrgControl uco = new RingelOrgControl(); 16 | return View(uco.GetAllDepartmentName()); 17 | } 18 | 19 | public JsonResult QueryEmp(string deptName) 20 | { 21 | RingelOrgControl uco = new RingelOrgControl(); 22 | return Json(uco.GetAllEmployeeName(deptName), JsonRequestBehavior.AllowGet); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/TemplateMethodController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.TemplateMethod; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Configuration; 5 | using System.Linq; 6 | using System.Web; 7 | using System.Web.Mvc; 8 | 9 | namespace WebMVC.Controllers.Behavioural 10 | { 11 | public class TemplateMethodController : Controller 12 | { 13 | // GET: TemplateMethod 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | public JsonResult QueryPolicyList(string type) 20 | { 21 | PolicyTransactionUco uco = new PolicyTransactionUco(); 22 | return Json(uco.GetTransactionList(ConfigurationManager.AppSettings[type]), JsonRequestBehavior.AllowGet); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Mediator/StockingUco.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.Mediator.Singleton; 2 | using hsdc.dpt.Control.DTO.Behavioral.Mediator; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Behavioral.Mediator 10 | { 11 | public class StockingUco 12 | { 13 | 14 | public List GetAllProducts() 15 | { 16 | return Db.Instance.GetStocking().Select(d => new Product() { ProductName = d.Product.ProductName }).ToList(); 17 | } 18 | 19 | public bool CheckQunatity(string prodName) 20 | { 21 | return Db.Instance.GetStocking().Where(s => s.Product.ProductName == prodName).SingleOrDefault().Quantity == 0; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/InterpreterController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.Interpreter.Money; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Web; 6 | using System.Web.Mvc; 7 | 8 | namespace WebMVC.Controllers.Behavioural 9 | { 10 | public class InterpreterController : Controller 11 | { 12 | // GET: Interpreter 13 | public ActionResult Index() 14 | { 15 | return View(); 16 | } 17 | 18 | public JsonResult TransferToChinese(long number) 19 | { 20 | MoneyContext context = new MoneyContext(number); 21 | MoneyController uco = new MoneyController(); 22 | uco.TransferToChinese(ref context); 23 | return Json(context.ToChinese(), JsonRequestBehavior.AllowGet); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Singleton/SingletonDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Singleton 8 | { 9 | public class Form 10 | { 11 | public string FormType { get; set; } 12 | public string FormDateStr { get; set; } 13 | public override bool Equals(object obj) 14 | { 15 | if (obj.GetType() != this.GetType()) 16 | return false; 17 | return ((Form)obj).FormType.Equals(this.FormType) && 18 | ((Form)obj).FormDateStr.Equals(this.FormDateStr); 19 | } 20 | public override int GetHashCode() 21 | { 22 | return FormType.GetHashCode() ^ FormDateStr.GetHashCode(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/FlyweightController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Flyweight; 2 | using hsdc.dpt.Control.Structural.Flyweight; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Web; 7 | using System.Web.Mvc; 8 | 9 | namespace WebMVC.Controllers.Structural 10 | { 11 | public class FlyweightController : Controller 12 | { 13 | public ActionResult Index() 14 | { 15 | List cityCodes = CodeFactory.GetInstance().GetCode("CITY").GetCodeList(); 16 | return View(cityCodes); 17 | } 18 | 19 | public JsonResult QueryArea(string city) 20 | { 21 | List cityCodes = CodeFactory.GetInstance().GetCode("AREA").GetCodeList(city); 22 | return Json(cityCodes, JsonRequestBehavior.AllowGet); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/AdapterController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | 7 | using hsdc.dpt.Control.Structural.Adapter; 8 | using hsdc.dpt.Control.DTO.Structual.Adapter; 9 | 10 | namespace WebMVC.Controllers.Structural 11 | { 12 | public class AdapterController : Controller 13 | { 14 | // GET: Adapter 15 | public ActionResult Index() 16 | { 17 | // new a control object. 18 | 查詢商品Control control = new 查詢商品Control(); 19 | 20 | // new a List object. 21 | List<烏龜商品> prodList = new List<烏龜商品>(); 22 | 23 | // 取得可訂購的烏龜清單 24 | prodList = control.get可訂購烏龜清單(); 25 | 26 | //return View(DTO); 27 | return View(prodList); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Strategy/StrategyContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Strategy 8 | { 9 | /// 10 | /// The 'Context' class 11 | /// 交易策略處理的主體 12 | /// 13 | public class StrategyContext 14 | { 15 | // Hold a reference to a sub-clss implement IStrategy interface 16 | private IStrategy select_strategy; 17 | 18 | // 交易策略執行的操作 (每次有 Tick 資料更新時,即會呼叫該 function) 19 | public string UpdateOnTick() 20 | { 21 | return select_strategy.execute(); 22 | } 23 | 24 | // 設定所選擇的交易策略,將參考變數指向實作該策略 Strategy 的子類別 25 | public void SetStrategy(IStrategy strategy) 26 | { 27 | this.select_strategy = strategy; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Adapter/查詢商品Control.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO.Structual.Adapter; 8 | 9 | namespace hsdc.dpt.Control.Structural.Adapter 10 | { 11 | public class 查詢商品Control 12 | { 13 | // Constructor 14 | public 查詢商品Control() 15 | { 16 | } 17 | 18 | public List<烏龜商品> get可訂購烏龜清單() 19 | { 20 | IGet存貨資訊 adapter = this.getAdapter(); 21 | 22 | return adapter.get存貨清單(); 23 | } 24 | 25 | // 取得實現 IGet存貨資訊 介面的物件。 26 | // TODO: 實務上會透過如 Factory 工廠類別產出實作介面的 Adatper 物件並傳回 27 | // 附帶說明:ADO.NET 的資料庫連線物件即為以此方式取得 28 | private IGet存貨資訊 getAdapter() 29 | { 30 | return new Get存貨Adapter(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Prototype/RingleFramework/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Prototype.RingleFramework.Intf 8 | { 9 | public interface PurchaseOrder 10 | { 11 | String PoNumber { get; set; } 12 | String CustomerName { get; set; } 13 | List LineItems { get; } 14 | void AddLineItem(PurchaseOrderLineItem item); 15 | decimal Total { get; } 16 | } 17 | 18 | public interface PurchaseOrderLineItem 19 | { 20 | String ProductName { get; set; } 21 | int Quantity { get; set; } 22 | decimal Price { get; set; } 23 | decimal SubTotal { get; } 24 | } 25 | 26 | public interface Prototype 27 | { 28 | T Clone(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/ProxyController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Structural.Proxy; 7 | 8 | namespace WebMVC.Controllers.Structural 9 | { 10 | public class ProxyController : Controller 11 | { 12 | // GET: Proxy 13 | public ActionResult Index() 14 | { 15 | return View(); 16 | } 17 | 18 | public ActionResult Identify(string selTurtleType, string txtSerialNo) 19 | { 20 | ViewData["result1"] = selTurtleType; 21 | ViewData["result2"] = txtSerialNo; 22 | 販售服務Control control = new 販售服務Control(); 23 | 24 | string 認證結果 = control.get認證資訊(selTurtleType, txtSerialNo); 25 | ViewData["IdentifyResult"] = 認證結果; 26 | 27 | return View(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/AbstractFactory/AddWorkItem.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "AddWorkItem"; 4 | } 5 | 6 |

新增工作單

7 |
8 |
9 | 10 | @Html.TextBox("txtItemTitle")
11 | 12 |
17 | 18 | @Html.TextBox("txtAssignee") 19 |
20 |
21 |
22 | 23 |
24 |
25 |
26 |

27 |

@Html.ActionLink("列出所有工作單", "ListWorkItems")

28 |
29 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Observer/ConcreteObservers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Observer 8 | { 9 | /// 10 | /// The 'ConcreteObserver' class. 11 | /// ActiveMonitor 代表畫面展示 (Console/Windows Form/Web) 12 | /// 13 | public class ActiveMonitor : IObserver 14 | { 15 | public void Update(string news) 16 | { 17 | Console.WriteLine("即時監視器 - 新聞快訊:" + news); 18 | } 19 | } 20 | 21 | /// 22 | /// The 'ConcreteObserver' class. 23 | /// Reporter 代表接收的新聞記者 24 | /// 25 | public class Reporter : IObserver 26 | { 27 | public void Update(string news) 28 | { 29 | /// 實作上,系統需主動將新聞訊息 Push (推播) 至 所訂閱記者的行動裝置內 (如 Line) 30 | Console.WriteLine("記者接收 - 新聞快訊 - :" + news); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Decorator/DecoratorDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Structual.Decorator 8 | { 9 | public class User 10 | { 11 | public string UserID { get; set; } 12 | public string UserName { get; set; } 13 | } 14 | 15 | public class Function 16 | { 17 | public string FunctionName { get; set; } 18 | public string FunctionUrl { get; set; } 19 | 20 | public override int GetHashCode() 21 | { 22 | return base.GetHashCode() ^ FunctionName.GetHashCode(); 23 | } 24 | 25 | public override bool Equals(object obj) 26 | { 27 | if (obj.GetType() != typeof(Function)) 28 | return false; 29 | Function f = (Function)obj; 30 | return FunctionName.Equals(f.FunctionName); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Facade/烏龜BMO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO; 8 | 9 | namespace hsdc.dpt.Control.Structural.Facade 10 | { 11 | // BMO = Business Management Object 12 | // 負責關於烏龜商品的商務邏輯處理與運算 13 | public class 烏龜BMO 14 | { 15 | public ShoppingCart proc訂購資訊(ShoppingCart cart) 16 | { 17 | int total = 0; 18 | foreach (烏龜商品 item in cart.CartList) 19 | { 20 | total = total + (item.price * item.quantity); 21 | } 22 | 23 | // Todo: 24 | // 實作應有相關於 eCoupon 的處理邏輯︰這裡僅示範簡單的商業邏輯處理 25 | if (cart.eCoupon.Equals("1688")) 26 | cart.TotalPrice = Convert.ToInt32(total * 0.9); //九折優惠 27 | else 28 | cart.TotalPrice = total; 29 | 30 | return cart; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Adapter/供應商系統/OrderSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Structural.Adapter.供應商系統 8 | { 9 | public class OrderSystem 10 | { 11 | /* 12 | * 實務上,需實現可供遠端存取的協定(如 Web Service),以及公開介面與標準的參數、回傳值的格式 (如 Jason 字串) 13 | */ 14 | 15 | // 僅模擬透過資訊庫取得產品清單 16 | public string[][] get產品清單() 17 | { 18 | string[][] products = new string[4][]; 19 | // ================== 編號, 龜名, 價格, 數量 ================== 20 | products[0] = new string[] { "001", "澤龜", "300", "200" }; 21 | products[1] = new string[] { "002", "星龜", "1800", "103" }; 22 | products[2] = new string[] { "003", "金錢龜", "2400", "56" }; 23 | products[3] = new string[] { "004", "象龜", "68800", "12"}; 24 | 25 | return products; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/FactoryMethod/FactoryMethodDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.FactoryMethod 8 | { 9 | public class Customer 10 | { 11 | public string CustomerID { get; set; } 12 | public string CustomerName { get; set; } 13 | public string CustomerContactPerson { get; set; } 14 | } 15 | 16 | public class Supplier 17 | { 18 | public string SupplierID { get; set; } 19 | public string SupplierName { get; set; } 20 | public string SupplierContactEmail { get; set; } 21 | } 22 | 23 | public class Product 24 | { 25 | public string ProductID { get; set; } 26 | public string ProductName { get; set; } 27 | public string ProductType { get; set; } 28 | public decimal ProductMSRP { get; set; } 29 | public bool IsDelete { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HSDcDesingPatternCourse 2 | 1. 本專案內含完整的 C#.NET 程式碼與 UML Model 檔。 3 | 2. HSDc 未來所提供的開源文件,均會於 [FB 社團-軟體設計鮮思維](https://www.facebook.com/groups/softthinking/ "FB 社團-軟體設計鮮思維")公布。 4 | 3. 關於 C#.NET 程式碼的結構部分說明,請參考-http://www.kenming.idv.tw/opensource_hsdc-design-patterns_for-free 5 | 6 | # System Requirement 7 | 1. This demo is writing by Visual Studio Community 2015, if you want to view, debug or run this sample, please download Visual Studio Community 2015. 8 | 2. (optional) [EA UML Viewer](http://www.sparxsystems.com.au/products/ea/trial.html "EA UML Viewer")。 9 | 10 | # How to run this Sample? 11 | 1. Download or clone this repository. 12 | 2. Use Visual Studio Community to open the "Design Patterns Demo Solution.sln" solution file on "Source Code" directory. 13 | 3. Run the WebMVC Project. 14 | 4. Enjoy it! 15 | 16 | # Model file 17 | This sample contain all 23 design pattern's example and structure, and you could find the "Design Pattern.eap" in the "Model" directory. 18 | You could open this model file by using "Sparx System Enterprise Architect". 19 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/VisitorController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Behavioral.Visitor; 7 | 8 | namespace WebMVC.Controllers.Behavioral 9 | { 10 | public class VisitorController : Controller 11 | { 12 | private ManageHRControl control; 13 | 14 | // GET: Visitor 15 | public ActionResult Index() 16 | { 17 | return View(); 18 | } 19 | 20 | public ActionResult CalcTotalPrice() 21 | { 22 | control = new ManageHRControl(); 23 | 24 | ViewData["ProcssResult"] = control.calc公司每月固定支出(); 25 | 26 | return View("Index"); 27 | } 28 | 29 | public ActionResult CalcInventory() 30 | { 31 | control = new ManageHRControl(); 32 | 33 | ViewData["ProcssResult"] = control.calce公司資產總數(); 34 | 35 | return View("Index"); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/TemplateMethod/TemplateMethodDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.DTO.Behavioral.TemplateMethod 9 | { 10 | public class PolicyTransaction 11 | { 12 | public bool CanEdit { get; set; } 13 | public List Policies { get; set; } 14 | } 15 | public class Policy 16 | { 17 | public string PolicyNumber { get; set; } 18 | public string PolicyName { get; set; } 19 | public string PolicyOwnerName { get; set; } 20 | public string PolicyInsuredName { get; set; } 21 | public PolicyType PolicyType { get; set; } 22 | } 23 | 24 | public enum PolicyType 25 | { 26 | [DescriptionAttribute("正常")] 27 | Normal, 28 | [DescriptionAttribute("審核中")] 29 | InAprrove, 30 | [DescriptionAttribute("已理賠")] 31 | HasClaimed 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Singleton/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "ID Generator"; 4 | } 5 | 6 |

ID Generator

7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
請輸入表單類別
產生的編號為:
18 |
19 |
20 | 21 | 22 | @section BodyScripts 23 | { 24 | 39 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/CodeModel.Context.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 這個程式碼是由範本產生。 4 | // 5 | // 對這個檔案進行手動變更可能導致您的應用程式產生未預期的行為。 6 | // 如果重新產生程式碼,將會覆寫對這個檔案的手動變更。 7 | // 8 | //------------------------------------------------------------------------------ 9 | 10 | namespace hsdc.dpt.Control.Structural.Flyweight.Dao 11 | { 12 | using System; 13 | using System.Data.Entity; 14 | using System.Data.Entity.Infrastructure; 15 | 16 | public partial class DesignPatternEntities : DbContext 17 | { 18 | public DesignPatternEntities() 19 | : base("name=DesignPatternEntities") 20 | { 21 | Code = Set(); 22 | } 23 | 24 | protected override void OnModelCreating(DbModelBuilder modelBuilder) 25 | { 26 | throw new UnintentionalCodeFirstException(); 27 | } 28 | 29 | internal virtual DbSet Code { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/Code.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 這個程式碼是由範本產生。 4 | // 5 | // 對這個檔案進行手動變更可能導致您的應用程式產生未預期的行為。 6 | // 如果重新產生程式碼,將會覆寫對這個檔案的手動變更。 7 | // 8 | //------------------------------------------------------------------------------ 9 | 10 | namespace hsdc.dpt.Control.Structural.Flyweight.Dao 11 | { 12 | using System; 13 | using System.Collections.Generic; 14 | 15 | internal partial class Code 16 | { 17 | public Code() 18 | { 19 | this.Code1 = new HashSet(); 20 | } 21 | 22 | internal int Id { get; set; } 23 | internal string CodeType { get; set; } 24 | internal string CodeValue { get; set; } 25 | internal string CodeText { get; set; } 26 | internal Nullable ParentId { get; set; } 27 | 28 | internal virtual ICollection Code1 { get; set; } 29 | internal virtual Code Code2 { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/MediatorController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.Mediator; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Web; 6 | using System.Web.Mvc; 7 | 8 | namespace WebMVC.Controllers.Behavioural 9 | { 10 | public class MediatorController : Controller 11 | { 12 | // GET: Mediator 13 | public ActionResult Index() 14 | { 15 | return View(); 16 | } 17 | 18 | public ActionResult Order() 19 | { 20 | StockingUco uco = new StockingUco(); 21 | return View(uco.GetAllProducts()); 22 | } 23 | 24 | public ActionResult StockIn() 25 | { 26 | StockingUco uco = new StockingUco(); 27 | return View(uco.GetAllProducts()); 28 | } 29 | 30 | public JsonResult CheckQuantity(string prodName) 31 | { 32 | StockingUco uco = new StockingUco(); 33 | return Json(uco.CheckQunatity(prodName), JsonRequestBehavior.AllowGet); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Facade/AddToCart.cshtml: -------------------------------------------------------------------------------- 1 | @model hsdc.dpt.Control.DTO.ShoppingCart 2 | 3 | @{ 4 | ViewBag.Title = "AddToCart"; 5 | } 6 | 7 |

購物車資訊

8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | @foreach (var cartItem in Model.CartList) 19 | { 20 | 21 | 22 | 23 | 24 | 25 | 26 | } 27 |
編號龜名價錢購買數量
@cartItem.id@cartItem.name@cartItem.price@cartItem.quantity
28 |
29 |
@Model.TotalPrice


30 |
  
31 |
32 | 33 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/EmailSender.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | /// 11 | /// The 'ConcreteImplementor' class 12 | /// 實現電子郵件的傳送訊息 13 | /// 14 | public class EmailSender : IMessageSender 15 | { 16 | // 實務上,需實作呼叫郵件系統(如 SMTP)的 API 17 | // 將訊息傳入至郵件系統 18 | public string SendMessage(Message msg) 19 | { 20 | string result; 21 | result = "交由 Email Sender 傳送:\n\n" 22 | + "收件人:" + msg.Recipient + "\n" 23 | + "主旨 :" + msg.Subject + "\n" 24 | + "內容 :" + msg.Body; 25 | return result; 26 | } 27 | 28 | // 模擬將訊息內容編碼的程序 29 | public string 編碼(string msgBody) 30 | { 31 | string mBody = "{已編碼內容:" + msgBody + "}\n\n"; 32 | return mBody; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/LineSender.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO.Structual.Bridge; 8 | 9 | namespace hsdc.dpt.Control.Structural.Bridge 10 | { 11 | /// 12 | /// The 'ConcreteImplementor' class 13 | /// 實現由 Line 傳送訊息 14 | /// 15 | public class LineSender : IMessageSender 16 | { 17 | // 實務上,需實作呼叫 Line 系統的 API 18 | // 將訊息傳入至 Line 系統 19 | public string SendMessage(Message msg) 20 | { 21 | string result; 22 | result = "交由 Line Sender 傳送:\n\n" 23 | + "收件人:" + msg.Recipient + "\n" 24 | + "主旨 :" + msg.Subject + "\n" 25 | + "內容 :" + msg.Body; 26 | return result; 27 | } 28 | 29 | // 模擬將訊息內容編碼的程序 30 | public string 編碼(string msgBody) 31 | { 32 | // Line 不實作編碼的程序,僅傳未編碼的訊息內容 33 | return msgBody; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 組件的一般資訊是由下列的屬性集 6 | // 控制。變更這些屬性值可修改與組件關聯的 7 | // 資訊。 8 | [assembly: AssemblyTitle("WebMVC")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("WebMVC")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 將 ComVisible 設定為 false 會使得這個組件中的型別 18 | // 對 COM 元件而言為不可見。如果您需要從 COM 存取此組件中 19 | // 的型別,請在該型別上將 ComVisible 屬性設定為 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 下列 GUID 為專案公開 (Expose) 至 COM 時所要使用的 typelib ID 23 | [assembly: Guid("11f0592c-ad6a-4cdc-9f3f-2ed23c9a3b21")] 24 | 25 | // 組件的版本資訊是由下列四項值構成: 26 | // 27 | // 主要版本 28 | // 次要版本 29 | // 組建編號 30 | // 修訂編號 31 | // 32 | // 您可以指定所有的值,也可以依照以下的方式,使用 '*' 將修訂和組建編號 33 | // 指定為預設值: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Observer/ManageNewsControl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Observer 8 | { 9 | /// 10 | /// The 'Client' class. 11 | /// 12 | public class ManageNewsControl 13 | { 14 | // declare a News Publisher (Subject) 15 | private NewsPublisher publisher; 16 | 17 | // Constructor 18 | public ManageNewsControl() 19 | { 20 | publisher = new NewsPublisher(); 21 | } 22 | 23 | // 發佈新聞 24 | public void 發佈(string news) 25 | { 26 | publisher.publish(news); 27 | } 28 | 29 | // 新增訂閱者 (Observer) 30 | public void 訂閱(IObserver observer) 31 | { 32 | publisher.Subscribe(observer); 33 | } 34 | 35 | // 移除訂閱者 (Observer) 36 | public void 取消訂閱(IObserver observer) 37 | { 38 | publisher.UnSubscribe(observer); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/SystemNotifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | public class SystemNotifier : Notifier 11 | { 12 | /// 13 | /// 實現 Notify 處理的程序 14 | /// 15 | /// Message 物件 16 | /// 處理結果 17 | public override string Notify(Message message) 18 | { 19 | string procResult; // 處理結果 20 | 21 | // 1. 寫入到 System Event Log 22 | procResult = "程序:訊息內容寫入到 System Event Log\n\n"; 23 | 24 | // 2.1 將訊息內容編碼 25 | procResult = procResult + "執行編碼程序:\n"; 26 | message.Body = sender.編碼(message.Body); 27 | 28 | // 2.2 將訊息傳送給所指定的收件人 29 | procResult = procResult + sender.SendMessage(message); 30 | 31 | return procResult; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/IMessageSender.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | /// 11 | /// The 'Bridge/Implementor' interface 12 | /// 負責所關聯 Abstraction 類別定義 function 的實作行為的介面定義 13 | /// 註:一個 Abstraction 所定義的 function,可能需一至多個 Bridge 介面所定義的 function 來實現 14 | /// 15 | public interface IMessageSender 16 | { 17 | /// 18 | /// 傳送訊息的行為定義 19 | /// 20 | /// Message(訊息)物件 21 | /// 傳送結果 22 | string SendMessage(Message message); 23 | 24 | /// 25 | /// 對訊息內容編碼行為的定義 26 | /// 此為想像上的需求,目的在於呈現 Abstraction 主體的行為(本例為 notify),可能會需由 1到多個 實作行為完成 27 | /// 28 | /// 訊息內容 29 | /// 取得編碼後的訊息內容 30 | string 編碼(string messageBody); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Bridge/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Bridge Pattern

7 |
8 |
9 | 10 | @Html.RadioButton("NotifyType", "UserNotify", true) 使用者訊息 11 | @Html.RadioButton("NotifyType", "SystemNotify", false) 系統訊息
12 |
13 |
14 |
15 | 16 | @Html.RadioButton("SenderType", "EmailSender", true) Email 17 | @Html.RadioButton("SenderType", "LineSender", false) Line
18 |
19 |
20 |
21 | 22 | @Html.TextBox("txtRecipient") 23 |
24 |
25 | 26 | @Html.TextBox("txtSubject") 27 |
28 |
29 | 30 | @Html.TextArea("txtBody") 31 |
32 |

33 |
34 |
35 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/StrategyController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Behavioral.Strategy; 7 | using hsdc.dpt.Control.DTO.Behavioral.Strategy; 8 | 9 | namespace WebMVC.Controllers.Behavioral 10 | { 11 | public class StrategyController : Controller 12 | { 13 | // GET: Strategy 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | [HttpPost] 20 | public ActionResult ExecuteStrategy(string selStrategyType) 21 | { 22 | PlaceOrderControl control = new PlaceOrderControl(); // domain control object 23 | StrategyType type; // enum type 24 | 25 | if (selStrategyType.Equals("黃金交叉")) 26 | type = StrategyType.GoldenCross; 27 | else 28 | type = StrategyType.RSI; 29 | 30 | ViewData["ProcssResult"] = control.ExecuteStrategy(type); 31 | 32 | return View("Index"); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Interpreter/Index.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | ViewBag.Title = "中文數值轉換"; 3 | } 4 | 5 |

中文數值轉換

6 | 7 | 8 | 9 | 10 | 11 |
請輸入數值進行轉換:
12 | 13 |
14 | 16 | 17 | @section BodyScripts 18 | { 19 | 38 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 組件的一般資訊是由下列的屬性集控制。 6 | // 變更這些屬性的值即可修改組件的相關 7 | // 資訊。 8 | [assembly: AssemblyTitle("Control")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Control")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 將 ComVisible 設定為 false 會使得這個組件中的類型 18 | // 對 COM 元件而言為不可見。如果您需要從 COM 存取這個組件中 19 | // 的類型,請在該類型上將 ComVisible 屬性設定為 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 下列 GUID 為專案公開 (Expose) 至 COM 時所要使用的 typelib ID 23 | [assembly: Guid("e2cf7496-6257-4d9b-8270-a933b2790264")] 24 | 25 | // 組件的版本資訊是由下列四項值構成: 26 | // 27 | // 主要版本 28 | // 次要版本 29 | // 組建編號 30 | // 修訂編號 31 | // 32 | // 您可以指定所有的值,也可以依照以下的方式,使用 '*' 將組建和修訂編號 33 | // 指定為預設值: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/FactoryMethod/MaintainCustomer.cshtml: -------------------------------------------------------------------------------- 1 | @model IEnumerable 2 | 3 | @{ 4 | ViewBag.Title = "維護客戶資料"; 5 | } 6 | 7 |

維護客戶資料

8 | 9 |

10 | @Html.ActionLink("新增客戶", "CreateCustomer") 11 |

12 | 13 | 14 | 17 | 20 | 23 | 24 | 25 | 26 | @foreach (var item in Model) { 27 | 28 | 31 | 34 | 37 | 41 | 42 | } 43 | 44 |
15 | 客戶編號 16 | 18 | 客戶姓名 19 | 21 | 客戶聯絡人 22 |
29 | @Html.DisplayFor(modelItem => item.CustomerID) 30 | 32 | @Html.DisplayFor(modelItem => item.CustomerName) 33 | 35 | @Html.DisplayFor(modelItem => item.CustomerContactPerson) 36 | 38 | @Html.ActionLink("編輯", "EditCustomer", new { id=item.CustomerID }) | 39 | @Html.ActionLink("刪除", "DeleteCustomer", new { id=item.CustomerID }) 40 |
45 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/FactoryMethod/MaintainSupplier.cshtml: -------------------------------------------------------------------------------- 1 | @model IEnumerable 2 | 3 | @{ 4 | ViewBag.Title = "維護廠商資料"; 5 | } 6 | 7 |

維護廠商資料

8 | 9 |

10 | @Html.ActionLink("新增廠商", "CreateSupplier") 11 |

12 | 13 | 14 | 17 | 20 | 23 | 24 | 25 | 26 | @foreach (var item in Model) { 27 | 28 | 31 | 34 | 37 | 41 | 42 | } 43 | 44 |
15 | 廠商編號 16 | 18 | 廠商名稱 19 | 21 | 廠商聯絡Email 22 |
29 | @Html.DisplayFor(modelItem => item.SupplierID) 30 | 32 | @Html.DisplayFor(modelItem => item.SupplierName) 33 | 35 | @Html.DisplayFor(modelItem => item.SupplierContactEmail) 36 | 38 | @Html.ActionLink("編輯", "EditSupplier", new { id=item.SupplierID }) | 39 | @Html.ActionLink("刪除", "DeleteSupplier", new { id=item.SupplierID }) 40 |
45 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Structual/Bridge/Interface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.DTO.Structual.Bridge.Intf 9 | { 10 | internal interface StatusProgressImpl 11 | { 12 | OrderProgress GetCurrentOrderProgress(string oid); 13 | } 14 | 15 | public enum OrderProgress 16 | { 17 | [DescriptionAttribute("收到訂單")] 18 | RECEIVE_ORDER = 0, 19 | [DescriptionAttribute("撿貨")] 20 | PICK_PRODUCT = 1, 21 | [DescriptionAttribute("理貨")] 22 | ARRANGE_PRODUCT = 2, 23 | [DescriptionAttribute("出貨")] 24 | SHIPPING = 3, 25 | [DescriptionAttribute("宅配單位收貨")] 26 | IN_SHIPMENT_STOCK = 4, 27 | [DescriptionAttribute("宅配單位運送中")] 28 | IN_DELIVER_WAY = 5, 29 | [DescriptionAttribute("到貨")] 30 | DELIVERED = 6, 31 | [DescriptionAttribute("無資料")] 32 | NONE = 99 33 | } 34 | 35 | public enum OrderStatus 36 | { 37 | IN_STOCK, 38 | SHIPPING 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Facade/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Facade Pattern

7 | 8 |

列出菜單

9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | @foreach (var item in Model) 21 | { 22 | 23 | 24 | 25 | 26 | 27 | 28 | } 29 |
編號菜名價錢購買數量 
@item.id@item.name@item.price
30 |
31 |
32 |

33 | 34 |
35 |
-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Web.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 30 | 31 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/ConsoleApplication/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 組件的一般資訊是由下列的屬性集控制。 6 | // 變更這些屬性的值即可修改組件的相關 7 | // 資訊。 8 | [assembly: AssemblyTitle("ConsoleApplication")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ConsoleApplication")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 將 ComVisible 設定為 false 會使得這個組件中的類型 18 | // 對 COM 元件而言為不可見。如果您需要從 COM 存取這個組件中 19 | // 的類型,請在該類型上將 ComVisible 屬性設定為 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 下列 GUID 為專案公開 (Expose) 至 COM 時所要使用的 typelib ID 23 | [assembly: Guid("3265b500-957e-48a0-8a83-96d634e9d71c")] 24 | 25 | // 組件的版本資訊是由下列四項值構成: 26 | // 27 | // 主要版本 28 | // 次要版本 29 | // 組建編號 30 | // 修訂編號 31 | // 32 | // 您可以指定所有的值,也可以依照以下的方式,使用 '*' 將組建和修訂編號 33 | // 指定為預設值: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Memento/複合彩.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Memento 8 | { 9 | /// 10 | /// The 'Originator' Class. 11 | /// 12 | public class 複合彩 13 | { 14 | public int 彩券號碼 { set; get; } // 兩位數 00~99 15 | public string 字彩 { set; get; } // 四字成語 16 | private string _其它屬性; // 為表達封裝性,該屬性並不需要讓外界直接操作存取 17 | 18 | // 自動選號。目前以亂數產生,但可以搭配更多的策略來產生選號的結果 19 | public int 選號() 20 | { 21 | Random rnd = new Random(); 22 | int num = rnd.Next(100); 23 | 24 | return num; 25 | } 26 | 27 | // 設定 (還原)原來選號的狀態 28 | public void SetMemento(複合彩Memento memento) 29 | { 30 | this.彩券號碼 = memento.彩券號碼; 31 | this.字彩 = memento.字彩; 32 | } 33 | 34 | // 建立 (儲存) 選號的狀態 35 | public 複合彩Memento CreateMemento() 36 | { 37 | return new 複合彩Memento(this.彩券號碼, this.字彩); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/CoR/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Index"; 4 | } 5 | 6 |

Chain of Responsibility Pattern

7 |
8 |
9 | 10 | @Html.TextBox("TxtName") 11 |
12 |
13 | 14 | 20 |
21 |
22 | 23 | @Html.TextBox("TxtDonateMoney") 24 |
25 |
26 | 27 | @Html.TextBox("TxtExtInfo") 28 |
29 |
30 |
31 |
32 |
33 |

執行結果:

34 |
35 |

@MvcHtmlString.Create(Html.Encode(ViewData["ProcssResult"]).Replace("\n", "
"))

36 |
-------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Strategy/RSI指標Strategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Strategy 8 | { 9 | /// 10 | /// The 'ConcreteStrategy' Class 11 | /// 使用指標數值,>30 執行買進;<70 執行賣出交易策略 12 | /// 13 | public class RSI指標Strategy : IStrategy 14 | { 15 | public string execute() 16 | { 17 | string comment; 18 | string pseudo_code; 19 | 20 | comment = "RSI 交易策略:\n" + 21 | "指標數值>30,即執行買進;\n" + 22 | "指標數值<70,即執行賣出;\n" + 23 | "==========================================\n\n"; 24 | 25 | pseudo_code = "var RSI = RSI(CLOSED,14);\n" + 26 | "\n" + 27 | "If CrossOver(RSI, 30)\n" + 28 | " // 觸發(Trig)買進訊號\n" + 29 | "else\n" + 30 | " If CrossUnder(RSI, 70)\n" + 31 | " // 觸發(Trig)賣出訊號\n"; 32 | 33 | return comment + pseudo_code; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Builder/NewLeave.cshtml: -------------------------------------------------------------------------------- 1 | @model hsdc.dpt.Control.DTO.Creational.Builder.Leave 2 | 3 | @{ 4 | ViewBag.Title = "新增請假單"; 5 | } 6 | 7 |

新增請假單

8 | 9 | @using (Html.BeginForm("SaveLeave", "Builder", FormMethod.Post, new { id = "frmSave" })) 10 | { 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
請假單號@(Model.LeaveID == null ? "" : Model.LeaveID)
申請人@Html.TextBoxFor(m => m.Applier)
申請類別@Html.TextBoxFor(m => m.LeaveType)
申請日期@Html.TextBoxFor(m => m.LeaveBeginDate)
總申請日@Html.TextBoxFor(m => m.LeaveDay)
33 |
34 |
35 | 36 | } 37 | 38 | @section BodyScripts 39 | { 40 | 45 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/State/2_Refactoring/OrderStateContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.State 8 | { 9 | /// 10 | /// The 'Context' Class 11 | /// 1. 定義外界會呼叫使用的 function,且該 function 會影響狀態的變化 12 | /// 2. 持有實現 (realize) State 介面 (interface) 的子類別 (Concrete State)個體 (instance),代表現行狀態。 13 | /// 14 | public class OrderStateContext 15 | { 16 | // 指向實現 IOrderState 介面的子類別的參考變數;代表現行的狀態。 17 | private IOrderState current_state; 18 | 19 | // constructor 20 | public OrderStateContext() 21 | { 22 | current_state = new NewOrderState(); // 訂單建立時的初始狀態為 'NEW_ORDER' 23 | } 24 | 25 | // 依據訂單的狀態處理並簽署該狀態下的執行情形 26 | public string StampIt() 27 | { 28 | string procResult = current_state.processSomething(this); 29 | 30 | return procResult; 31 | } 32 | 33 | // 設定下一關卡的狀態 34 | public void setState(IOrderState state) 35 | { 36 | current_state = state; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Web.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 31 | 32 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Strategy/黃金交叉Strategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Strategy 8 | { 9 | /// 10 | /// The 'ConcreteStrategy' Class 11 | /// 使用雙均線 (一長一短)交叉向上/往下,執行買進/賣出交易策略 12 | /// 13 | public class 黃金交叉Strategy : IStrategy 14 | { 15 | public string execute() 16 | { 17 | string comment; 18 | string pseudo_code; 19 | 20 | comment = "黃金交叉策略:\n" + 21 | "5根均Bar 與 15根均Bar 交叉向上,即執行買進;\n" + 22 | "5根均Bar 與 15根均Bar 交叉往上,即執行賣出;\n" + 23 | "==========================================\n\n"; 24 | 25 | pseudo_code = "var MA5 = MA(5);\n" + 26 | "var MA15 = MA(15);\n" + 27 | "\n" + 28 | "If CrossOver(MA5,MA15)\n" + 29 | " // 觸發(Trig)買進訊號\n" + 30 | "else\n" + 31 | "If CrossUnder(MA5,MA15)" + 32 | " // 觸發(Trig)賣出訊號\n"; 33 | 34 | return comment + pseudo_code; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Prototype/RingleERP.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "取得採購清單"; 5 | } 6 | 7 |

RingleERP範例 - 取得採購清單

8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | @if (Model != null) 26 | { 27 | foreach (var po in Model) 28 | { 29 | 30 | 31 | 32 | 33 | 34 | 35 | } 36 | } 37 | else 38 | { 39 | 40 | 41 | 42 | } 43 | 44 |
採購編號客戶名稱總金額複製
@po.PoNumber@po.CustomerName@po.Total複製
查無相關資料
45 |
46 |
47 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/FactoryMethod/MaintainProduct.cshtml: -------------------------------------------------------------------------------- 1 | @model IEnumerable 2 | 3 | @{ 4 | ViewBag.Title = "維護產品資料"; 5 | } 6 | 7 |

維護產品資料

8 | 9 |

10 | @Html.ActionLink("新增產品", "CreateProduct") 11 |

12 | 13 | 14 | 17 | 20 | 23 | 26 | 27 | 28 | 29 | @foreach (var item in Model) { 30 | 31 | 34 | 37 | 40 | 43 | 47 | 48 | } 49 | 50 |
15 | 產品編號 16 | 18 | 產品名稱 19 | 21 | 產品類別 22 | 24 | 產品建議售價 25 |
32 | @Html.DisplayFor(modelItem => item.ProductID) 33 | 35 | @Html.DisplayFor(modelItem => item.ProductName) 36 | 38 | @Html.DisplayFor(modelItem => item.ProductType) 39 | 41 | @Html.DisplayFor(modelItem => item.ProductMSRP) 42 | 44 | @Html.ActionLink("編輯", "EditProduct", new { id=item.ProductID }) | 45 | @Html.ActionLink("刪除", "DeleteProduct", new { id=item.ProductID }) 46 |
51 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/Participant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Creational.AbstractFactory; 7 | 8 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 9 | { 10 | /// 11 | /// The root class of Participant. 12 | /// 13 | public abstract class Participant 14 | { 15 | public AssigneeType assignType { get; set; } // 參與者類型 16 | public string aggignee_name { get; set; } // 參與者名稱 17 | 18 | // 定義處理行為的介面 19 | public abstract string process(); 20 | } 21 | 22 | /// 23 | /// The 'Abstract Product' class 24 | /// '組織參與者' 抽象產品 25 | /// 26 | public abstract class OrganizationParticipant : Participant { 27 | } 28 | 29 | /// 30 | /// The 'Abstract Product' class 31 | /// '角色參與者' 抽象產品 32 | /// 33 | public abstract class RoleParticipant : Participant { 34 | } 35 | 36 | /// 37 | /// The 'Abstract Product' class 38 | /// '人參與者' 抽象產品 39 | /// 40 | public abstract class HumanParticipant : Participant { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Builder/NewApplyExpense.cshtml: -------------------------------------------------------------------------------- 1 | @model hsdc.dpt.Control.DTO.Creational.Builder.ApplyExpense 2 | 3 | @{ 4 | ViewBag.Title = "新增請假單"; 5 | } 6 | 7 |

新增請假單

8 | 9 | @using (Html.BeginForm("SaveApplyExpense", "Builder", FormMethod.Post, new { id = "frmSave" })) 10 | { 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
費用申請單單號@(Model.ApplyExpenseID == null ? "" : Model.ApplyExpenseID)
申請人@Html.TextBoxFor(m => m.Applier)
申請類別@Html.TextBoxFor(m => m.ApplyExpenseType)
申請日期@Html.TextBoxFor(m => m.ApplyDate)
總申請金額@Html.TextBoxFor(m => m.TotalExpense, new Dictionary() { { "readonly", "true" } })
33 |
34 |
35 | 36 | } 37 | 38 | @section BodyScripts 39 | { 40 | 45 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/State/TrackOrderControl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.State 8 | { 9 | public class TrackOrderControl 10 | { 11 | private OrderState_SwitchCase order_state_use_switch; 12 | private OrderStateContext state_context; 13 | 14 | // constructor 15 | public TrackOrderControl() 16 | { 17 | order_state_use_switch = new OrderState_SwitchCase(); 18 | state_context = new OrderStateContext(); 19 | } 20 | 21 | /// 22 | /// 作法一:使用 Switch-Case 的作法處理訂單的狀態 23 | /// 24 | /// 25 | public string StampOrder_1() 26 | { 27 | string procResult = order_state_use_switch.StampIt(); 28 | 29 | return procResult; 30 | } 31 | 32 | /// 33 | /// 作法二:使用 State Pattern 設計處理訂單的狀態 (完全消彌掉 Switch-If-then-else) 34 | /// 35 | /// 36 | public string StampOrder_2() 37 | { 38 | string procResult = state_context.StampIt(); 39 | 40 | return procResult; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/Dao/CodeDao.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Flyweight; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Structural.Flyweight.Dao 9 | { 10 | internal class CodeDao 11 | { 12 | internal List query(string type) 13 | { 14 | Dao.DesignPatternEntities entity = new DesignPatternEntities(); 15 | var codes = (from c in entity.Code 16 | where c.CodeType.Equals(type) 17 | orderby c.Id 18 | select new CodeDto() { CodeValue = c.CodeValue, CodeText = c.CodeText }).ToList(); 19 | return codes; 20 | } 21 | 22 | internal List query(string type, string parentValue) 23 | { 24 | Dao.DesignPatternEntities entity = new DesignPatternEntities(); 25 | var codes = (from c in entity.Code 26 | where c.CodeType.Equals(type) && c.Code2.CodeValue.Equals(parentValue) 27 | orderby c.Id 28 | select new CodeDto() { CodeValue = c.CodeValue, CodeText = c.CodeText }).ToList(); 29 | return codes; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Proxy/龜庫Proxy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Structural.Proxy 8 | { 9 | public class 龜庫Proxy : I龜庫接口 10 | { 11 | /// 12 | /// 取得所販售烏龜的認證資訊 13 | /// 14 | /// 烏龜品種 15 | /// 認證資訊 16 | public string Get認證資訊(string m品種, string m序號) 17 | { 18 | string 認證資訊; 19 | 20 | // 1. 如果販售的烏龜為保育類 (本例象龜為保育類) 21 | // 需透過 龜庫Informer 類別連線至總部系統取得認證資訊 22 | // 2. 若為一般烏龜,則直接從區域端資料庫取得認證資訊 23 | if (m品種.Equals("象龜")) 24 | { 25 | // new the real-subject instance 26 | 龜庫Informer subject = new 龜庫Informer(); 27 | 認證資訊 = subject.Get認證資訊(m品種, m序號); 28 | } 29 | else 30 | 認證資訊 = this.查詢認證(m序號); 31 | 32 | return 認證資訊; 33 | } 34 | 35 | // 透過序號至資料庫系統取得認證資訊 36 | private string 查詢認證(string m序號) 37 | { 38 | // TODO: 需連線至資料庫系統 39 | string queryResult = "序號:" + m序號 + " 的認證資訊從" + "「區域端資料庫」取得認證結果。"; 40 | 41 | return queryResult; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Adapter/Get存貨Adapter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO.Structual.Adapter; 8 | using hsdc.dpt.Control.Structural.Adapter.供應商系統; 9 | 10 | namespace hsdc.dpt.Control.Structural.Adapter 11 | { 12 | // Realize the IGet存貨資訊 Interface 13 | public class Get存貨Adapter : IGet存貨資訊 14 | { 15 | public List<烏龜商品> get存貨清單() 16 | { 17 | OrderSystem mOrder = new OrderSystem(); // 建立對外部供應商系統的參考 18 | List<烏龜商品> aProdList = new List<烏龜商品>(); 19 | 20 | // 從外部系統取得產品清單 21 | // TODO: 實務應實作如何取得遠端系統的協定, 介面, 參數, 回傳值 22 | string[][] mOrders = mOrder.get產品清單(); 23 | 24 | // A供應商系統的回傳格式為2維字串陣列,因本系統以 List 為回傳值,故需實作轉型的工作 25 | foreach (string[] product in mOrders) 26 | { 27 | 烏龜商品 aProd = new 烏龜商品(); 28 | 29 | aProd.id = product[0]; 30 | aProd.name = product[1]; 31 | aProd.price = Convert.ToInt32(product[2]); 32 | aProd.quantity = Convert.ToInt32(product[3]); 33 | 34 | // 將轉型後的烏龜商品資訊新增至 List 集合 35 | aProdList.Add(aProd); 36 | } 37 | 38 | return aProdList; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/StateController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Behavioral.State; 7 | 8 | namespace WebMVC.Controllers.Behavioral 9 | { 10 | public class StateController : Controller 11 | { 12 | private TrackOrderControl control; 13 | 14 | // GET: State 15 | public ActionResult Index() 16 | { 17 | if (Session["TrackOrderControl"] == null) 18 | { 19 | control = new TrackOrderControl(); // new a domain control instance. 20 | Session["TrackOrderControl"] = control; 21 | } 22 | else 23 | Session["TrackOrderControl"] = control; // hold the control object as stateful. 24 | 25 | return View(); 26 | } 27 | 28 | public ActionResult Stamp() 29 | { 30 | control = (TrackOrderControl)Session["TrackOrderControl"]; // get the control object from the Session Object. 31 | ViewData["OrderState"] = control.StampOrder_1(); // 作法一:使用 Switch-Case 處理訂單狀態 32 | //ViewData["OrderState"] = control.StampOrder_2(); // 作法二:使用 State Pattern 處理訂單狀態 33 | 34 | return View("Index"); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Home/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "Design Pattern課程範例"; 4 | } 5 | 6 |

Design Pattern課程範例

7 |

Creational Pattern:

8 |

Abstract Factory Pattern

9 |

Builder Pattern

10 |

Factory Method Pattern

11 |

Prototype Pattern

12 |

Singleton Pattern

13 |
14 |

Structural Pattern:

15 |

Adapter Pattern

16 |

Bridge Pattern

17 |

Composite Pattern

18 |

Decorator Pattern

19 |

Facade Pattern

20 |

Flyweight Pattern

21 |

Proxy Pattern

22 |
23 |

Behavioral Pattern:

24 |

Command Pattern

25 |

Chain of Responsibility Pattern

26 |

Interpreter Pattern

27 |

Mediator Pattern

28 |

Memento Pattern

29 |

State Pattern

30 |

Strategy Pattern

31 |

Template Method Pattern

32 |

Visitor Pattern

33 |
34 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Builder/BuilderDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Builder 8 | { 9 | public class Leave 10 | { 11 | public String LeaveID { get; set; } 12 | public String Applier { get; set; } 13 | public String LeaveType { get; set; } 14 | public int LeaveDay { get; set; } 15 | public DateTime LeaveBeginDate { get; set; } 16 | } 17 | 18 | public class ApplyExpense 19 | { 20 | public String ApplyExpenseID { get; set; } 21 | public String Applier { get; set; } 22 | public String ApplyExpenseType { get; set; } 23 | public decimal TotalExpense { 24 | get 25 | { 26 | decimal exp = 0; 27 | if (LineItems != null) 28 | LineItems.ForEach(d => exp += d.Expense); 29 | return exp; 30 | } 31 | } 32 | public DateTime ApplyDate { get; set; } 33 | public List LineItems { get; set; } 34 | } 35 | 36 | public class ApplyExpenseDetail 37 | { 38 | public String ReferenceNumber { get; set; } 39 | public DateTime OccureDate { get; set; } 40 | public decimal Expense { get; set; } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Prototype/RingleFramework.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "取得採購清單"; 5 | } 6 | 7 |

取得採購清單

8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | @if (Model != null) 24 | { 25 | foreach (var po in Model) 26 | { 27 | 28 | 29 | 30 | 31 | 32 | } 33 | } 34 | else 35 | { 36 | 37 | 38 | 39 | } 40 | 41 |
採購編號客戶名稱總金額
@po.PoNumber@po.CustomerName@po.Total
查無相關資料
42 |
43 |
44 | 45 |
46 | 47 | @section BodyScripts 48 | { 49 | 54 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Observer/NewsPublisher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Observer 8 | { 9 | /// 10 | /// The 'ConcreteSubject' class. 11 | /// 新聞發佈者 12 | /// 13 | public class NewsPublisher : IPublisher 14 | { 15 | private List observers; // 利用 List 儲存所有 Observer (觀察者) 16 | private string news; // 新聞訊息 17 | 18 | // Constructor 19 | public NewsPublisher() 20 | { 21 | observers = new List(); 22 | } 23 | 24 | // 新增 Observer 25 | public void Subscribe(IObserver observer) 26 | { 27 | observers.Add(observer); 28 | } 29 | 30 | // 移除 Observer 31 | public void UnSubscribe(IObserver observer) 32 | { 33 | observers.Remove(observer); 34 | } 35 | 36 | // 通知 (Notify)所有已訂閱的 Observer 執行更新的操作 37 | public void Notify() 38 | { 39 | foreach (IObserver o in observers) 40 | { 41 | o.Update(news); 42 | } 43 | } 44 | 45 | // 發佈新聞 46 | public void publish(string _news) 47 | { 48 | this.news = _news; 49 | this.Notify(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Strategy/PlaceOrderControl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Behavioral.Strategy; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.Strategy 9 | { 10 | /// 11 | /// The 'Client' Class 12 | /// 處理交易的控制類別 13 | /// 14 | public class PlaceOrderControl 15 | { 16 | private StrategyContext context; 17 | 18 | // constructor 19 | public PlaceOrderControl() 20 | { 21 | this.context = new StrategyContext(); 22 | } 23 | 24 | // 執行交易策略 25 | public string ExecuteStrategy(StrategyType type) 26 | { 27 | string procResult; 28 | 29 | // TODO: 這一段未來重構(refactor)時應該要交給工廠物件產生 30 | switch (type) 31 | { 32 | case(StrategyType.GoldenCross): 33 | context.SetStrategy(new 黃金交叉Strategy()); 34 | break; 35 | case(StrategyType.RSI): 36 | context.SetStrategy(new RSI指標Strategy()); 37 | break; 38 | default: 39 | procResult = string.Empty; 40 | break; 41 | } 42 | procResult = context.UpdateOnTick(); 43 | 44 | return procResult; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Behavioral/Interpreter/InterpreterDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.DTO.Behavioral.Interpreter 9 | { 10 | public enum ChineseUnit 11 | { 12 | [DescriptionAttribute("拾")] 13 | Ten = 1, 14 | [DescriptionAttribute("佰")] 15 | Hundred = 2, 16 | [DescriptionAttribute("仟")] 17 | Thousand = 3, 18 | [DescriptionAttribute("萬")] 19 | TenThousand = 4, 20 | [DescriptionAttribute("億")] 21 | HundredMillion = 8, 22 | [DescriptionAttribute("兆")] 23 | ThousandBillion = 12 24 | } 25 | 26 | public enum ChineseNumber 27 | { 28 | [DescriptionAttribute("壹")] 29 | One = 1, 30 | [DescriptionAttribute("貳")] 31 | Two = 2, 32 | [DescriptionAttribute("參")] 33 | Three = 3, 34 | [DescriptionAttribute("肆")] 35 | Four = 4, 36 | [DescriptionAttribute("伍")] 37 | Five = 5, 38 | [DescriptionAttribute("陸")] 39 | Six = 6, 40 | [DescriptionAttribute("柒")] 41 | Seven = 7, 42 | [DescriptionAttribute("捌")] 43 | Eight = 8, 44 | [DescriptionAttribute("玖")] 45 | Nine = 9, 46 | [DescriptionAttribute("零")] 47 | Zero = 0 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Memento/管理博彩Control.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Behavioral.Memento; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.Memento 9 | { 10 | /// 11 | /// The 'Client' class 12 | /// 13 | public class 管理博彩Control 14 | { 15 | private 複合彩 originator; // 需要儲存狀態的來源物件 16 | private 複合彩Caretaker caretaker; // 復原機制 17 | 18 | // Constructor 19 | public 管理博彩Control() 20 | { 21 | originator = new 複合彩(); 22 | caretaker = new 複合彩Caretaker(); 23 | } 24 | 25 | // 由 ogiginator 負責執行自動選號的功能 26 | public int 選號() 27 | { 28 | return originator.選號(); 29 | } 30 | 31 | // 將組合彩券的狀態儲存起來 32 | public void Save選號(複合彩DTO dto) 33 | { 34 | originator.字彩 = dto.成語字謎; 35 | originator.彩券號碼 = Convert.ToInt32(dto.彩券號碼); 36 | 37 | caretaker.SaveState(this.originator); 38 | } 39 | 40 | // 透過復原機制回復組合彩券的狀態 41 | public 複合彩DTO 還原選號() 42 | { 43 | 複合彩DTO dto = new 複合彩DTO(); 44 | 45 | caretaker.RestoreState(this.originator); 46 | dto.彩券號碼 = originator.彩券號碼.ToString(); 47 | dto.成語字謎 = originator.字彩; 48 | 49 | return dto; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/DecoratorController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Decorator; 2 | using hsdc.dpt.Control.DTO.Structual.Decorator.intf; 3 | using hsdc.dpt.Control.Structural.Decorator; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Web; 8 | using System.Web.Mvc; 9 | 10 | namespace WebMVC.Controllers.Structural 11 | { 12 | public class DecoratorController : Controller 13 | { 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | public JsonResult Login(string type, string userId, string password) 20 | { 21 | UserService userService = new DefaultUserService(); 22 | switch (type) 23 | { 24 | case "db": 25 | userService = new DbAuthenticationUserService(userService); 26 | break; 27 | case "ldap": 28 | userService = new AuthorityUserService(new LdapAuthenticationUserService(userService)); 29 | break; 30 | default: 31 | break; 32 | } 33 | return Json(new Result() { User = userService.Login(userId, password), Acl = userService.GetAclList(userId) }, JsonRequestBehavior.AllowGet); 34 | } 35 | } 36 | 37 | public class Result 38 | { 39 | public User User { get; set; } 40 | public List Acl { get; set; } 41 | } 42 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Mediator/Singleton.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Behavioral.Mediator; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.Mediator.Singleton 9 | { 10 | internal class Db 11 | { 12 | private static volatile Db instance; 13 | private static object syncRoot = new Object(); 14 | private List db; 15 | 16 | private Db() 17 | { 18 | db = new List(); 19 | db.Add(new Stocking() { Product = new Product() { ProductName = "Ringle's Book" }, Quantity = 0 }); 20 | db.Add(new Stocking() { Product = new Product() { ProductName = "Kenming's Book" }, Quantity = 100 }); 21 | db.Add(new Stocking() { Product = new Product() { ProductName = "GoF" }, Quantity = 2 }); 22 | } 23 | 24 | internal static Db Instance 25 | { 26 | get 27 | { 28 | if (instance == null) 29 | { 30 | lock (syncRoot) 31 | { 32 | if (instance == null) 33 | instance = new Db(); 34 | } 35 | } 36 | 37 | return instance; 38 | } 39 | } 40 | 41 | internal List GetStocking() 42 | { 43 | return this.db; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/Builder/EOfficeBuilderPattern.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Creational.Builder; 2 | using hsdc.dpt.Control.DTO.Creational.Builder.Intf; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Creational.Builder 10 | { 11 | public class EOfficeDirector 12 | { 13 | public T Construct(OfficeFormBuilder builder) 14 | { 15 | builder.BuildMaster(); 16 | builder.BuildDetails(); 17 | return builder.GetResult(); 18 | } 19 | } 20 | 21 | public class LeaveBuilder : OfficeFormBuilder 22 | { 23 | private Leave dto = null; 24 | public void BuildMaster() 25 | { 26 | dto = new Leave(); 27 | } 28 | 29 | public void BuildDetails() 30 | { 31 | } 32 | 33 | public Leave GetResult() 34 | { 35 | return dto; 36 | } 37 | } 38 | 39 | public class ApplyExpenseBuilder : OfficeFormBuilder 40 | { 41 | private ApplyExpense dto; 42 | public void BuildMaster() 43 | { 44 | dto = new ApplyExpense(); 45 | } 46 | 47 | public void BuildDetails() 48 | { 49 | dto.LineItems = new List(); 50 | } 51 | 52 | public ApplyExpense GetResult() 53 | { 54 | return dto; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/App.Config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/CommandController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.Command.Caculator; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Web; 6 | using System.Web.Mvc; 7 | 8 | namespace WebMVC.Controllers.Behavioural 9 | { 10 | public class CommandController : Controller 11 | { 12 | // GET: Command 13 | public ActionResult Index() 14 | { 15 | return View(); 16 | } 17 | 18 | public JsonResult Compute(int operand, string @operator) 19 | { 20 | ElementaryArithCaculator receiver; 21 | if (Session["calculator"] == null) 22 | { 23 | Session.Add("calculator", new ElementaryArithCaculator()); 24 | } 25 | receiver = (ElementaryArithCaculator)Session["calculator"]; 26 | (new CalculationInvoker()).Compute(receiver, @operator, operand); 27 | Session["calculator"] = receiver; 28 | return Json(receiver.GetResult(), JsonRequestBehavior.AllowGet); 29 | } 30 | 31 | public JsonResult Clear() 32 | { 33 | ElementaryArithCaculator receiver; 34 | if (Session["calculator"] == null) 35 | { 36 | Session.Add("calculator", new ElementaryArithCaculator()); 37 | } 38 | receiver = new ElementaryArithCaculator(); 39 | Session["calculator"] = receiver; 40 | return Json(receiver.GetResult(), JsonRequestBehavior.AllowGet); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Visitor/ConcreteVisitors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Visitor 8 | { 9 | /// 10 | /// The 'ConcreteVisitor' class. 11 | /// 計算公司每月固定支出費用 (針對員工薪資與辦公室開銷費用) 12 | /// 13 | public class PricingVisitor : Visitor 14 | { 15 | private int totalPrice; // 總支出費用 16 | 17 | public override void VisitEmployee(Employee emp) 18 | { 19 | totalPrice += emp.salary; // 累加所有員工薪資 20 | } 21 | 22 | public override void VisitOffice(Office office) 23 | { 24 | totalPrice += office.支出費用; // 累加所有辦公室費用 25 | } 26 | 27 | public int getTotalPrice() 28 | { 29 | return this.totalPrice; 30 | } 31 | } 32 | 33 | /// 34 | /// The 'ConcreteVisitor' class. 35 | /// 統計公司資產總數 (員工人數與辦公室數目) 36 | /// 37 | public class InventoryVisitor : Visitor 38 | { 39 | private int totalInstances; // 公司資產總數 40 | 41 | public override void VisitEmployee(Employee emp) 42 | { 43 | totalInstances++; // 累計員工人數 44 | } 45 | 46 | public override void VisitOffice(Office office) 47 | { 48 | totalInstances++; // 累計辦公室數目 49 | } 50 | 51 | public int getTotalInstances() 52 | { 53 | return totalInstances; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Composite/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "查詢員工資訊"; 5 | } 6 | 7 |

查詢員工資訊

8 | 9 | 請選擇部門: 10 | 19 |
20 | 22 | 23 | @section BodyScripts 24 | { 25 | 54 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/AbstractFactory/ListWorkItems.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "ListWorkItems"; 5 | } 6 | 7 |

列出所有工作單

8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | @foreach (var item in Model) 20 | { 21 | 22 | 23 | 24 | @switch (item.assign_type) 25 | { 26 | case hsdc.dpt.Control.DTO.Creational.AbstractFactory.AssigneeType.ORGANIZATION : 27 | 28 | break; 29 | case hsdc.dpt.Control.DTO.Creational.AbstractFactory.AssigneeType.ROLE: 30 | 31 | break; 32 | case hsdc.dpt.Control.DTO.Creational.AbstractFactory.AssigneeType.HUMAN: 33 | 34 | break; 35 | } 36 | 37 | 38 | 39 | } 40 |
編號項目名稱指派類型指派者 
@item.ID@item.item_titleORGANIZATIONROLEHUMAN@item.assignee_name@Html.ActionLink("處理", "Process", new { id = item.ID })
41 |
42 | 43 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/LDAPParticipant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 8 | { 9 | public class LDAPOrganizationParticipant : OrganizationParticipant 10 | { 11 | public override string process() 12 | { 13 | string process_result = "製造工廠: LDAPFactory\n" + 14 | "參與者類型: Organization\n" + 15 | "參與者: " + this.aggignee_name + "\n" + 16 | "執行處理工作事務...。"; 17 | 18 | return process_result; 19 | } 20 | } 21 | 22 | public class LDAPRoleParticipant : RoleParticipant 23 | { 24 | public override string process() 25 | { 26 | string process_result = "製造工廠: LDAPFactory\n" + 27 | "參與者類型: Role\n" + 28 | "參與者: " + this.aggignee_name + "\n" + 29 | "執行處理工作事務...。"; 30 | 31 | return process_result; 32 | } 33 | } 34 | 35 | public class LDAPHumanParticipant : HumanParticipant 36 | { 37 | public override string process() 38 | { 39 | string process_result = "製造工廠: LDAPFactory\n" + 40 | "參與者類型: Organization\n" + 41 | "參與者: " + this.aggignee_name + "\n" + 42 | "執行處理工作事務...。"; 43 | 44 | return process_result; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/LocalDBParticipant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 8 | { 9 | public class LocalDBOrganizationParticipant : OrganizationParticipant 10 | { 11 | public override string process() 12 | { 13 | string process_result = "製造工廠: LocalDBFactory\n" + 14 | "參與者類型: Organization\n" + 15 | "參與者: " + this.aggignee_name + "\n" + 16 | "執行處理工作事務...。"; 17 | 18 | return process_result; 19 | } 20 | } 21 | 22 | public class LocalDBRoleParticipant : RoleParticipant 23 | { 24 | public override string process() 25 | { 26 | string process_result = "製造工廠: LocalDBFactory\n" + 27 | "參與者類型: Role\n" + 28 | "參與者: " + this.aggignee_name + "\n" + 29 | "執行處理工作事務...。"; 30 | 31 | return process_result; 32 | } 33 | } 34 | 35 | public class LocalDBHumanParticipant : HumanParticipant 36 | { 37 | public override string process() 38 | { 39 | string process_result = "製造工廠:LocalDBFactory\n" + 40 | "參與者類型: Organization\n" + 41 | "參與者: " + this.aggignee_name + "\n" + 42 | "執行處理工作事務...。"; 43 | 44 | return process_result; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/Singleton/IDGeneratorSingleton.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Creational.Singleton; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Creational.Singleton 9 | { 10 | public class IDGeneratorSingleton 11 | { 12 | private static volatile IDGeneratorSingleton instance; 13 | private static object syncRoot = new Object(); 14 | 15 | private IDGeneratorSingleton() 16 | { 17 | this.IdCounter = new Dictionary(); 18 | } 19 | 20 | public static IDGeneratorSingleton Instance 21 | { 22 | get 23 | { 24 | if (instance == null) 25 | { 26 | lock (syncRoot) 27 | { 28 | if (instance == null) 29 | instance = new IDGeneratorSingleton(); 30 | } 31 | } 32 | 33 | return instance; 34 | } 35 | } 36 | 37 | private Dictionary IdCounter; 38 | 39 | public string GetNextID(string formType, DateTime formDate) 40 | { 41 | int currentId = 0; 42 | Form key = new Form() { FormType = formType, FormDateStr = formDate.ToString("yyyyMMdd") }; 43 | if (!IdCounter.TryGetValue(key, out currentId)) 44 | { 45 | IdCounter.Add(key, currentId); 46 | } 47 | currentId += 1; 48 | IdCounter[key] = currentId; 49 | return key.FormDateStr + currentId.ToString("D4"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | @ViewBag.Title - Design Pattern Demo 7 | 8 | 9 | 10 | 11 | 12 | 28 | 29 |
30 | @RenderBody() 31 |
32 |
33 |

© @DateTime.Now.Year - My ASP.NET Application

34 |
35 |
36 | 37 | 38 | 39 | @RenderSection("BodyScripts", false) 40 | 41 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Decorator/ConcreteComponent.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Decorator; 2 | using hsdc.dpt.Control.DTO.Structual.Decorator.intf; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Structural.Decorator 10 | { 11 | public class DefaultUserService : UserService 12 | { 13 | public DTO.Structual.Decorator.User Login(string userId, string password) 14 | { 15 | LoginDao dao = new LoginDao(); 16 | User u = dao.GetMemberData(userId); 17 | return u; 18 | } 19 | 20 | 21 | public List GetAclList(string userId) 22 | { 23 | List defaultAcl = new List(); 24 | defaultAcl.Add(new Function() { FunctionName = "BuilderPattern", FunctionUrl = "/Builder" }); 25 | defaultAcl.Add(new Function() { FunctionName = "PrototypePattern", FunctionUrl = "/Prototype" }); 26 | return defaultAcl; 27 | } 28 | } 29 | 30 | internal class LoginDao 31 | { 32 | internal User GetMemberData(string userId) 33 | { 34 | return Users.Where(u => u.UserID.Equals(userId)).SingleOrDefault(); 35 | } 36 | 37 | private List Users 38 | { 39 | get 40 | { 41 | List users = new List(); 42 | users.Add(new User() { UserID = "ringle", UserName = "Ringle Lai"}); 43 | users.Add(new User() { UserID = "kenming", UserName = "Kenming Wang" }); 44 | return users; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Creational/BuilderController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Creational.Builder; 2 | using hsdc.dpt.Control.DTO.Creational.Builder; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Web; 7 | using System.Web.Mvc; 8 | 9 | namespace WebMVC.Controllers.Creational 10 | { 11 | public class BuilderController : Controller 12 | { 13 | // GET: Builder 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | public ActionResult NewLeave() 20 | { 21 | LeaveBuilder builder = new LeaveBuilder(); 22 | EOfficeDirector director = new EOfficeDirector(); 23 | Leave form = director.Construct(builder); 24 | return View(form); 25 | } 26 | 27 | [HttpPost] 28 | public ActionResult SaveLeave(Leave leave) 29 | { 30 | SaveLeaveControl uco = new SaveLeaveControl(); 31 | leave = uco.Save(leave); 32 | return View("NewLeave", leave); 33 | } 34 | 35 | public ActionResult NewApplyExpense() 36 | { 37 | ApplyExpenseBuilder builder = new ApplyExpenseBuilder(); 38 | EOfficeDirector director = new EOfficeDirector(); 39 | ApplyExpense form = director.Construct(builder); 40 | return View(form); 41 | } 42 | 43 | [HttpPost] 44 | public ActionResult SaveApplyExpense(ApplyExpense expense) 45 | { 46 | SaveApplyExpenseControl uco = new SaveApplyExpenseControl(); 47 | expense = uco.Save(expense); 48 | return View("NewApplyExpense", expense); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/web.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Structural/BridgeController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Structural.Bridge; 7 | using hsdc.dpt.Control.DTO.Structual.Bridge; 8 | 9 | namespace WebMVC.Controllers.Structural 10 | { 11 | public class BridgeController : Controller 12 | { 13 | // GET: Bridge 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | [HttpPost] 20 | public ActionResult Notify() 21 | { 22 | 傳送訊息DemoControl control = new 傳送訊息DemoControl(); // new a control object. 23 | NotifyType notifytype; // declare enumernation type reference. 24 | SenderType sendertype; 25 | Message message = new Message(); // Data Transfer Object. 26 | 27 | // 判斷選擇哪一種通知類型 28 | if (Request.Form["NotifyType"].Equals("SystemNotify")) 29 | notifytype = NotifyType.System; 30 | else 31 | notifytype = NotifyType.User; 32 | 33 | // 判斷選擇哪一種傳送方式 34 | if (Request.Form["SenderType"].Equals("EmailSender")) 35 | sendertype = SenderType.Email; 36 | else 37 | sendertype = SenderType.Line; 38 | 39 | // 組裝 Message 物件的內容 40 | message.Recipient = Request.Form["txtRecipient"]; 41 | message.Subject = Request.Form["txtSubject"]; 42 | message.Body = Request.Form["txtBody"]; 43 | 44 | // 交由 control 委派處理 45 | string procResult = control.Notify(notifytype, sendertype, message); 46 | ViewData["result"] = procResult; 47 | 48 | return View(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/AbstractoryFactory/ConcreteFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Creational.AbstractoryFactory 8 | { 9 | /// 10 | /// The 'Concrete Factory' class. 11 | /// 12 | public class LDAPParticipantFactory : ParticipantFactory 13 | { 14 | public override OrganizationParticipant CreateOrganizationParticipant() 15 | { 16 | OrganizationParticipant op = new LDAPOrganizationParticipant(); 17 | return op; 18 | } 19 | 20 | public override RoleParticipant CreateRoleParticipant() 21 | { 22 | RoleParticipant rp = new LDAPRoleParticipant(); 23 | return rp; 24 | } 25 | 26 | public override HumanParticipant CreateHumanParticipant() 27 | { 28 | HumanParticipant hp = new LDAPHumanParticipant(); 29 | return hp; 30 | } 31 | } 32 | 33 | /// 34 | /// The 'Concrete Factory' class. 35 | /// 36 | public class LocalDBParticipantFactory : ParticipantFactory 37 | { 38 | public override OrganizationParticipant CreateOrganizationParticipant() 39 | { 40 | OrganizationParticipant op = new LocalDBOrganizationParticipant(); 41 | return op; 42 | } 43 | 44 | public override RoleParticipant CreateRoleParticipant() 45 | { 46 | RoleParticipant rp = new LocalDBRoleParticipant(); 47 | return rp; 48 | } 49 | 50 | public override HumanParticipant CreateHumanParticipant() 51 | { 52 | HumanParticipant hp = new LocalDBHumanParticipant(); 53 | return hp; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Flyweight/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "地址輸入"; 5 | } 6 | 7 |

輸入地址

8 | 9 | 10 | 11 | 12 | 22 | 23 |
請選擇城市別: 13 | 21 |
24 |
25 |
26 | 27 | @section BodyScripts 28 | { 29 | 58 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/CoR/procDonateControl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Behavioral.CoR; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.CoR 9 | { 10 | /// 11 | /// The 'Client' class 12 | /// 13 | public class procDonateControl 14 | { 15 | public string procDonation(Donation donate) 16 | { 17 | string procResult = ""; 18 | DonateHandler handler; 19 | 20 | handler = this.SetupHandlers(); // Setup all the donation handlers and return the first hander. 21 | try 22 | { 23 | procResult = handler.handleDonate(donate); 24 | } 25 | catch (DonateExceedException de) 26 | { 27 | procResult = de.Message; 28 | } 29 | catch (NullHanderException ne) 30 | { 31 | procResult = ne.Message; 32 | } 33 | 34 | return procResult; 35 | } 36 | 37 | /// 38 | /// 設定 Donation Concrete Handlers whose extend DonateHandler. 39 | /// 40 | /// The first handler instance. 41 | private DonateHandler SetupHandlers() 42 | { 43 | DonateHandler people, enterprise, organization; 44 | 45 | // new all the concreate handlers 46 | people = new 個人Handler(); 47 | enterprise = new 營利事業Handler(); 48 | organization = new 人民團體Handler(); 49 | 50 | // Set the Handler's Successor 51 | people.Successor = enterprise; 52 | enterprise.Successor = organization; 53 | 54 | return people; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/CoRController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Behavioral.CoR; 7 | using hsdc.dpt.Control.DTO.Behavioral.CoR; 8 | 9 | namespace WebMVC.Controllers.Behavioral 10 | { 11 | public class CoRController : Controller 12 | { 13 | // GET: CoR 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | [HttpPost] 20 | public ActionResult handleDonate(string TxtName, string SelDonateType, string TxtDonateMoney, string TxtExtInfo) 21 | { 22 | procDonateControl control = new procDonateControl(); // new a domain controller 23 | Donation donate = new Donation(); // new a Donation DTO Class. 24 | DonateType dtype; 25 | 26 | switch (SelDonateType) 27 | { 28 | case ("個人") : 29 | dtype = DonateType.個人; 30 | break; 31 | case ("營利事業"): 32 | dtype = DonateType.營利事業; 33 | break; 34 | case ("人民團體"): 35 | dtype = DonateType.人民團體; 36 | break; 37 | default : 38 | dtype = DonateType.UNKNOWN; 39 | break; 40 | } 41 | 42 | // put the form's values into donate instance. 43 | donate.name = TxtName; 44 | donate.type = dtype; 45 | donate.money = Convert.ToInt32(TxtDonateMoney); 46 | donate.ext_info = TxtExtInfo; 47 | 48 | // get the process result. 49 | ViewData["ProcssResult"] = control.procDonation(donate); 50 | 51 | return View("Index"); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/TemplateMethod/InsurancePolicyTransationTemplateMethodPattern.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Behavioral.TemplateMethod.Singleton; 2 | using hsdc.dpt.Control.DTO.Behavioral.TemplateMethod; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Behavioral.TemplateMethod.InsurancePolicy 10 | { 11 | public abstract class AbstractPolicyChangeTemplate 12 | { 13 | public List GetTransactionList() 14 | { 15 | List trans = new List(); 16 | trans.Add(new PolicyTransaction() {CanEdit = true, Policies = new List()}); 17 | trans.Add(new PolicyTransaction() {CanEdit = false, Policies = new List()}); 18 | foreach(var p in Db.Instance.GetPolicies()) 19 | trans.Where(t => t.CanEdit == CanEdit(p)).SingleOrDefault().Policies.Add(p); 20 | return trans; 21 | } 22 | 23 | protected abstract bool CanEdit(Policy p); 24 | } 25 | 26 | public class PolicyLoanTransction : AbstractPolicyChangeTemplate 27 | { 28 | 29 | protected override bool CanEdit(Policy p) 30 | { 31 | return p.PolicyType == PolicyType.Normal; 32 | } 33 | } 34 | 35 | public class ChangeInsuredNameTransaction : AbstractPolicyChangeTemplate 36 | { 37 | 38 | protected override bool CanEdit(Policy p) 39 | { 40 | return p.PolicyType != PolicyType.HasClaimed; 41 | } 42 | } 43 | 44 | public class ChangeInsuredNameTransaction2 : AbstractPolicyChangeTemplate 45 | { 46 | 47 | protected override bool CanEdit(Policy p) 48 | { 49 | return p.PolicyType != PolicyType.InAprrove; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Facade/買烏龜Control.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using hsdc.dpt.Control.DTO; 8 | 9 | namespace hsdc.dpt.Control.Structural.Facade 10 | { 11 | public class 買烏龜Control 12 | { 13 | // Constructor 14 | public 買烏龜Control() 15 | { 16 | } 17 | 18 | public List<烏龜商品> list烏龜清單() 19 | { 20 | return this.getItems(); 21 | } 22 | 23 | public ShoppingCart 確認訂購資訊(ShoppingCart cart) 24 | { 25 | 烏龜BMO bmo = new 烏龜BMO(); 26 | cart = bmo.proc訂購資訊(cart); 27 | 28 | return cart; 29 | } 30 | 31 | public bool Save訂購交易(ShoppingCart cart) 32 | { 33 | // Todo: 34 | // 交由 DAO (Data Access Object) 物件,並透 EF (Entity Framework)將本次訂購商品交易儲存至資料庫 35 | 36 | return true; 37 | } 38 | 39 | 40 | /* 41 | * 實作時,會交由如 DAO (Data Access Object) 物件,並透過 EF (Entity Framework) 從資料庫系統取得資料。 42 | */ 43 | private List<烏龜商品> getItems() 44 | { 45 | List<烏龜商品> list = new List<烏龜商品>(); 46 | 烏龜商品 item1, item2, item3; 47 | 48 | item1 = new 烏龜商品(); 49 | item1.id = "001"; 50 | item1.name = "粉圓龜"; 51 | item1.price = 3000; 52 | 53 | item2 = new 烏龜商品(); 54 | item2.id = "002"; 55 | item2.name = "金錢龜"; 56 | item2.price = 6000; 57 | 58 | item3 = new 烏龜商品(); 59 | item3.id = "003"; 60 | item3.name = "象龜"; 61 | item3.price = 120000; 62 | 63 | list.Add(item1); 64 | list.Add(item2); 65 | list.Add(item3); 66 | 67 | return list; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Utility/EnumUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Utility 10 | { 11 | internal class EnumUtils 12 | { 13 | public static string stringValueOf(Enum value) 14 | { 15 | FieldInfo fi = value.GetType().GetField(value.ToString()); 16 | if (fi == null) 17 | return string.Empty; 18 | DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 19 | if (attributes.Length > 0) 20 | { 21 | return attributes[0].Description; 22 | } 23 | else 24 | { 25 | return string.Empty; 26 | } 27 | } 28 | 29 | public static object enumValueOf(string value, Type enumType) 30 | { 31 | string[] names = Enum.GetNames(enumType); 32 | foreach (string name in names) 33 | { 34 | if (stringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value)) 35 | { 36 | return Enum.Parse(enumType, name); 37 | } 38 | } 39 | 40 | throw new ArgumentException("The string is not a description or value of the specified enum."); 41 | } 42 | 43 | public static IEnumerable MaskToList(Enum mask) 44 | { 45 | if (typeof(T).IsSubclassOf(typeof(Enum)) == false) 46 | throw new ArgumentException(); 47 | 48 | return Enum.GetValues(typeof(T)) 49 | .Cast() 50 | .Where(m => mask.HasFlag(m)) 51 | .Cast(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/TemplateMethod/Singleton.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Behavioral.TemplateMethod; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Behavioral.TemplateMethod.Singleton 9 | { 10 | internal class Db 11 | { 12 | private static volatile Db instance; 13 | private static object syncRoot = new Object(); 14 | private List db; 15 | 16 | private Db() 17 | { 18 | db = new List(); 19 | db.Add(new Policy() { PolicyNumber = "01010001", PolicyName = "健康險1", PolicyOwnerName = "Ringle", PolicyInsuredName = "Ringle", PolicyType = PolicyType.Normal }); 20 | db.Add(new Policy() { PolicyNumber = "01010002", PolicyName = "健康險2", PolicyOwnerName = "Ringle", PolicyInsuredName = "Ringle", PolicyType = PolicyType.HasClaimed }); 21 | db.Add(new Policy() { PolicyNumber = "01010003", PolicyName = "壽險1", PolicyOwnerName = "Ringle", PolicyInsuredName = "Test", PolicyType = PolicyType.InAprrove }); 22 | db.Add(new Policy() { PolicyNumber = "01010004", PolicyName = "壽險2", PolicyOwnerName = "Ringle", PolicyInsuredName = "Ajewl", PolicyType = PolicyType.Normal }); 23 | db.Add(new Policy() { PolicyNumber = "01010005", PolicyName = "壽險3", PolicyOwnerName = "Ringle", PolicyInsuredName = "Ringle", PolicyType = PolicyType.InAprrove }); 24 | } 25 | 26 | internal static Db Instance 27 | { 28 | get 29 | { 30 | if (instance == null) 31 | { 32 | lock (syncRoot) 33 | { 34 | if (instance == null) 35 | instance = new Db(); 36 | } 37 | } 38 | 39 | return instance; 40 | } 41 | } 42 | 43 | internal List GetPolicies() 44 | { 45 | return this.db; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Design Patterns Demo Solution.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 2013 for Web 4 | VisualStudioVersion = 12.0.30723.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebMVC", "WebMVC\WebMVC.csproj", "{183A1FFB-959B-41EE-8F54-B4326416D3AF}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Control", "Control\Control.csproj", "{D3CF315B-8512-49C8-AC29-B81447E0260F}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication", "ConsoleApplication\ConsoleApplication.csproj", "{2BE70449-EB14-4BD2-87F2-06CE79BDBE55}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {183A1FFB-959B-41EE-8F54-B4326416D3AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {183A1FFB-959B-41EE-8F54-B4326416D3AF}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {183A1FFB-959B-41EE-8F54-B4326416D3AF}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {183A1FFB-959B-41EE-8F54-B4326416D3AF}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {D3CF315B-8512-49C8-AC29-B81447E0260F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {D3CF315B-8512-49C8-AC29-B81447E0260F}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {D3CF315B-8512-49C8-AC29-B81447E0260F}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {D3CF315B-8512-49C8-AC29-B81447E0260F}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {2BE70449-EB14-4BD2-87F2-06CE79BDBE55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {2BE70449-EB14-4BD2-87F2-06CE79BDBE55}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {2BE70449-EB14-4BD2-87F2-06CE79BDBE55}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {2BE70449-EB14-4BD2-87F2-06CE79BDBE55}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | EndGlobal 35 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Hubs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using Microsoft.AspNet.SignalR; 6 | using hsdc.dpt.Control.Behavioral.Mediator.Intf; 7 | using hsdc.dpt.Control.Behavioral.Mediator; 8 | using hsdc.dpt.Control.DTO.Behavioral.Mediator; 9 | 10 | namespace WebMVC.Controllers.Behavioural 11 | { 12 | public class OrderHub : Hub, NotifierProxy 13 | { 14 | private const string GROUP = "ORDER"; 15 | 16 | public override System.Threading.Tasks.Task OnConnected() 17 | { 18 | StockingMediator.Instance.OrderColleagues.Add(new OrderColleague(StockingMediator.Instance, this)); 19 | Groups.Add(Context.ConnectionId, GROUP); 20 | return base.OnConnected(); 21 | } 22 | 23 | public void Notify(string prodName) 24 | { 25 | Clients.Group(GROUP).Notify(prodName); 26 | } 27 | 28 | public void Send(string prodName) 29 | { 30 | StockingMediator.Instance.OrderColleagues[0].Send(new Order() { Product = new Product() { ProductName = prodName } }); 31 | if ((new StockingUco()).CheckQunatity(prodName)) 32 | Clients.All.AddSellOut(prodName); 33 | } 34 | } 35 | 36 | public class StockInHub : Hub, NotifierProxy 37 | { 38 | private const string GROUP = "STOCKING"; 39 | 40 | public override System.Threading.Tasks.Task OnConnected() 41 | { 42 | StockingMediator.Instance.StockInColleagues.Add(new StockInColleague(StockingMediator.Instance, this)); 43 | Groups.Add(Context.ConnectionId, GROUP); 44 | return base.OnConnected(); 45 | } 46 | 47 | public void Notify(string prodName) 48 | { 49 | Clients.Group(GROUP).Notify(prodName); 50 | } 51 | 52 | public void Send(string prodName, int qty) 53 | { 54 | StockingMediator.Instance.StockInColleagues[0].Send(new StockIn() { Product = new Product() { ProductName = prodName }, Quantity = qty }); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Mediator/StockIn.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "入庫"; 5 | } 6 | 7 |

入庫

8 | 9 |

10 | 請選擇產品: 11 | 20 |

21 |

請輸入數量:

22 | 23 | 24 |
25 | 26 | 27 | @section BodyScripts 28 | { 29 | 30 | 31 | 32 | 33 | 60 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/Prototype/RingleFramework/MaintainPurchaseConrol.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Creational.Prototype.RingleFramework.InternalDto; 2 | using hsdc.dpt.Control.DTO.Creational.Prototype.RingleFramework.Intf; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace hsdc.dpt.Control.Creational.Prototype.RingleFramework 10 | { 11 | public class MaintainPurchaseConrol 12 | { 13 | public List GetPurchaseLists() 14 | { 15 | return InitialData(); 16 | } 17 | 18 | private List InitialData() 19 | { 20 | List poList = new List(); 21 | List items = new List(); 22 | items.Add(new PurchaseOrderLineItemDto() { ProductNameInternal= "烏龜", QuantityInternal = 2, PriceInternal = 12 }); 23 | items.Add(new PurchaseOrderLineItemDto() { ProductNameInternal = "小鳥", QuantityInternal = 1, PriceInternal = (decimal)15.2 }); 24 | poList.Add(new PurchaseOrderDto() { PoNumberInternal = "PO010001", CustomerNameInternal = "Kenming", LineItemsInternal = items }); 25 | items = new List(); 26 | items.Add(new PurchaseOrderLineItemDto() { ProductNameInternal = "烏龜", QuantityInternal = 1, PriceInternal = (decimal)11.5 }); 27 | poList.Add(new PurchaseOrderDto() { PoNumberInternal = "PO010002", CustomerNameInternal = "Ringle", LineItemsInternal = items }); 28 | return poList; 29 | } 30 | 31 | public hsdc.dpt.Control.DTO.Creational.Prototype.RingleFramework.Intf.Prototype NewPurchaseOrder() 32 | { 33 | return new PurchaseOrderDto(); 34 | } 35 | 36 | public List Save(PurchaseOrder po) 37 | { 38 | List poList = InitialData(); 39 | po.PoNumber = "PO010003"; 40 | poList.Add(po); 41 | return poList; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Visitor/ConcreteElements.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Visitor 8 | { 9 | /// 10 | /// The 'ConcreteElement' class. 11 | /// 12 | public class Employee : IElement 13 | { 14 | public string name { set; get; } // Employee's name 15 | public int salary { set; get; } // 應領薪資 16 | //public int vocationDays { set; get; } // 休假天數 17 | 18 | public Employee() { } // default Constructor 19 | 20 | // Constructor 21 | public Employee(string _name, int _salary) 22 | { 23 | this.name = _name; 24 | this.salary = _salary; 25 | //this.vocationDays = vDays; 26 | } 27 | 28 | public void Accept(Visitor visitor) 29 | { 30 | visitor.VisitEmployee(this); 31 | } 32 | } 33 | 34 | /// 35 | /// The 'ConcreteElement' class. 36 | /// 37 | public class Clerk : Employee 38 | { 39 | // Constructor 40 | public Clerk(string _name, int _salary) 41 | : base(_name, _salary) 42 | {} 43 | } 44 | 45 | /// 46 | /// The 'ConcreteElement' class. 47 | /// 48 | public class Manager : Employee 49 | { 50 | // Constructor 51 | public Manager(string _name, int _salary) 52 | : base(_name, _salary) 53 | {} 54 | } 55 | 56 | public class Office : IElement 57 | { 58 | // 包括租金、水電費、辦公用具等採購費用 59 | // 此為簡化;實作應有 calc支出費用() 計算邏輯函數 60 | public int 支出費用 { set; get; } 61 | public string name {set;get;} // 辦公室名稱 62 | 63 | // Constructor 64 | public Office(string _name, int expense) 65 | { 66 | this.支出費用 = expense; 67 | this.name = _name; 68 | } 69 | 70 | // realize the 'Accept()' operation. 71 | public void Accept(Visitor visitor) 72 | { 73 | visitor.VisitOffice(this); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Creational/Prototype/RingleERP/MaintainPurchaseOrderControl.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Creational.Prototype.RingleERP.InternalDto; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace hsdc.dpt.Control.Creational.Prototype.RingleERP 9 | { 10 | public class MaintainPurchaseOrderControl 11 | { 12 | public List GetPurchaseLists() 13 | { 14 | return InitialData(); 15 | } 16 | 17 | private List InitialData() 18 | { 19 | List poList = new List(); 20 | List items = new List(); 21 | items.Add(new PurchaseOrderLineItemDto() { ProductName = "烏龜", Quantity = 2, Price = 12 }); 22 | items.Add(new PurchaseOrderLineItemDto() { ProductName = "小鳥", Quantity = 1, Price = (decimal)15.2 }); 23 | poList.Add(new PurchaseOrderDto() { PoNumber = "PO010001", CustomerName = "Kenming", LineItems = items }); 24 | items = new List(); 25 | items.Add(new PurchaseOrderLineItemDto() { ProductName = "烏龜", Quantity = 1, Price = (decimal)11.5 }); 26 | poList.Add(new PurchaseOrderDto() { PoNumber = "PO010002", CustomerName = "Ringle", LineItems = items }); 27 | return poList; 28 | } 29 | 30 | public PurchaseOrderDto CopyPo(String sourcePoNumber) 31 | { 32 | PurchaseOrderDto source = InitialData().Where(p => p.PoNumber.Equals(sourcePoNumber)).SingleOrDefault(); 33 | PurchaseOrderDto target = null; 34 | if (source != null) 35 | { 36 | target = source.Clone(); 37 | target.PoNumber = string.Empty; 38 | } 39 | return target; 40 | } 41 | 42 | public List Save(PurchaseOrderDto po) 43 | { 44 | List poList = InitialData(); 45 | po.PoNumber = "PO010003"; 46 | poList.Add(po); 47 | return poList; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Bridge/傳送訊息DemoControl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.DTO.Structual.Bridge; 7 | 8 | namespace hsdc.dpt.Control.Structural.Bridge 9 | { 10 | public class 傳送訊息DemoControl 11 | { 12 | private Notifier notifier; 13 | private IMessageSender mailSender; 14 | private IMessageSender lineSender; 15 | 16 | public 傳送訊息DemoControl() 17 | { 18 | // 實務會設計如 Factory 來產出所有的 Implementor instance 19 | mailSender = new EmailSender(); 20 | lineSender = new LineSender(); 21 | } 22 | 23 | /// 24 | /// 處理傳遞訊息的控制邏輯 25 | /// 26 | /// enum: System, User 27 | /// enum: Email, Line 28 | /// 訊息物件 29 | /// 30 | public string Notify(NotifyType notifytype, SenderType sendertype, Message message) 31 | { 32 | string procResult = ""; 33 | 34 | // 判斷需產出哪一個 RefinedAbstraction 的子類別物件 35 | switch (notifytype) 36 | { 37 | case NotifyType.System : 38 | notifier = new SystemNotifier(); 39 | break; 40 | case NotifyType.User : 41 | notifier = new UserNotifier(); 42 | break; 43 | } 44 | 45 | // 判斷需哪個實作 MessageSender 介面的實作物件來服務 46 | // 實務上會設計如 List or Hastable 集合儲存這些實作物件,並透過 Key 來取得相對應的實作物件 47 | switch (sendertype) 48 | { 49 | case SenderType.Email : 50 | notifier.sender = mailSender; 51 | break; 52 | case SenderType.Line : 53 | notifier.sender = lineSender; 54 | break; 55 | } 56 | 57 | // 回傳執行結果,藉此觀察使用哪一個 Notifier 子類別物件與 MessengSender 物件 58 | procResult = notifier.Notify(message); 59 | 60 | return procResult; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Memento/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model hsdc.dpt.Control.DTO.Behavioral.Memento.複合彩DTO 2 | 3 | @{ 4 | ViewBag.Title = "Index"; 5 | IEnumerable 成語選項 = new List{ 6 | new SelectListItem() { Value = "晨星寥落" , Text = "晨星寥落", Selected = false}, 7 | new SelectListItem() { Value = "明珠彈雀" , Text = "明珠彈雀", Selected = false}, 8 | new SelectListItem() { Value = "尾生之信" , Text = "尾生之信", Selected = false}, 9 | new SelectListItem() { Value = "遺大投艱" , Text = "遺大投艱", Selected = false}, 10 | new SelectListItem() { Value = "人心不古" , Text = "人心不古", Selected = false} 11 | }; 12 | if ((Model != null) && (!string.IsNullOrEmpty(Model.成語字謎))) 13 | { 14 | 成語選項.Where(w => w.Value.Equals(Model.成語字謎)).SingleOrDefault().Selected = true; 15 | } 16 | } 17 | 18 |

Memento Pattern

19 |

複合彩選號服務系統

20 |
21 | 22 |
23 | 24 | @{ if (IsPost) 25 | { 26 | @Html.TextBoxFor(model => model.彩券號碼, new { Value = @Model.彩券號碼 }) 27 | } 28 | else 29 | { 30 | @Html.TextBoxFor(model => model.彩券號碼) 31 | } 32 | } 33 |
34 |
35 | 36 | 37 | @Html.DropDownListFor(model => model.成語字謎, new SelectList( 38 | 成語選項, 39 | "Value", 40 | "Text" 41 | )) 42 | 43 | 44 |
45 |

46 |
47 | 48 | 49 | 50 | 51 |
52 |
53 | 54 | @{ if (IsPost) 55 | { 56 |

57 |
58 |

@MvcHtmlString.Create(Html.Encode(ViewData["Result"]))

59 |
60 | } 61 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Creational/PrototypeController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Creational.Prototype.RingleERP; 2 | using hsdc.dpt.Control.Creational.Prototype.RingleFramework; 3 | using hsdc.dpt.Control.DTO.Creational.Prototype.RingleERP.InternalDto; 4 | using hsdc.dpt.Control.DTO.Creational.Prototype.RingleFramework.Intf; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Web; 9 | using System.Web.Mvc; 10 | 11 | namespace WebMVC.Controllers.Structural 12 | { 13 | public class PrototypeController : Controller 14 | { 15 | // GET: Prototype 16 | public ActionResult Index() 17 | { 18 | return View(); 19 | } 20 | 21 | #region Case1 22 | public ActionResult RingleFramework() 23 | { 24 | MaintainPurchaseConrol uco = new MaintainPurchaseConrol(); 25 | return View(uco.GetPurchaseLists()); 26 | } 27 | 28 | public ActionResult RingleFrameworkAdd() 29 | { 30 | return View(); 31 | } 32 | 33 | public ActionResult RFAddPo(string CustName) 34 | { 35 | MaintainPurchaseConrol uco = new MaintainPurchaseConrol(); 36 | PurchaseOrder newPo = uco.NewPurchaseOrder().Clone(); 37 | newPo.CustomerName = CustName; 38 | List poList = uco.Save(newPo); 39 | return View("RingleFramework", poList); 40 | } 41 | #endregion 42 | 43 | #region Case2 44 | public ActionResult RingleERP() 45 | { 46 | MaintainPurchaseOrderControl uco = new MaintainPurchaseOrderControl(); 47 | return View(uco.GetPurchaseLists()); 48 | } 49 | 50 | public ActionResult RingleERPCopy(string poNumber) 51 | { 52 | MaintainPurchaseOrderControl uco = new MaintainPurchaseOrderControl(); 53 | PurchaseOrderDto dto = uco.CopyPo(poNumber); 54 | return View(dto); 55 | } 56 | 57 | public ActionResult REAddPo(PurchaseOrderDto po) 58 | { 59 | MaintainPurchaseOrderControl uco = new MaintainPurchaseOrderControl(); 60 | List poList = uco.Save(po); 61 | return View("RingleERP", poList); 62 | } 63 | #endregion 64 | } 65 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/Command/CaculatorCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.Command.Caculator 8 | { 9 | public class CalculationInvoker 10 | { 11 | private List commands = new List(); 12 | public void Compute(object receiver, string @operator, int operand) 13 | { 14 | // Create command operation and execute it 15 | Command command = new ElementaryArithCommand( 16 | receiver, @operator, operand); 17 | command.Execute(); 18 | commands.Add(command); 19 | } 20 | } 21 | 22 | public abstract class Command 23 | { 24 | protected string Operator; 25 | protected int Operand; 26 | public abstract void Execute(); 27 | public Command(object receiver, string Operator, int Operand) 28 | { 29 | this.Operator = Operator; 30 | this.Operand = Operand; 31 | } 32 | } 33 | 34 | public class ElementaryArithCommand : Command 35 | { 36 | private ElementaryArithCaculator _caculator; 37 | public ElementaryArithCommand(object receiver, string Operator, int Operand) 38 | : base(receiver, Operator, Operand) 39 | { 40 | if (receiver.GetType() == typeof(ElementaryArithCaculator)) 41 | this._caculator = (ElementaryArithCaculator)receiver; 42 | } 43 | public override void Execute() 44 | { 45 | if (this._caculator != null) 46 | this._caculator.Operation(Operator, Operand); 47 | } 48 | } 49 | 50 | [Serializable] 51 | public class ElementaryArithCaculator 52 | { 53 | private int _curr = 0; 54 | 55 | public void Operation(string @operator, int operand) 56 | { 57 | switch (@operator) 58 | { 59 | case "+": _curr += operand; break; 60 | case "-": _curr -= operand; break; 61 | case "*": _curr *= operand; break; 62 | case "/": _curr /= operand; break; 63 | } 64 | } 65 | 66 | public int GetResult() 67 | { 68 | return _curr; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Behavioral/State/2_Refactoring/ConcreteStateClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.Behavioral.State 8 | { 9 | // 實作在 'NEW_ORDER 狀態時所需處理的行為 10 | public class NewOrderState : IOrderState 11 | { 12 | public string processSomething(OrderStateContext context) 13 | { 14 | // 回傳處理過程資訊 15 | string procResult = "目前狀態:新訂單成立中‧‧‧\n" + 16 | "紀錄本關卡的處理過程‧‧‧。\n"; 17 | //current_state = ORDER_STATE.PAID; // 設定下一關的狀態 18 | context.setState(new PaidOrderState()); 19 | 20 | procResult = procResult + "轉移至下一關卡狀態為:已付款"; 21 | 22 | return procResult; 23 | } 24 | } 25 | 26 | // 實作在 'PAID_ORDER 狀態時所需處理的行為 27 | public class PaidOrderState : IOrderState 28 | { 29 | public string processSomething(OrderStateContext context) 30 | { 31 | // 回傳處理過程資訊 32 | string procResult = "目前狀態:新訂單\n" + 33 | "紀錄本關卡的處理過程‧‧‧。\n"; 34 | //current_state = ORDER_STATE.SHIPPED; // 設定下一關的狀態 35 | context.setState(new ShippedOrderState()); 36 | procResult = procResult + "轉移至下一關卡狀態為:已出貨\n\n"; 37 | 38 | return procResult; 39 | } 40 | } 41 | 42 | // 實作在 'SHIPPED_ORDER 狀態時所需處理的行為 43 | public class ShippedOrderState : IOrderState 44 | { 45 | public string processSomething(OrderStateContext context) 46 | { 47 | // 回傳處理過程資訊 48 | string procResult = "目前狀態:已付款\n" + 49 | "紀錄本關卡的處理過程‧‧‧。\n"; 50 | //current_state = ORDER_STATE.FINISHED; // 設定下一關的狀態 51 | context.setState(new FINISHEDOrderState()); 52 | procResult = procResult + "轉移至下一關卡狀態為:已完成\n\n"; 53 | 54 | return procResult; 55 | } 56 | } 57 | 58 | // 實作在 'FINISHED_ORDER 狀態時所需處理的行為 59 | public class FINISHEDOrderState : IOrderState 60 | { 61 | public string processSomething(OrderStateContext context) 62 | { 63 | // 回傳處理過程資訊 64 | string procResult = "目前狀態:已完成訂單\n" + 65 | "紀錄本關卡的處理過程‧‧‧。\n"; 66 | procResult = procResult + "實作上應該把已完成訂單的狀態設定為關閉 (CLOSED)\n\n"; 67 | 68 | return procResult; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/Structural/Flyweight/FlyweightPattern.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.DTO.Structual.Flyweight; 2 | using hsdc.dpt.Control.DTO.Structual.Flyweight.Intf; 3 | using System; 4 | using System.Collections.Concurrent; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace hsdc.dpt.Control.Structural.Flyweight 11 | { 12 | public class CodeFactory 13 | { 14 | private static CodeFactory singleton; 15 | private static object syncRoot = new Object(); 16 | private ConcurrentDictionary flyweights = new ConcurrentDictionary(); 17 | 18 | private CodeFactory() { } 19 | 20 | public static CodeFactory GetInstance() 21 | { 22 | if (singleton == null) 23 | { 24 | lock (syncRoot) 25 | { 26 | if (singleton == null) 27 | singleton = new CodeFactory(); 28 | } 29 | } 30 | 31 | return singleton; 32 | } 33 | 34 | public Code GetCode(string type) 35 | { 36 | Code code = null; 37 | switch(type) 38 | { 39 | case "CITY": 40 | code = flyweights.GetOrAdd(type, c => new CachedCode(type)); 41 | break; 42 | case "AREA": 43 | code = new UncachedCode(type); 44 | break; 45 | } 46 | return code; 47 | } 48 | } 49 | public class CachedCode : Code 50 | { 51 | private Dao.CodeDao dao = new Dao.CodeDao(); 52 | private List codes; 53 | public CachedCode(string CodeType) 54 | { 55 | this.codes = dao.query(CodeType); 56 | } 57 | 58 | public List GetCodeList(string parentValue = "") 59 | { 60 | return this.codes; 61 | } 62 | } 63 | 64 | public class UncachedCode : Code 65 | { 66 | private Dao.CodeDao dao = new Dao.CodeDao(); 67 | private string CodeType = ""; 68 | public UncachedCode(string CodeType) 69 | { 70 | this.CodeType = CodeType; 71 | } 72 | public List GetCodeList(string parentValue = "") 73 | { 74 | return dao.query(CodeType, parentValue); 75 | } 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Creational/FactoryMethodController.cs: -------------------------------------------------------------------------------- 1 | using hsdc.dpt.Control.Creational.FactoryMethod.Erp; 2 | using hsdc.dpt.Control.DTO.Creational.FactoryMethod; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Web; 7 | using System.Web.Mvc; 8 | 9 | namespace WebMVC.Controllers.Creational 10 | { 11 | public class FactoryMethodController : Controller 12 | { 13 | // GET: FactoryMethod 14 | public ActionResult Index() 15 | { 16 | return View(); 17 | } 18 | 19 | public ActionResult MaintainCustomer() 20 | { 21 | MaintainCustomerUco uco = new MaintainCustomerUco(); 22 | List customers = uco.ListAll(); 23 | return View("MaintainCustomer", customers); 24 | } 25 | public ActionResult CreateCustomer() 26 | { 27 | return MaintainCustomer(); 28 | } 29 | 30 | public ActionResult EditCustomer(string id) 31 | { 32 | return MaintainCustomer(); 33 | } 34 | 35 | public ActionResult DeleteCustomer(string id) 36 | { 37 | return MaintainCustomer(); 38 | } 39 | 40 | public ActionResult MaintainSupplier() 41 | { 42 | MaintainSupplierUco uco = new MaintainSupplierUco(); 43 | List suppliers = uco.ListAll(); 44 | return View("MaintainSupplier", suppliers); 45 | } 46 | public ActionResult CreateSupplier() 47 | { 48 | return MaintainSupplier(); 49 | } 50 | 51 | public ActionResult EditSupplier(string id) 52 | { 53 | return MaintainSupplier(); 54 | } 55 | 56 | public ActionResult DeleteSupplier(string id) 57 | { 58 | return MaintainSupplier(); 59 | } 60 | 61 | public ActionResult MaintainProduct() 62 | { 63 | MaintainProductUco uco = new MaintainProductUco(); 64 | List products = uco.ListAll(); 65 | return View("MaintainProduct", products); 66 | } 67 | public ActionResult CreateProduct() 68 | { 69 | return MaintainProduct(); 70 | } 71 | 72 | public ActionResult EditProduct(string id) 73 | { 74 | return MaintainProduct(); 75 | } 76 | 77 | public ActionResult DeleteProduct(string id) 78 | { 79 | return MaintainProduct(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Decorator/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "使用者登入"; 4 | } 5 | 6 |

使用者登入

7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
使用者代號
使用者密碼
19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 |
28 |
29 |
30 |
31 |
32 | @section BodyScripts 33 | { 34 | 78 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Controllers/Behavioral/MementoController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using hsdc.dpt.Control.Behavioral.Memento; 7 | using hsdc.dpt.Control.DTO.Behavioral.Memento; 8 | 9 | namespace WebMVC.Controllers.Behavioral 10 | { 11 | public class MementoController : Controller 12 | { 13 | 管理博彩Control control; // declare a domain controller. 14 | 15 | // GET: Memento 16 | public ActionResult Index() 17 | { 18 | if (Session["ManageGambleControl"] == null) 19 | { 20 | control = new 管理博彩Control(); // new a domain control instance. 21 | Session["ManageGambleControl"] = control; 22 | } 23 | else 24 | Session["ManageGambleControl"] = control; // hold the control object as stateful. 25 | return View(); 26 | } 27 | 28 | public ActionResult SelectLotteService(string Submit, 複合彩DTO dto) 29 | { 30 | switch (Submit) 31 | { 32 | case "自動選號": 33 | return this.DoAutoSelect(dto); 34 | case "暫存本組複合彩": 35 | return this.DoSave(dto); 36 | case "回復所選複合彩": 37 | return this.DoRestore(); 38 | case "列印選號": 39 | break; 40 | } 41 | return View("Index", dto); 42 | } 43 | 44 | // 選擇自動選號 45 | private ActionResult DoAutoSelect(複合彩DTO dto) 46 | { 47 | // get the control object from the Session Object. 48 | control = (管理博彩Control)Session["ManageGambleControl"]; 49 | 50 | dto.彩券號碼 = control.選號().ToString(); 51 | ViewData["Result"] = "已自動選號。"; 52 | 53 | return View("Index", dto); 54 | } 55 | 56 | // 選擇自動選號 57 | private ActionResult DoSave(複合彩DTO dto) 58 | { 59 | // get the control object from the Session Object. 60 | control = (管理博彩Control)Session["ManageGambleControl"]; 61 | 62 | control.Save選號(dto); 63 | ViewData["Result"] = "本複合彩組合已暫存。"; 64 | 65 | return View("Index", dto); 66 | } 67 | 68 | // 選擇自動選號 69 | private ActionResult DoRestore() 70 | { 71 | // get the control object from the Session Object. 72 | control = (管理博彩Control)Session["ManageGambleControl"]; 73 | 複合彩DTO dto; 74 | 75 | dto = control.還原選號(); 76 | ModelState.Remove("成語字謎"); 77 | ViewData["Result"] = "已還原原來所選擇複合彩組合。"; 78 | 79 | return View("Index", dto); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/Control/DTO/Creational/Prototype/RingleERP/PrototypeDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace hsdc.dpt.Control.DTO.Creational.Prototype.RingleERP.InternalDto 8 | { 9 | public class PurchaseOrderDto : Prototype.RingleERP.Intf.PorotoType 10 | { 11 | 12 | public PurchaseOrderDto() 13 | { 14 | if (LineItems == null) 15 | LineItems = new List(); 16 | } 17 | 18 | public PurchaseOrderDto Clone() 19 | { 20 | PurchaseOrderDto newDto = (PurchaseOrderDto)this.MemberwiseClone(); 21 | return newDto; 22 | } 23 | 24 | public string PoNumber 25 | { 26 | get; 27 | set; 28 | } 29 | 30 | public string CustomerName 31 | { 32 | get; 33 | set; 34 | } 35 | 36 | public decimal Total 37 | { 38 | get 39 | { 40 | decimal subTotal = 0; 41 | if (LineItems == null) 42 | return subTotal; 43 | LineItems.ForEach(d => subTotal += d.SubTotal); 44 | return subTotal; 45 | } 46 | } 47 | 48 | public List LineItems 49 | { 50 | get; 51 | set; 52 | } 53 | 54 | public void AddLineItem(PurchaseOrderLineItemDto item) 55 | { 56 | if (this.LineItems == null) 57 | LineItems = new List(); 58 | LineItems.Add((PurchaseOrderLineItemDto)item); 59 | } 60 | } 61 | 62 | public class PurchaseOrderLineItemDto : Prototype.RingleERP.Intf.PorotoType 63 | { 64 | 65 | public PurchaseOrderLineItemDto() { } 66 | 67 | public PurchaseOrderLineItemDto Clone() 68 | { 69 | PurchaseOrderLineItemDto newDto = (PurchaseOrderLineItemDto)this.MemberwiseClone(); 70 | return newDto; 71 | } 72 | 73 | public string ProductName 74 | { 75 | get; 76 | set; 77 | } 78 | 79 | public int Quantity 80 | { 81 | get; 82 | set; 83 | } 84 | 85 | public decimal Price 86 | { 87 | get; 88 | set; 89 | } 90 | 91 | public decimal SubTotal 92 | { 93 | get { return Caculate(); } 94 | } 95 | 96 | private decimal Caculate() 97 | { 98 | return Price * (decimal)Quantity; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/ConsoleApplication/ConsoleApplication.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {2BE70449-EB14-4BD2-87F2-06CE79BDBE55} 8 | Exe 9 | Properties 10 | ConsoleApplication 11 | ConsoleApplication 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {d3cf315b-8512-49c8-ac29-b81447e0260f} 51 | Control 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/ConsoleApplication/Observer/ObserverConsole.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using hsdc.dpt.Control.Behavioral.Observer; 7 | 8 | namespace ConsoleApplication.Observer 9 | { 10 | public class ObserverConsole 11 | { 12 | public static void Main(string[] args) 13 | { 14 | ManageNewsControl control = new ManageNewsControl(); // new a domain controller 15 | // UI 是不應該會直接接觸到 Observer 這類的 domain 物件 (違背封裝原則) 16 | // 正式的作法應該是傳入如 Enum 類型的 Observer 名稱,然後再由 domain controller 依其名稱來建立對應的物件 17 | IObserver activeMonitor = new ActiveMonitor(); 18 | IObserver 狗仔 = new Reporter(); 19 | 20 | Console.WriteLine("新增訂閱者:ActiveMonitor"); 21 | control.訂閱(activeMonitor); 22 | Console.WriteLine("新增訂閱者:狗仔記者"); 23 | control.訂閱(狗仔); 24 | Console.WriteLine("--------------------------------------------------------------------------"); 25 | Console.ReadKey(); 26 | Console.WriteLine(""); 27 | 28 | string news1 = "2014/12/10-10:16 - 阿基師與熟女上摩鐵"; 29 | Console.WriteLine("發佈新聞 (Subject):" + news1); 30 | Console.WriteLine("--------------------------------------------------------------------------"); 31 | Console.ReadKey(); 32 | Console.WriteLine(""); 33 | 34 | Console.WriteLine("新聞接收者 (Observers): "); 35 | Console.WriteLine("--------------------------------------------------------------------------"); 36 | control.發佈(news1); 37 | Console.WriteLine("--------------------------------------------------------------------------"); 38 | Console.ReadKey(); 39 | 40 | Console.WriteLine(""); 41 | Console.WriteLine("取消訂閱:狗仔記者"); 42 | control.取消訂閱(狗仔); 43 | Console.WriteLine("--------------------------------------------------------------------------"); 44 | Console.ReadKey(); 45 | Console.WriteLine(""); 46 | 47 | string news2 = "2014/12/10-15:32 - 太陽花女王被壹週刊爆出援交"; 48 | Console.WriteLine("發佈新聞 (Subject):" + news2); 49 | Console.WriteLine("--------------------------------------------------------------------------"); 50 | Console.ReadKey(); 51 | Console.WriteLine(""); 52 | 53 | Console.WriteLine("新聞接收者 (Observers): "); 54 | Console.WriteLine("--------------------------------------------------------------------------"); 55 | control.發佈(news2); 56 | Console.WriteLine("--------------------------------------------------------------------------"); 57 | Console.ReadKey(); 58 | 59 | Console.WriteLine("\n\n------------------------- The End -------------------------"); 60 | Console.ReadKey(); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/Mediator/Order.cshtml: -------------------------------------------------------------------------------- 1 | @model List 2 | 3 | @{ 4 | ViewBag.Title = "線上訂購"; 5 | } 6 | 7 |

線上訂購

8 | 9 |

請選擇產品: 10 | 19 |

20 | 21 | 22 |
23 | 24 | 25 | @section BodyScripts 26 | { 27 | 28 | 29 | 30 | 31 | 86 | } -------------------------------------------------------------------------------- /Source Code/Design_Patterns_Demo/WebMVC/Views/TemplateMethod/Index.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @{ 3 | ViewBag.Title = "保單異動作業"; 4 | } 5 | 6 |

保單異動作業

7 | 8 | 選擇異動作業: 9 | 14 | 15 | 17 | 18 | @section BodyScripts 19 | { 20 | 74 | } 75 | --------------------------------------------------------------------------------