๋๋ถ์ ์ธํฐํ์ด์ค ๊ธฐ๋ณธ ๊ตฌํ์ ๋ํ ์ ์ ํ ์ฉ๋๋ฅผ ๋ ์ ์๊ฒ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ค๋ง, ๋งํฌ์์ ์๊ฐ๋ ๋ด์ฉ์ ์ธํฐํ์ด์ค ๊ตฌํ ์ฝ๋, ์๋น ์ฝ๋๋ฅผ ์์ ๋กญ๊ฒ ์ฌ์์ฑ/๋น๋๊ฐ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ง ์ ํจํ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฌผ๋ก ๋๋ถ๋ถ ์ฌ๊ธฐ์ ํด๋น๋์ง๋ง, ๊ทธ ๋ชจ๋์ด ์ธ๋ถ ๋ชจ๋(๋ค๋ฅธ ์
์ฒด๊ฐ ์ ๊ณตํ ํจํค์ง)์ด๋ผ๋ฉด, ์์ ์์ฒญ์ ๋ํ ๋์์ด ์ํํ์ง ์์ ๊ฒฝ์ฐ๋ ์์ ์ ์์ต๋๋ค.
FP ๋ ๋ฐ์ดํฐ์ ํจ์๋ฅผ ๋ถ๋ฆฌํด์ ์ ์ธํ๊ธฐ ๋๋ฌธ์, ์ด๋ฌํ ์ ์ฝ์์ ์์ ๋ก์ด ํธ์ธ๋ฐ, ์ด๋ ์๋น ์ฝ๋๊ฐ ๊ธฐ์กด ๋ชจ๋์ ๋ณ๊ฒฝํ์ง ์๊ณ ๋ ํ์์ ํ์๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ์ผ๋ง๋ ์ง ๊ฐ๋ฅํ๋ค๋ ์๋ฏธ๊ฐ ๋ฉ๋๋ค.
C#์ FP๊ฐ ์๊ตฌํ๋ ๋ฐ์ดํฐ ๋ถ๋ณ์ฑ(record), ๋ฐ์ดํฐ ํ์์ ํ์, ๊ฐ๋ ฅํ ํ์์ ํจ์(delegate) ๋ฅผ ๋ชจ๋ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ FP ์ ์ฅ์ ๋ ํฅ์ ํ ์ ์์ต๋๋ค.
๋งํฌ์ ์์ ์ค ์ผ๋ถ๋ฅผ FP ๋ก ํํํ๋ค๋ฉด,
namespace Original;
public record CustomerType( // ... );
public record OrderType(//...);
์๋๋ ๋ฒ์ 1์
๋๋ค.
using Original;
namespace Version1;
public static class Customer
{
public static decimal ComputeLoyaltyDiscount(CustomerType customer)
{
DateTime TwoYearsAgo = DateTime.Now.AddYears(-2);
if ((customer.DateJoined < TwoYearsAgo) && (customer.PreviousOrders.Count() > 10))
{
return 0.10m;
}
return 0;
}
}
์๋๋ ๋ฒ์ 1์ ์๋น ์ฝ๋์
๋๋ค.
using Version1;
// ...
// Check the discount:
Console.WriteLine($"Current discount: {Customer.ComputeLoyaltyDiscount(c)}");
์๋๋ ๋ฒ์ 2์
๋๋ค.
using Original;
namespace Version2;
public static class Customer
{
public static void SetLoyaltyThresholds(
TimeSpan ago,
int minimumOrders = 10,
decimal percentageDiscount = 0.10m)
{
length = ago;
orderCount = minimumOrders;
discountPercent = percentageDiscount;
}
private static TimeSpan length = new TimeSpan(365 * 2, 0,0,0); // two years
private static int orderCount = 10;
private static decimal discountPercent = 0.10m;
public static decimal ComputeLoyaltyDiscount(CustomerType customer)
{
DateTime start = DateTime.Now - length;
if ((customer.DateJoined < start) && (customer.PreviousOrders.Count() > orderCount))
{
return discountPercent;
}
return 0;
}
}
์๋น์ฝ๋
using Version2;
// ...
Customer.SetLoyaltyThresholds(new TimeSpan(30, 0, 0, 0), 1, 0.25m);
Console.WriteLine($"Current discount: {Customer.ComputeLoyaltyDiscount(c)}");
๋ณด์๋ค์ํผ, ๊ตฌ๋ชจ๋์ ์ฝ๋๋ฅผ ์์ (๊ณผ ์ฌ๋น๋)ํ ํ์๋ ์๊ณ , ๋์ค์ ์์ฑ๋๋ ๋ฒ์ ๋ค์ ๊ธฐ์กด ๋ฒ์ ๋ค ๋ฟ๋ง ์๋๋ผ ๊ณผ๊ฑฐ์ ์๋น ์ฝ๋์๋ ์ด๋ ํ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
๋ฟ๋ง ์๋๋ผ, ๋งํฌ์ OOP ์ฝ๋์ ๋นํด์, FP ์ฝ๋๋ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋
์ฑ ์ธก๋ฉด์์๋ ํฌ๊ฒ ๋ฌ๋ผ์ง๋ ๋ถ๋ถ๋ ์์ต๋๋ค.
ํนํ ์ฃผ๋ชฉํ ๋ถ๋ถ์, ๋งํฌ๋ ๊ธ์ ๋ค์ ๊ธ์
๊ธฐ๋ณธ ์ธํฐํ์ด์ค ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ mixin ํ์ ๋ง๋ค๊ธฐ - C# | Microsoft Learn
๋งจ ๋ง์ง๋ง์ ์๋ ์ฃผ์ ์ฌํญ์ ๋ํ ์ฐ๋ ค๋ FP์๋ ์ ์ฉ๋์ง ์์ต๋๋ค.
๋ฌผ๋ก , ๋ชจ๋ C# ์ฝ๋๋ฅผ FP ์คํ์ผ๋ก ๋ฐ๊ฟ ์๋ ์์ต๋๋ค.
OOP ๋ง์ ์ฅ์ ์ด ํนํ ๋ถ๊ฐ๋๋ ๋ถ๋ถ, ์๋ฅผ ๋ค๋ฉด ์๋น์ค ๊ฐ์ฒด์ ๊ตฌํ์๋ ์ ์ ํ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์๊ธ์ ํ๋ก์ ํธ๋ ์ด๋ฌํ FP ์ ์ฅ์ ์ ์คํํด๋ณด๊ธฐ ์ํ ๊ฒ์ธ๋ฐ, ๋๋ถ์ ๊ทธ ์ฅ์ ๊ณผ ๋จ์ ์ด ๋์ฑ ์ ๋ช
ํด์ง๋ ๊ณ๊ธฐ๊ฐ ๋ ๊ฒ ๊ฐ์ ํฐ ๋์์ด ๋์์ต๋๋ค.