Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would start by separating the domain model (cheques, banks, etc) from the view (the way the cheques are printed). This is the basic idea behind the <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow">MVC</a> pattern and one of its aims is to allow the same domain model to be displayed in different ways (which seems to be your case). So, I would first create the domain classes, something like:</p> <pre><code>class Cheque { protected $bank; protected $money; ... } class Bank {...} </code></pre> <p>Note that these classes are the "M" of the MVC triad and implement the logic of your domain model, not the behavior related to the rendering process. The next step would be to implement the View classes used to render a cheque. Which approach to take heavily depends on how complex your rendering is, but I would start by having a <code>ChequeView</code> class that renders the common parts and that delegates to other sub-view the specific parts that can change (in this case the date):</p> <pre><code>abstract class ChequeView { protected $cheque; protected $dateView; public function __construct($cheque) { $this-&gt;cheque = $cheque; $this-&gt;dateView = //Whatever process you use to decide if the payment date is shown or not } public function render() { $this-&gt;coreRender(); $this-&gt;dateView-&gt;render(); } abstract protected function coreRender(); } class BankACheckView extends ChequeView { protected function coreRender() {...} } class BankBCheckView extends ChequeView { protected function coreRender() {...} } abstract class DateView { abstract function render() } class ShowDateView extends DateView { function render() {...} } class NullDateView extends DateView { function render() {...} } </code></pre> <p>And, if there is code to reuse across subclasses, you can of course factor them in <code>ChequeView</code> and make <code>coreRender()</code> call them. </p> <p>In case your rendering turns to be too complex, this design may not scale. In that case I would go for splitting your view in meaningful subparts (e.g. <code>HeaderView</code>, <code>AmountView</code>, etc) so that rendering a cheque becomes basically rendering its different sub-parts. In this case the <code>ChequeView</code> may end basically working as a <a href="http://www.oodesign.com/composite-pattern.html" rel="nofollow">Composite</a>. Finally, if you reach this case and setting up the <code>ChequeView</code> turns out to be a complex task you may want to use a <a href="http://www.oodesign.com/builder-pattern.html" rel="nofollow">Builder</a>.</p> <p><strong>Edit based on the OP comments</strong></p> <p>The Builder is mostly used when the instantiation of the final object is a complex process (e.g. there are many things to sync between the sub-parts in order to get a consistent whole). There is generally one builder class and different clients, that send messages (potentially in different orders and with different arguments) to create a variety of final objects. So, while not prohibited, it is not usual to have one builder per type of object that you want to build.</p> <p>If you are looking for a class that represents the creation of a particular instance you may want to check the <a href="http://www.oodesign.com/factory-pattern.html" rel="nofollow">Factory</a> family of patterns (maybe the <a href="http://www.oodesign.com/abstract-factory-pattern.html" rel="nofollow">Abstract Factory</a> resembles closely to what you had in mind).</p> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload