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