Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2abfb4a

Browse files
full code
1 parent 3ef4390 commit 2abfb4a

22 files changed

+647
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<StartupObject>Program</StartupObject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Folder Include="ExtractMethod\" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34525.116
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp.Tutorials.CodeRefactoring", "CSharp.Tutorials.CodeRefactoring.csproj", "{FE8A0736-8BD4-4403-A553-35033970E525}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FE8A0736-8BD4-4403-A553-35033970E525}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FE8A0736-8BD4-4403-A553-35033970E525}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FE8A0736-8BD4-4403-A553-35033970E525}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FE8A0736-8BD4-4403-A553-35033970E525}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FADC7234-E041-44CB-BBAA-6F1A2D074799}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using CSharp.Tutorials.CodeRefactoring.Samples;
2+
3+
namespace CSharp.Tutorials.CodeRefactoring.DesignPatterns
4+
{
5+
class FactoryPattern
6+
{
7+
public static void Run()
8+
{
9+
ShapeFactory factory = new ShapeFactory();
10+
Console.WriteLine("Calculating areas by using Factory Pattern");
11+
ShapeBase rectangle = factory.CreateShape(ShapeType.Rectangle, 5, 10);
12+
Console.WriteLine("Area of rectangle: " + rectangle.CalculateArea());
13+
14+
ShapeBase circle = factory.CreateShape(ShapeType.Circle, 7);
15+
Console.WriteLine("Area of circle: " + circle.CalculateArea());
16+
17+
ShapeBase triangle = factory.CreateShape(ShapeType.Triangle, 3, 4, 5);
18+
Console.WriteLine("Area of triangle: " + triangle.CalculateArea());
19+
}
20+
21+
class ShapeFactory
22+
{
23+
public ShapeBase CreateShape(ShapeType type, params double[] dimensions)
24+
{
25+
switch (type)
26+
{
27+
case ShapeType.Rectangle:
28+
if (dimensions.Length != 2)
29+
throw new ArgumentException("Invalid dimensions for rectangle.");
30+
return new Rectangle(dimensions[0], dimensions[1]);
31+
case ShapeType.Circle:
32+
if (dimensions.Length != 1)
33+
throw new ArgumentException("Invalid dimensions for circle.");
34+
return new Circle(dimensions[0]);
35+
case ShapeType.Triangle:
36+
if (dimensions.Length != 3)
37+
throw new ArgumentException("Invalid dimensions for triangle.");
38+
return new Triangle(dimensions[0], dimensions[1], dimensions[2]);
39+
default:
40+
throw new ArgumentException("Invalid shape specified.");
41+
}
42+
}
43+
}
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using CSharp.Tutorials.CodeRefactoring.Samples;
2+
3+
namespace CSharp.Tutorials.CodeRefactoring.DesignPatterns
4+
{
5+
class StrategyPattern
6+
{
7+
public static void Run()
8+
{
9+
Console.WriteLine("Calculating areas by using Strategy Pattern");
10+
11+
Shape rectangle = new Shape(new RectangleAreaCalculator(), 5, 10);
12+
Console.WriteLine("Area of rectangle: " + rectangle.CalculateArea());
13+
14+
Shape circle = new Shape(new CircleAreaCalculator(), 7);
15+
Console.WriteLine("Area of circle: " + circle.CalculateArea());
16+
17+
Shape triangle = new Shape(new TriangleAreaCalculator(), 3, 4, 5);
18+
Console.WriteLine("Area of triangle: " + triangle.CalculateArea());
19+
}
20+
21+
interface IAreaCalculator
22+
{
23+
double CalculateArea(params double[] dimensions);
24+
}
25+
26+
class RectangleAreaCalculator : IAreaCalculator
27+
{
28+
public double CalculateArea(params double[] dimensions)
29+
{
30+
if (dimensions.Length != 2)
31+
throw new ArgumentException("Invalid dimensions for rectangle.");
32+
return dimensions[0] * dimensions[1];
33+
}
34+
}
35+
36+
class CircleAreaCalculator : IAreaCalculator
37+
{
38+
public double CalculateArea(params double[] dimensions)
39+
{
40+
if (dimensions.Length != 1)
41+
throw new ArgumentException("Invalid dimensions for circle.");
42+
return Math.PI * dimensions[0] * dimensions[0];
43+
}
44+
}
45+
46+
class TriangleAreaCalculator : IAreaCalculator
47+
{
48+
public double CalculateArea(params double[] dimensions)
49+
{
50+
if (dimensions.Length != 3)
51+
throw new ArgumentException("Invalid dimensions for triangle.");
52+
double s = (dimensions[0] + dimensions[1] + dimensions[2]) / 2;
53+
return Math.Sqrt(s * (s - dimensions[0]) * (s - dimensions[1]) * (s - dimensions[2]));
54+
}
55+
}
56+
57+
class Shape : ShapeBase
58+
{
59+
private readonly IAreaCalculator _areaCalculator;
60+
private readonly double[] _dimensions;
61+
62+
public Shape(IAreaCalculator areaCalculator, params double[] dimensions)
63+
{
64+
_areaCalculator = areaCalculator;
65+
_dimensions = dimensions;
66+
}
67+
68+
public override double CalculateArea()
69+
{
70+
return _areaCalculator.CalculateArea(_dimensions);
71+
}
72+
}
73+
}
74+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace CSharp.Tutorials.CodeRefactoring
2+
{
3+
class EliminateCodeDuplication
4+
{
5+
public static void Run()
6+
{
7+
Console.WriteLine("Calculating areas by single method...");
8+
9+
// Calculate the area of a rectangle
10+
double rectangleArea = CalculateArea(ShapeType.Rectangle, 5, 10);
11+
Console.WriteLine("Area of rectangle: " + rectangleArea);
12+
13+
// Calculate the area of a circle
14+
double circleArea = CalculateArea(ShapeType.Circle, 7);
15+
Console.WriteLine("Area of circle: " + circleArea);
16+
17+
// Calculate the area of a triangle
18+
double triangleArea = CalculateArea(ShapeType.Triangle, 3, 4, 5);
19+
Console.WriteLine("Area of triangle: " + triangleArea);
20+
}
21+
22+
// Method to calculate the area of various shapes
23+
static double CalculateArea(ShapeType shape, params double[] dimensions)
24+
{
25+
switch (shape)
26+
{
27+
case ShapeType.Rectangle:
28+
return dimensions[0] * dimensions[1];
29+
case ShapeType.Circle:
30+
return Math.PI * dimensions[0] * dimensions[0];
31+
case ShapeType.Triangle:
32+
double s = (dimensions[0] + dimensions[1] + dimensions[2]) / 2;
33+
return Math.Sqrt(s * (s - dimensions[0]) * (s - dimensions[1]) * (s - dimensions[2]));
34+
default:
35+
throw new ArgumentException("Invalid shape specified");
36+
}
37+
}
38+
}
39+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using CSharp.Tutorials.CodeRefactoring.Samples;
2+
3+
namespace CSharp.Tutorials.CodeRefactoring
4+
{
5+
class ExtractMethod
6+
{
7+
// After refactoring
8+
public static void ProcessOrder(Order order)
9+
{
10+
Console.WriteLine("Processing order by using seperated methods...");
11+
ValidateOrder(order);
12+
UpdateInventory(order);
13+
CalculateTotalPrice(order);
14+
NotifyCustomer(order);
15+
}
16+
17+
static void ValidateOrder(Order order)
18+
{
19+
if (!order.IsValid)
20+
{
21+
throw new ArgumentException("Invalid order.");
22+
}
23+
}
24+
25+
static void UpdateInventory(Order order)
26+
{
27+
foreach (var product in order.Products)
28+
{
29+
// Reduce inventory by 1 for each ordered product
30+
int updatedQuantity = product.AvailableQuantity - 1;
31+
32+
// Ensure the updated quantity is non-negative
33+
updatedQuantity = Math.Max(updatedQuantity, 0);
34+
35+
// Update the product's available quantity
36+
product.AvailableQuantity = updatedQuantity;
37+
38+
// Print a message to simulate inventory update
39+
Console.WriteLine($"Inventory updated for product '{product.Name}': Available Quantity = {product.AvailableQuantity}");
40+
}
41+
}
42+
43+
static void CalculateTotalPrice(Order order)
44+
{
45+
double totalPrice = order.Products.Sum(p => p.Price);
46+
totalPrice -= CalculateDiscount(order);
47+
Console.WriteLine($"Order Total: {totalPrice}");
48+
}
49+
50+
static double CalculateDiscount(Order order)
51+
{
52+
double discount = 0;
53+
double totalPrice = order.Products.Sum(p => p.Price);
54+
55+
// Check if the customer is a preferred customer and apply a discount if eligible
56+
if (order.Customer.IsPreferredCustomer)
57+
{
58+
// Apply a 10% discount for preferred customers
59+
discount = totalPrice * 0.1;
60+
}
61+
62+
// Check if the order total exceeds a certain threshold and apply additional discount
63+
if (totalPrice >= 1000)
64+
{
65+
// Apply a 50ドル discount for orders totaling 1000ドル or more
66+
discount += 50;
67+
}
68+
else if (totalPrice >= 500)
69+
{
70+
// Apply a 20ドル discount for orders totaling 500ドル or more
71+
discount += 20;
72+
}
73+
74+
// Ensure the discount does not exceed the order total
75+
discount = Math.Min(discount, totalPrice);
76+
77+
return discount;
78+
}
79+
80+
static void NotifyCustomer(Order order)
81+
{
82+
string shippingLabel = GenerateShippingLabel(order.Customer, order.Address);
83+
Console.WriteLine($"Sending notification to customer {order.Customer.Name}: Your order has been shipped. Shipping label: {shippingLabel}");
84+
}
85+
86+
static string GenerateShippingLabel(Customer customer, Address address)
87+
{
88+
return $"Shipping label for {customer.Name} at {address.Street}, {address.City}, {address.ZipCode}";
89+
}
90+
}
91+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using CSharp.Tutorials.CodeRefactoring;
2+
using CSharp.Tutorials.CodeRefactoring.DesignPatterns;
3+
using CSharp.Tutorials.CodeRefactoring.Samples;
4+
using ExtractMethod = CSharp.Tutorials.CodeRefactoring.ExtractMethod;
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
ProcessOrder();
10+
CalculateAreas();
11+
}
12+
13+
static void ProcessOrder()
14+
{
15+
var order = OrderService.GetOrder();
16+
17+
OrderService.ProcessOrder(order);
18+
19+
ExtractMethod.ProcessOrder(order);
20+
}
21+
22+
static void CalculateAreas()
23+
{
24+
ShapeService.CalculateAreas();
25+
26+
EliminateCodeDuplication.Run();
27+
UseOOP.Run();
28+
FactoryPattern.Run();
29+
StrategyPattern.Run();
30+
}
31+
}
32+
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace CSharp.Tutorials.CodeRefactoring.Samples
2+
{
3+
public class Address
4+
{
5+
public string Street { get; set; }
6+
7+
public string City { get; set; }
8+
9+
public string ZipCode { get; set; }
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace CSharp.Tutorials.CodeRefactoring.Samples
2+
{
3+
class Circle : ShapeBase
4+
{
5+
private double Radius { get; }
6+
7+
public Circle(double radius)
8+
{
9+
Radius = radius;
10+
}
11+
12+
public override double CalculateArea()
13+
{
14+
return Math.PI * Radius * Radius;
15+
}
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CSharp.Tutorials.CodeRefactoring.Samples
2+
{
3+
public class Customer
4+
{
5+
public string Name { get; set; }
6+
7+
public bool IsPreferredCustomer { get; set; }
8+
}
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /