<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天看到一篇应用接口的文章,主要是如何利用.net内置的功能,来重构代码,觉得有点意思,摘录其中的代码下来,以备将来参考。但是我与这篇文章的作者有同样的疑问,这样重构代码真的可以吗?是如何实现松耦合的呢?不知道有没有高手,可以指点一下?谢谢!</span>
首先是旧代码,如下:
<span style="font-size:18px;">public class ClassRegistration { public void Create() { // create registration code } public void Transfer() { // class transfer code } public decimal Total { get; private set; } } public class RegistrationProcessor { public decimal ProcessRegistration(ClassRegistration registration) { registration.Create(); return registration.Total; } }</span>
<span style="font-size:18px;">public interface IClassRegistration { void Create(); decimal Total { get; } } public class ClassRegistration : IClassRegistration { public void Create() { // create registration code } public void Transfer() { // class transfer code } public decimal Total { get; private set; } } public class RegistrationProcessor { public decimal ProcessRegistration(IClassRegistration registration) { registration.Create(); return registration.Total; } }</span>
原文地址:http://blog.csdn.net/hehuii/article/details/40341313