Refactoring Day 9 : Extract Interface
来源:互联网 发布:上海弘历软件 编辑:程序博客网 时间:2024/04/19 13:09
Today we look at an often overlooked refactoring. Extract Interface. When you notice more than one class
using a similar subset of methods on a class, it is useful to break the dependency and introduce an interface
that the consumers to use. It’s easy to implement and has benefits from loose coupling.
1: public class ClassRegistration
2: {
3: public void Create()
4: {
5: // create registration code
6: }
7:
8: public void Transfer()
9: {
10: // class transfer code
11: }
12:
13: public decimal Total { get; private set; }
14: }
15:
16: public class RegistrationProcessor
17: {
18: public decimal ProcessRegistration(ClassRegistration registration)
19: {
20: registration.Create();
21: return registration.Total;
22: }
23: }
In the after example, you can see we extracted the methods that both consumers use and placed them in
an interface. Now the consumers don’t care/know about the class that is implementing these methods. We
have decoupled our consumer from the actual implementation and depend only on the contract that we
have created.
1: public interface IClassRegistration
2: {
3: void Create();
4: decimal Total { get; }
5: }
6:
7: public class ClassRegistration : IClassRegistration
8: {
9: public void Create()
10: {
11: // create registration code
12: }
13:
14: public void Transfer()
15: {
16: // class transfer code
17: }
18:
19: public decimal Total { get; private set; }
20: }
21:
22: public class RegistrationProcessor
23: {
24: public decimal ProcessRegistration(IClassRegistration registration)
25: {
using a similar subset of methods on a class, it is useful to break the dependency and introduce an interface
that the consumers to use. It’s easy to implement and has benefits from loose coupling.
1: public class ClassRegistration
2: {
3: public void Create()
4: {
5: // create registration code
6: }
7:
8: public void Transfer()
9: {
10: // class transfer code
11: }
12:
13: public decimal Total { get; private set; }
14: }
15:
16: public class RegistrationProcessor
17: {
18: public decimal ProcessRegistration(ClassRegistration registration)
19: {
20: registration.Create();
21: return registration.Total;
22: }
23: }
In the after example, you can see we extracted the methods that both consumers use and placed them in
an interface. Now the consumers don’t care/know about the class that is implementing these methods. We
have decoupled our consumer from the actual implementation and depend only on the contract that we
have created.
1: public interface IClassRegistration
2: {
3: void Create();
4: decimal Total { get; }
5: }
6:
7: public class ClassRegistration : IClassRegistration
8: {
9: public void Create()
10: {
11: // create registration code
12: }
13:
14: public void Transfer()
15: {
16: // class transfer code
17: }
18:
19: public decimal Total { get; private set; }
20: }
21:
22: public class RegistrationProcessor
23: {
24: public decimal ProcessRegistration(IClassRegistration registration)
25: {
26: registration.Create();
27: return registration.Total;
28: }
29: }
0 0
- Refactoring Day 9 : Extract Interface
- Refactoring Day 1 封装集合
- Refactoring Day 2 : Move Method
- 11.8 extract interface(提炼接口)
- Extract Interface(提炼接口)
- Refactoring Day 3 : Pull Up Method
- Refactoring Day 4 : Push Down Method
- Refactoring Day 5 : Pull Up Field
- Refactoring Day 6 : Push Down Field
- Refactoring Day 7 : Rename (method, class, parameter)
- Refactoring Day 8 : Replace Inheritance with Delegation
- Refactoring Day 31 : Replace conditional with Polymorphism
- Oracle,INTERVAL DAY TO SECOND,EXTRACT (datetime)
- Refactoring
- Refactoring
- Refactoring
- Refactoring
- refactoring笔记——代码重构的方法:Extract Method
- 深度恢复
- linux strace命令--跟踪系统调用
- volatile的用法
- 地理空间基础设施 Geospatial cyberinfrastructure (GCI)
- 安卓 webview的js交互
- Refactoring Day 9 : Extract Interface
- 串口通信整理
- 我的vim配置
- Properties 和 Jsp Compile Nullpointer
- Android WebView开发问题及优化汇总
- iOS 7 UI 过渡指南 - 开始之前(iOS 7 UI Transition Guide - Before You Start)
- Django:The translation infrastructure cannot be initialized before the apps registry is ready
- 来新公司啦!
- 中国企业家为什么越来越能说了?