Encapsulation

本篇同步发文於个人网站: Encapsulation

This article references the chapter 7 ” Encapsulation ” of Refactoring: Improving the Design of Existing Code (2nd Edition). Author had highlighted many important refactorings in this chapter.

(adsbygoogle = window.adsbygoogle || []).push({});

I use .NET C# to practice with these refactorings and upload to Github.

Encapsulation

(Photo from Pixabay: https://pixabay.com/illustrations/plumber-repair-tools-pipe-plunger-4427401/ )

Encapsulate Record

Tips
  • For mutable data, use Class object instead of Record.
  • If some record has nested data (list/hash/….), encapsulating this nested data is better.
  • Usually use a class to encapsulate the record and create the access methods in the class.
  • For nested data, we first focus on “Update Method”, then “Read Method” can return the copy / readonly proxy of the data.
  • If a record’s field is also a complex structure, consider to use Encapsulate Record or Encapsulate Collection.
Examples:
  • Simple Encapsulation
  • Encapsulating a Nested Record

Github

Encapsulate Collection

Tips
  • Usually add “Add” and “Remove” functions for accessing the collection
  • Create a function that returns the copy of the collection. This avoids changing the original collection
  • If we want set the collection, use the copy of parameter collection to set.
Examples:
  • Simple Encapsulation

Github

Replace Primitive with Object

Tips
  • Some simple number/string instances will has complex logic, we should encapsulate them into classes. These classes are value classes.
  • Consider Change Reference to Value or Change Value to Reference for the created object.
Examples:
  • Simple Replacement

Github

Replace Temp with Query

Tips
  • This skill is used for the temp variable that is calculated once.
  • Some temp variable is calculated many times, we also extract its calculation logic into query function.
Examples:
  • Simple Replacement

Github

Extract Class

Tips
  • If one class’s responsibility is too huge, we consider that the class is split into main class and sub class. Sub class has the closed activities of functions/data.
Examples:
  • Simple Extraction

Github

(adsbygoogle = window.adsbygoogle || []).push({});

Inline Class

Tips
  • If one class’s responsibility has no reason to exist by its single class, move this class’s member into another class.
Examples:
  • Simple Inline

Github

Hide Delegate

Tips
  • Create the delegate function to hide the inner relationship between the called object and the caller. If the called object changes, the caller still uses the delegate function without knowing the change.
Examples:
  • Simple Hiding

Github

Remove Middle Man

Tips
  • If the delegate functions are too many, adjust caller to directly call the target object.
Examples:
  • Just use the Hide Delegate example from end to start

Substitute Algorithm

Tips
  • If the new algorithm is easy to maintain, we consider that the old algorithm is replaced by the new one.
Examples:
// before
static string FindPerson(string[] people)
{
	for(int i = 0; i < people.Length; i++) 
	{
		if (people[i] == "Don") 
		{
			return "Don";
		}
		if (people[i] == "John") 
		{
			return "John";
		}
		if (people[i] == "Kent") 
		{
			return "Kent";
		}
	}
	
	return "";
}


// after
static string FindPerson(string[] people)
{
	string[] candidates = new string[]{"Don", "John", "Kent"};
	return people.FirstOrDefault(x => candidates.Contains(x)) ?? "";
}

Conclusion

This chapter introduces me to the concept of complete encapsulation! If you want to learn detailed motivation and mechanics, study the chapter 7 ” Encapsulation ” of Refactoring: Improving the Design of Existing Code (2nd Edition). and it improves our programmer’s ability.


<<:  求救 各位linux大神! 关於mediapipe build 成apk的问题

>>:  ISO 27001 资讯安全管理系统 【解析】(六)

GitHub Advanced Security - 程序码扫描 (Code Scanning)

在前一篇文章GitHub Security - 基本安全相关功能介绍 内文中我们有对於 Repo 内...

Day14-守护饼乾大作战(一)

前言 哈罗大家好,前几天讲完 HTTP Security Headers 之後今天紧接着要讲 coo...

Day18:【技术篇】HTML的冷门使用技巧

一、前言   大家不论学习什麽程序语言、选择走前端後端或系统分析,一定都学过HTML这个文本标记语言...

[Day 13] 非同步的操作资料库?谈 suspendedTransactionAsync

前面我们聊到了如何存取资料库,以及遇到 N+1 问题时该如何发现以及解决问题。 今天我们来谈谈 Ex...

Day 20 - 规划各功能模组的介面

紧接着今天我们要来规划各个功能模组的介面了! 首先是登入後的首页,会陈列使用者上传记帐资讯,包含图片...