C#程式設計模式學習心得篇

來源:互聯網
上載者:User

     在公司這幾天一直都在看程式設計模式,看完的之後感覺還是似懂非懂,但心得還是頗多,程式設計中使用的原則有一條:面向介面編程,而且在設計過程中,要保證能在可以很方便的對程式進行拓展,而且不用改動現有的程式.下面是我自己設計的一個抽象工作模式,不知道我的理解是否正確.

 

using System;
using System.Collections.Generic;
using System.Text;

namespace 抽象原廠模式
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateHouse newhouse = new CreateHouse(new CreateCountrysideHouse());
            newhouse.dispaly();
            Console.ReadKey();
        }
    }

    //建造房子的介面
    public interface ICreateHouse
    {
        //建立房子架構
        void createFrame();
        //建立房子的門
        void createDoor();
        //建立房子的窗
        void createWindow();
        //建立房子
        void display();
    }

    //農村風格
    public class CreateCountrysideHouse : ICreateHouse
    {
        public void createFrame()
        {
            Console.WriteLine("this is Countryside Frame");
        }
        public void createDoor()
        {
            Door door = new CountrysideDoor();
            door.dispaly();
        }
        public void createWindow()
        {
            Console.WriteLine("this is Countryside Window");
        }
        public void display()
        {
            this.createFrame();
            this.createDoor();
            this.createWindow();
        }
    }

    //城市風格
    public class CreateCityHouse : ICreateHouse
    {
        public void createFrame()
        {
            Console.WriteLine("this is City Frame");
        }
        public void createDoor()
        {
            //Console.WriteLine("this is City Door");
            Door door = new CityDoor();
            door.dispaly();
        }
        public void createWindow()
        {
            Console.WriteLine("this is City Window");
        }
        public void display()
        {
            this.createFrame();
            this.createDoor();
            this.createWindow();
        }
    }

    public class CreateHouse
    {
        private ICreateHouse iCreateHouse;
        public CreateHouse(ICreateHouse iCreateHouse)
        {
            this.iCreateHouse = iCreateHouse;
        }
        public void dispaly()
        {
            iCreateHouse.display();
        }
    }

    //門的基類
    public abstract class Door
    {
        //鎖
        public void Locker()
        {
            Console.WriteLine("this is Door have Locker");
        }
        //門的形狀
        public abstract void shape();
        public void dispaly()
        {
            this.Locker();
            this.shape();
        }
    }

    //農村門
    public class CountrysideDoor : Door
    {
        public override void shape()
        {
            Console.WriteLine("this is Countryside Door");
        }
    }
    //城市門
    public class CityDoor : Door
    {
        public override void shape()
        {
            Console.WriteLine("this is City Door");
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.