TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abhi Yadav
NA
133
678
Refactor using Solid Principles
Jan 6 2020 5:22 AM
I have this code , i wanted to modify it using SOLID principles where all can i edit it please suggest
class
Program
{
static
void
Main(
string
[] args)
{
List<Figure> figureList =
new
List<Figure>();
Figure rectangle =
new
Figure()
{
Height = 3,
Width = 4,
Type = ShapeType.Rectangle
};
figureList.Add(rectangle);
Figure square =
new
Figure()
{
Height = 4,
Type = ShapeType.Square
};
figureList.Add(square);
Figure circle =
new
Figure()
{
Radius = 3.5,
Type = ShapeType.Circle
};
figureList.Add(circle);
foreach
(Figure figure
in
figureList)
{
Console.WriteLine(figure.CalculateArea());
figure.Save();
}
}
}
public
class
Figure
{
public
double
Height {
get
;
set
; }
public
double
Width {
get
;
set
; }
public
double
Radius {
get
;
set
; }
public
ShapeType Type {
get
;
set
; }
public
void
Save()
{
if
(Type == ShapeType.Circle)
throw
new
Exception(
"Circle cannot be saved!"
);
//save the object to a file
using
(Stream stream = File.Open(
"saveFile.bin"
, FileMode.OpenOrCreate))
{
BinaryFormatter formatter =
new
BinaryFormatter();
formatter.Serialize(stream,
this
);
}
}
public
double
CalculateArea()
{
try
{
if
(Type == ShapeType.Rectangle)
{
return
Height * Width;
}
else
if
(Type == ShapeType.Square)
{
return
Height * Height;
}
else
if
(Type == ShapeType.Circle)
{
return
Radius * Radius * Math.PI;
}
}
catch
(Exception e)
{
if
(e
is
ArgumentNullException)
{
//log to a file
System.IO.File.WriteAllText(
"log.txt"
, e.ToString());
}
else
if
(e
is
ArgumentException)
{
//log to console
Console.WriteLine(e.ToString());
}
}
return
0;
}
}
public
enum
ShapeType
{
Rectangle,
Circle,
Square
}
Reply
Answers (
1
)
What technique can use for automatic sending SMS
How do I use this Custom Panel in wPF?