随笔分类 - Basic .Net
摘要:应用场景:通常的情况下,直接new完一个类之后,然后调用这个类的办法,但是在实际的项目中存在需要存在“重做”等功能,这样就需要使用command模式来实现。实现代码: class Program { static void Main(string[] args) { User user = new User(); user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2)
阅读全文
摘要:应用场景:在软件的设计中,通常软件的结构是相对固定的,但是子步骤是相对变化的,即算法的骨架是固定,实现延迟到子类中实现。实现代码: class Program { static void Main(string[] args) { VehicaltestFramework.DoTest(new HongqiCar()); } } // 框架开发者-先开发 public abstract class Vehical { // 这些方法可以实现或者是不实现,根据具体的应用 // 来决定。 public abstract void Startup(); public abstract void Ru
阅读全文
摘要:应用场景:在面向对象设计中,为某个对象提供一中代理,控制对这个对象的访问,例如可以实现缓存,分布式程序中调用远程对象。实现代码: public interface IEmployee { public void GetSalary(); public void Report(); public void ApplyVacation(); } public Employee : IEmployee { } // 客户端直接使用这个代理来访问emplooyee类 class EmployeeProxy : IEmployee { // 使用soap协议实现web service public v
阅读全文
摘要:应用场景:解决在系统中存在大量细小对象耗费内存的情况。实现代码: // 参考:http://java.chinaitlab.com/model/795882_2.html public abstract class Flyweight { public abstract void Operation(); } public class ConcreteFlyWeight : Flyweight { private string str; public ConcreteFlyWeight(string str) { this.str = str; } public override void O
阅读全文
摘要:使用场景:Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subs
阅读全文
摘要:使用场景:动态的给一个对象增加一些额外的功能。主体类在多个方向的功能拓展。实现代码:// Decorator pattern -- Structural example using System; namespace DoFactory.GangOfFour.Decorator.Structural{ /// <summary> /// MainApp startup class for Structural /// Decorator Design Pattern. /// </summary> class MainApp
阅读全文
摘要:应用场景:客户代码过多的依赖对象容器的复杂的内部实现,对象容器内部的变化将导致客户端的代码的剧烈变化。将客户代码和对象容器解耦,从而更能够应对变化。实现代码: public interface IBox { void Process(); void Add(IBox box); void Remove(IBox box); } public class SingleBox : IBox { public void Process() { // do process for myself } public void Add(IBox box) { // 两个实现方案 // 1. do noth
阅读全文
摘要:应用场景:解决的是在需求中,面临两个需求维度的变化的话,可以使用Bridge模式。实现代码: //------------------------------------------------------- // Tank 2.0 // 两个方向的变化,一中是平台,另外一中是类型的变化。 // 将事物中多个维度的变化分离,使得其能够独立的变化。 // 一个类只能有一个变化原因。 //--------------------------------------------------------------- public abstract class Tank { protected Tan
阅读全文
摘要:Get here : http://www.codeproject.com/KB/cs/cs_interfaces.aspxIntroductionInterfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions fro
阅读全文
摘要:使用场景:将现有系统应用到新系统中,但是两个系统接口是不相同的,这里使用adapter模式类解决这个问题。实现代码:// 现有类 class ExistingClass { public void SpecificRequest1() { } public void SpecificRequest2() { } // ... } // 新环境所使用的接口 interface ITarget { void Request(); } // 另外一个系统 class MySystem { public void Process(ITarget target) // 面向接口编程 { // 这里可以直
阅读全文
摘要:Using the CodeThis is a helper class that can be used to perform a deep copy of an object:CollapseUsage of this class becomesObjectCopier.Clone(objectBeingCloned);.Points of InterestIn case you prefer to use the new Extension methods of C# 3.0, change the method to have the following signature:Now t
阅读全文
摘要:应用场景: 复杂对象构建,这些对象是可能不断变化的,但是这些对象的借口是相对稳定的。实现代码: public interface INormalActor { INormalActor Clone(); } public interface IFlyActor { IFlyActor Clone(); } public interface IWaterActor { IWaterActor Clone(); } // 深拷贝通过序列化实现序列化 [Serializable] public class NormalActorA : INormalActor { #region INormalAc
阅读全文
摘要:应用场景:在软件系统中,经常面两“某个对象”的创建的工作,但是由于需求的变化,这个对象经常面临剧烈的变化,但是接口是相对稳定的。这里可以使用Factory Pattern来实现。Factory Pattern针对的是一个对象,而Abstract Factory针对的是一系列的对象的变化。实现代码:接口实现: public enum Direction { NORTH, SOUTH, WEST, EAST }; public interface ICar { void Startup(); void Run(); void Turn(Direction d
阅读全文
摘要:最近在看设计模式的一些东西,在讲到builder模式中使用到了c#的反射机制,从网上找了写资料,整理如下:在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系:System.Reflection命名空间(1)AppDomain:应用程序域,可以将其理解为一组程序集的逻辑容器(2)Assembly:程序集类(3)Module:模块类(4)Type:使用反射得到类型信息的最核心的类他们之间是一种从属关系,也就是说,一个AppDomain可以包含N个Assembly,一个Assembly可以包含N个Module,而一个Module可以包含N个Type.AppDomain这个类我们等下再来
阅读全文
摘要:应用场景:在软件系统中,有时面临着“一个复杂对象的创建”的工作,通常是由各个部分的是使用一定的算法来过程的。由于需求的变化(各个部分在变化),每个部分经常面临巨大的变化,但是将他们组合在一起的算法确实相对稳定的。同样的构建过程可以创建不同的表示。实现代码:interfaces: // 这里是相当于能够提供的服务,客户端只需要使用这些接口皆可以了。 public interface IBuilder { void BuildDoor(); void BuildWalls(); void BuildWindows(); void BiildFloor();
阅读全文
摘要:解决问题:Road road = new Road(), 显然road依赖于Road实现,而Road的类型可能是在不断变化,不能应对“具体实例化类型的变化”。这里的解决思路是“封装变化点”,将程序中一直在变化的地方封装起来,从而实现代码较小的改变。Abstract Factory模式应用场景 : 应对“新系列”的需求的变化,对象是大致不变的,但是对象的系列的变化是经常的。代码实现:interfaces,这里说明我能提供什么功能,具体怎么实现不需考虑: public inter
阅读全文
摘要:Design Pattern : Singleton 1.闲话:不是为了使用设计模式而学习设计模式,设计模式是在实际项目中根据常见的问题而总结出来的正确的做法。实际项目中,用户的需求是在不断变化,而设计模式的使用就是为了应对这种变化,尽量更改较少的代码来应对需求的变化。2.面向对象设计原则针对接口编程有先使用对象组合,尽量较少使用对象组合封装变化点,应对实际中需求变化3.Singleton Pattern // 实现传递参数singleton,如何控制用户使用new对共有对象的调用。 public class Singleton1 { private static volatile Singl
阅读全文
摘要:An interface can declare an event. The following example shows how to implement interface events in a class. Basically the rules are the same as when implementing any interface method or property.To implement interface events in a classDeclare the event in your class and then invoke it in the approp
阅读全文
摘要:1.使用PictureBox加载一个gif图像,这样的好处是可以避免winform中的线程的困扰。下面就是这个所使用的图片资源。比较容易。如果想要使用progress bar的话,直接设置PictureBox的visible属性http://cid-f41b44b9285d0b86.spaces.live.com/blog/cns!F41B44B9285D0B86!298.entry中存在这个资源。2.使用.net中的ProgressBar控件http://blog.csdn.net/gisfarmer/archive/2009/01/12/3757595.aspx第一步:设计界面不说了...
阅读全文
摘要:本文讨论:•用于编写单元测试的 NUnit•用于创建代码文档资料的 NDoc•用于生成解决方案的 NAnt•用于生成代码的 CodeSmith•用于监视代码的 FxCop•用于编译少量代码的 Snippet Compiler•两种不同的转换器工具:ASP.NET 版本转换器和 Visual Studio .NET 项目转换器•用于生成正则表达式的 Regulator•用于分析程序集的 .NET Reflector本文使用了下列技术:.
阅读全文