程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 無廢話C#設計模式之六:Builder(2)

無廢話C#設計模式之六:Builder(2)

編輯:關於C語言

示例代碼

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

namespace BuilderExemple

{

    class Program

    {

        static void Main(string[] args)

        {

            ComputerFactory factory = new ComputerFactory();

            ComputerBuilder office = new OfficeComputerBuilder();

            factory.BuildComputer(Office);

            Office.Computer.ShowSystemInfo();

            ComputerBuilder game = new GameComputerBuilder();

            factory.BuildComputer(game);

            game.Computer.ShowSystemInfo();

        }

    }

    class ComputerFactory

    {

        public void BuildComputer(ComputerBuilder cb)

        {

            Console.WriteLine();

            Console.WriteLine(">>>>>>>>>>>>>>>>>>Start Building " + cb.Name);

            cb.SetupMainboard();

            cb.SetupCpu();

            cb.SetupMemory();

            cb.SetupHarddisk();

            cb.SetupVideocard();

            Console.WriteLine(">>>>>>>>>>>>>>>>>>Build " + cb.Name + " Completed");

            Console.WriteLine();

        }

    }

    abstract class ComputerBuilder

    {

        protected string name;

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        protected Computer computer;

        public Computer Computer

        {

            get { return computer; }

            set { computer = value; }

        }

        public ComputerBuilder()

        {

            computer = new Computer();

        }

        public abstract void SetupMainboard();

        public abstract void SetupCpu();

        public abstract void SetupMemory();

        public abstract void SetupHarddisk();

        public abstract void SetupVideocard();

    }

    class OfficeComputerBuilder : ComputerBuilder

    {

        public OfficeComputerBuilder()

        {

            name = "OfficeComputer";

        }

        public override void SetupMainboard()

        {

            computer.Mainboard = "Abit升技LG-95C 主板(Intel 945GC芯片組/LGA 775/1066MHz) ";

        }

        public override void SetupCpu()

        {

            computer.Cpu = "Intel 英特爾賽揚D 336 (2.8GHz/LGA 775/256K/533MHz)  ";

        }

        public override void SetupMemory()

        {

            computer.Memory = "Patriot博帝DDR2 667 512MB 台式機內存";

        }

        public override void SetupHarddisk()

        {

            computer.Harddisk = "Hitachi日立SATAII接口台式機硬盤(80G/7200轉/8M)盒裝";

        }

        public override void SetupVideocard()

        {

            computer.Videocard = "主板集成";

        }

    }

    class GameComputerBuilder : ComputerBuilder

    {

        public GameComputerBuilder()

        {

            name = "GameComputer";

        }

        public override void SetupMainboard()

        {

            computer.Mainboard = "GIGABYTE技嘉GA-965P-DS3 3.3 主板(INTEl P965 東莞產)" ;

        }

        public override void SetupCpu()

        {

            computer.Cpu = "Intel 英特爾酷睿E4400 (2.0GHz/LGA 775/2M/800MHz)盒裝";

        }

        public override void SetupMemory()

        {

            computer.Memory = "G.SKILl 芝奇F2-6400CL5D-2GBNQ DDR2 800 1G*2台式機內存";

        }

        public override void SetupHarddisk()

        {

            computer.Harddisk = "Hitachi日立SATAII接口台式機硬盤(250G/7200轉/8M)盒裝";

        }

        public override void SetupVideocard()

        {

            computer.Videocard = "七彩虹逸彩GT-GD3 UP烈焰戰神H10 顯卡(GeForce 8600GT/256M/DDR3)支持HDMI!";

        }

    }

    class Computer

    {

        private string videocard;

        public string Videocard

        {

            get { return videocard; }

            set { videocard = value; }

        }

        private string cpu;

        public string Cpu

        {

            get { return cpu; }

            set { cpu = value; }

        }

        private string mainboard;

        public string Mainboard

        {

            get { return mainboard; }

            set { mainboard = value; }

        }

        private string memory;

        public string Memory

        {

            get { return memory; }

            set { memory = value; }

        }

        private string harddisk;

        public string Harddisk

        {

            get { return harddisk; }

            set { harddisk = value; }

        }

        public void ShowSystemInfo()

        {

            Console.WriteLine("==================SystemInfo==================");

            Console.WriteLine("CPU:" + cpu);

            Console.WriteLine("MainBoard:" + mainboard);

            Console.WriteLine("Memory:" + memory);

            Console.WriteLine("VideoCard:" + videocard);

            Console.WriteLine("HardDisk:" + harddisk);

        }

    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved