Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//--------------------------------------------------------------------------------------
// 5. Objects, Data Structures, Classes - Đối tượng, cấu trúc dữ liệu, lớp
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Sử dụng getters và setters
// Bad
class BankAccount
{
public double Balance = 1000;
}
var bankAccount = new BankAccount();
// Fake buy shoes...
bankAccount.Balance -= 100;
// Good
class BankAccount
{
private double _balance = 0.0D;
pubic double Balance {
get {
return _balance;
}
}
public BankAccount(balance = 1000)
{
_balance = balance;
}
public void WithdrawBalance(int amount)
{
if (amount > _balance)
{
throw new Exception('Amount greater than available balance.');
}
_balance -= amount;
}
public void DepositBalance(int amount)
{
_balance += amount;
}
}
var bankAccount = new BankAccount();
// Buy shoes...
bankAccount.WithdrawBalance(price);
// Get balance
balance = bankAccount.Balance;
//--------------------------------------------------------------------------------------
// Hạn chế tối đa public setters
// Bad
class Employee
{
public string Name { get; set; }
public Employee(name)
{
Name = name;
}
}
// Good
class Employee
{
public string Name { get; }
public Employee(string name)
{
Name = name;
}
}
//--------------------------------------------------------------------------------------
// Sử dụng các method chaining
// Bad
// Good
public static class ListExtensions
{
public static List<T> FluentAdd<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
public static List<T> FluentClear<T>(this List<T> list)
{
list.Clear();
return list;
}
public static List<T> FluentForEach<T>(this List<T> list, Action<T> action)
{
list.ForEach(action);
return list;
}
public static List<T> FluentInsert<T>(this List<T> list, int index, T item)
{
list.Insert(index, item);
return list;
}
public static List<T> FluentRemoveAt<T>(this List<T> list, int index)
{
list.RemoveAt(index);
return list;
}
public static List<T> FluentReverse<T>(this List<T> list)
{
list.Reverse();
return list;
}
}
internal static void ListFluentExtensions()
{
var list = new List<int>() { 1, 2, 3, 4, 5 }
.FluentAdd(1)
.FluentInsert(0, 0)
.FluentRemoveAt(1)
.FluentReverse()
.FluentForEach(value => value.WriteLine())
.FluentClear();
}
//--------------------------------------------------------------------------------------
// Khuyến khích dùng composition thay vì kế thừa
// Bad
class Employee
{
private string Name { get; set; }
private string Email { get; set; }
public Employee(string name, string email)
{
Name = name;
Email = email;
}
// ...
}
// Bad because Employees "have" tax data.
// EmployeeTaxData is not a type of Employee
class EmployeeTaxData : Employee
{
private string Name { get; }
private string Email { get; }
public EmployeeTaxData(string name, string email, string ssn, string salary)
{
// ...
}
// ...
}
// Good
class EmployeeTaxData
{
public string Ssn { get; }
public string Salary { get; }
public EmployeeTaxData(string ssn, string salary)
{
Ssn = ssn;
Salary = salary;
}
// ...
}
class Employee
{
public string Name { get; }
public string Email { get; }
public EmployeeTaxData TaxData { get; }
public Employee(string name, string email)
{
Name = name;
Email = email;
}
public void SetTax(string ssn, double salary)
{
TaxData = new EmployeeTaxData(ssn, salary);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment