Ok, i think my head is about to explode.
I'm trying to create a framework for a couple of applications that i'm rewriting from vb6. I've read a little bit about factory design patterns and decided i'd try and implement it.
I have created a business entity and given it a private constructor (So no myObject m = new myObject()) i've also given it a static CreateObject method to create new instances.
What i would like to do is build as much functionality (Such as logging and security) into a base class and have my other business objects automatically inherit that functionality without the need to program it into my derived classes. The only way i could think to do it was :-
employee emp = employee.CreateEmployee();
emp.setfieldvalues.....
emp.Update.
Upon calling emp.Update (Which incidently is a public method in the base class), a log file will be updated saying that the record was being updated, then call a private method called UpdateMe (using reflection) on the employee which would then actually write the record to the database.
Now i'm not sure that any of that even makes sense and it wouldn't supprise me if it was bad programming practice. If it is acceptible to do this, is there a way a forcing my derrived classes to implement the private method UpdateMe ?
Anyone?? Anyone??
Thanks.
Loading
Satya DevPosted Sep 15, 2008, 5:21 PM
Satya DevPosted Sep 15, 2008, 5:20 PM
Wedon't have to call "UpDateDerived" explicietely as it is being called from the Update() method of the base Employee class automatically. The client program just calls "Update" on the HourlyEmployee and every thing will be taken care automatically.
Here is how things will work:
in the client code (may be a Main method) it may be like this:
Employee emp = new HourlyEmployee();
emp.Update();
The "Update" method is only defined in the "Employee" base class only. If the Update is called on the derived class object of "Employee", in our example "HourlyEmployee" then base "Employee" class's "Update" is called. Not only that as the base class's "Update" method calls into "UpdateDerived" method which is just declared (abstract with no code) in the base class but actuall code is there in the derived class, the derived class gets the oppertunity to do its own extra Updates() in its "UpdateDerived" method that it implements. So it is like whne "Update" method on the derived class object is called, then base classes's Update is called it finishes it job and hands control over to the derived class to do the extra stuff.
I hope I explained well
Matthew BarnettPosted Sep 15, 2008, 4:34 PM
EDIT: Sorry scrub that question. I missed the line - protected abstract void UpdateDerived();.
Thanks, Matt.
Matthew BarnettPosted Sep 15, 2008, 4:29 PM
So if i'm understanding this correctly. When i create an instance of HourlyEmployee using say a static method called HourlyEmployee.CreateNewEmployee() i can then call Update (Based in Employee), this would then do it's stuff then finally call UpdateDerived on my HourlyEmployee almost like returning full circle and that the 'Protected' access modifier should stop the method from showing up in intellisense on my winform ?
Satya DevPosted Sep 15, 2008, 4:02 PM
I think you need to use "template" method pattern. I don't know VB, so I am giving it in C# but concept should be the same
Base Class:
public abstract class Employee
{
public void Update()
{
Log("Updating employee");
WriteToDatabase();
// Do updations Now:
Update1();
Update2();
// Following is the Template Method Call. We could call this either before
// updations (above ones) or here i.e. after the base class updation is done
// It depends on the requirement
UpdateDerived(); // Hook (Template Method) into the Derived class
}
// This template method is made "abstract", so that the derived classes "must"
// implement it if they don't want to be abstract. However a default "body with no code"
// could also be given. In that case this "Employee" class is nolonger abstract
protected abstract void UpdateDerived();
}
public class HourlyEmployee(): Employee
{
protected override void UpdateDerived()
{
// Do your derived class updations here
}
}