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");
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?