C#程序设计模式学习心得篇

     在公司这几天一直都在看程序设计模式,看完的之后感觉还是似懂非懂,但心得还是颇多,程序设计中使用的原则有一条:面向接口编程,而且在设计过程中,要保证能在可以很方便的对程序进行拓展,而且不用改动现有的程序.下面是我自己设计的一个抽象工作模式,不知道我的理解是否正确.

 

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");
        }
    }
}

posted @ 2008-10-16 14:06  TerryXu  阅读(3135)  评论(3编辑  收藏  举报