I'm reading on how to implement design patterns. I'm trying to implement the DIP after reading SOLID principles. I'm a beginner in design patterns.
Application scope:
I'm building a Windows form C# project that keeps some information. It will create a bunch of TextBox
and ComboBox
dynamically, depend upon user input.
In this application, I created multiple class where all the class will have the same behavior. So I thought of creating a abstract class called Component .
All the subclass which is also a form of component will have a is-a relationship with the main class.
On whenever I create a new dynamic component I can just inherit the add_components
class so that the new class will implement the add_dynamic_components
methods.
public abstract class add_components
{
public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
}
public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
txtBox.TabIndex = currentTabIndex * 7 + 2;
}
}
public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
txtBox.TabIndex = currentTabIndex * 7 + 1;
}
}
public class addDynamicSpellboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
{
RichTextBox sp = new RichTextBox();
f.panel1.Controls.Add(sp);
sp.Location = new Point(spPointX, spPointY);
sp.Size = new System.Drawing.Size(230, 43);
f.panel1.Controls.Add(sp);
sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
sp.TabIndex = currentTabIndex * 7 + 3;
}
}
On implementation I found that my form class is tightly coupled with the lower class.
Implementation:
public partial class Form1 : Form
{
addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
addDynamicDateofServiceComponents adddoscomponents = new addDynamicDateofServiceComponents();
public Form1()
{
try
{
InitializeComponent();
}
catch (Exception ex)
{
//Write ex.Message to a file
using (StreamWriter outfile = new StreamWriter(@".\error.txt"))
{
outfile.Write(ex.Message.ToString());
}
}
initalizeUserdefinedComponents();
}
public void createDynamiccomponents()
{
int padding = 24;
int gap = 50;
int value;
if (int.TryParse(txtboxvalLines.Text, out value))
{
int txtno = int.Parse(txtboxvalLines.Text);
int pointX = 125;
int pointY = padding ;
int spPointX = 240;
int spPointY = padding ;
int txtboxRecommendedpointX = 640;
int txtboxRecommendedpointY = padding ;
int btnPointX = 1170;
int btnPointY = padding ;
int cmbPointX = 520;
int cmbPointY = padding ;
int pncPointX = 920;
int pncPointY = padding ;
int finalcmbPointX = 780;
int finalcmbPointY = padding ;
int DOSPointX = 35;
int DOSPointY = padding ;
for (int i = 0; i < txtno; i++)
{
customize_panel();
addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);
adddoscomponents.add_dynamic_components(i, DOSPointX, DOSPointY, this, i);
panel1.Show();
spPointY += gap;
btnPointY += gap;
cmbPointY += gap;
pointY += gap;
pncPointY += gap;
txtboxRecommendedpointY += gap;
finalcmbPointY += gap;
DOSPointY += gap;
}
btnAdd.Enabled = false;
txtboxvalLines.Enabled = false;
panel1.Focus();
}
else
{
MessageBox.Show("Please provide the count of total Claim lines");
txtboxvalLines.Text = "";
}
}
}
In the above case you can see that the form is tightly coupled with the lower class i.e. addDynamicCptboxComponents, addDynamicSpellboxComponents which is seems to be a violation of DIP as far what i learned in internet.
Now my questions are:
- Whether my application violates DIP ? - I believe so. If yes In my case how to implement DIP.
- This is the first principle High-level modules should not depend on low-level modules. Both should depend on abstractions. All over the internet most of them examples have Interface as a method of abstraction. why not an abstract class.
Aucun commentaire:
Enregistrer un commentaire