RuleBritannia Posted January 6, 2014 Share Posted January 6, 2014 Hello. I have many classes all containing identical method names(different coding in each method), Which I control via 1 main class which can call any of them. I am wondering the best place to handle DB interaction. It seems best to handle once in the main class, but one problem can be datetime sql statements not as accurate, Along with possible other issues. If we handle within each of the "many" classes I described earlier, we will get exact timestamps, but then we will have alot of code duplication. I assumed some of you have come across this issue before? In all the books I have read I have not come across a "best" pattern for this. Above all this I am trying to stick with universal class principles(Creating classes which can be used elsewhere independently). Perhaps I am trying to run before I can cry? Thanks for any answers/views in advance. Quote Link to comment Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 Hey, if you create the db object in your "main class", you could then just inject these into classes which need them when you initialize the object. I don't see why timestamps wouldn't be as accurate; don't these work depending on when the actual statement was ran? Quote Link to comment Share on other sites More sharing options...
ignace Posted January 6, 2014 Share Posted January 6, 2014 (edited) Please provide us with a code example of what it is you mean. Edited January 6, 2014 by ignace Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 6, 2014 Author Share Posted January 6, 2014 Hey, if you create the db object in your "main class", you could then just inject these into classes which need them when you initialize the object. I don't see why timestamps wouldn't be as accurate; don't these work depending on when the actual statement was ran? I could inject into the many, But this would be alot of code duplication, each of these "many classes" have around 8 methods, so I would have to inject into 8 methods within many classes. As for timestamps, I dont think it would be a huge difference, But handling the timestamp and updating directly after completition would be more accurate than updating outside of the class as there is room for other things to happen in between Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 6, 2014 Author Share Posted January 6, 2014 (edited) Please provide us with a code example of what it is you mean. Here is a example which should be easy to get the general idea. class A { var $var = 'A'; function get_letter() { return $this->var; } } class B { var $var = 'B'; function get_letter() { return $this->var; } } class C { var $var = 'C'; function get_letter() { return $this->var; } } class D { var $var = 'D'; function get_letter() { return $this->var; } } class letter { var $many; var $letterClass; __construct($value) { $this->letterClass = $value; $this->many = new $letterClass(); } function get_letter() { return $this->many->get_letter(); } } $new = new letter('A'); echo $new->get_letter(); We can handle DB inside the classes A,B,C,D, var $var = 'A'; function get_letter() { $handle = array( 'id' => '1', 'start' => '2010/02/02 04:04:04' ); update($dbobj,$handle,$table); echo $this->var; $handle = array( 'id' => '1', 'end' => '2010/02/02 04:04:05' ); update($dbobj,$handle,$table); } } Also applying same code to B,C,D(duplicate code) or We can handle outside via the main class class letter() function get_letter() { $handle = array( 'id' => '1', 'start' => '2010/02/02 04:04:04' ); update($dbobj,$handle,$table); echo $this->many->get_letter();; $handle = array( 'id' => '1', 'end' => '2010/02/02 04:04:05' ); update($dbobj,$handle,$table); } } This way "seems" best, as no code duplication, But I am wondering if this is a correct pattern to follow. From a pattern/architecture point of view, once things need to be expanded etc, problems may arise?(That I am unaware of at this state in design due to limitations of foresight specifically in OOP) Thanks in advance. Edited January 6, 2014 by RuleBritannia Quote Link to comment Share on other sites More sharing options...
ignace Posted January 6, 2014 Share Posted January 6, 2014 Depends, what does A, B, C, and D do? Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 7, 2014 Author Share Posted January 7, 2014 (edited) Depends, what does A, B, C, and D do? They all perform the same overall task(logging in to another website(A,B,C,D)), but each one will take shorter or longer to do. A B C D will have different requirements within that must be done in order to complete the task.(crsf tokens, redirects, etc) Edited January 7, 2014 by RuleBritannia Quote Link to comment Share on other sites More sharing options...
ignace Posted January 7, 2014 Share Posted January 7, 2014 If you have an algorithm for each website, but only one or two steps differ you can use: http://sourcemaking.com/design_patterns/template_method Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) If you have an algorithm for each website, but only one or two steps differ you can use: http://sourcemaking.com/design_patterns/template_method Hello I believe I have already done this, But my main question is where does db interaction sit within this? I could put the database handling inside the classes which inheirt the template and decide which methods to use. Or I build a gateway and parse all the small classes through this gateway before other stuff? Here is a good comparable example. Do I handle my database interaction within PackageA and PackageB? Or do I build a separate class, Which I parse the output from PackageA,PackageB and so on? This way would abide by the DRY principles, But that still doesn't mean its right. Thanks in advance. Edited January 8, 2014 by RuleBritannia Quote Link to comment Share on other sites More sharing options...
ignace Posted January 8, 2014 Share Posted January 8, 2014 (edited) I would simply pass the DB object through the constructor and let Trip define the default method implementations. You simply refine steps inside PackageA and PackageB. class Trip { private $db; public function __construct(DB $db) { $this->db = $db; } protected function getDb() { return $this->db; } } I could put the database handling inside the classes which inheirt the template and decide which methods to use. This one. I build a gateway and parse all the small classes through this gateway before other stuff? I don't think you know what a Gateway is. A Gateway is to provide access to an external system. Not call local classes and parse it's output, like: class TwitterGateway { public function getTweetsBy($username, $count = null) { .. } } Edited January 8, 2014 by ignace Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 10, 2014 Author Share Posted January 10, 2014 (edited) I would simply pass the DB object through the constructor and let Trip define the default method implementations. You simply refine steps inside PackageA and PackageB. class Trip { private $db; public function __construct(DB $db) { $this->db = $db; } protected function getDb() { return $this->db; } }This one. I don't think you know what a Gateway is. A Gateway is to provide access to an external system. Not call local classes and parse it's output, like: class TwitterGateway { public function getTweetsBy($username, $count = null) { .. } } I meant the term gateway just as a abbreviation, I should have said wrapper. In regards to passing the DB to trips, Does this not break the single responsibility principle? Trying to abide by all these principles isnt easy.(for me anyway). Edit : Nvm, Im just going to get it done, Cannot learn and apply every OOP principle in this space of time, My work is getting nowhere because of this. Thanks for all the help" Edited January 10, 2014 by RuleBritannia Quote Link to comment Share on other sites More sharing options...
ignace Posted January 10, 2014 Share Posted January 10, 2014 (edited) Single responsibility does not mean do only one thing. You are a developer and your responsibility includes: - write code - anlyse code - write tests - estimate issues - update issues - etc.. This is your reponsibility as a Developer. Edited January 10, 2014 by ignace Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.