Jump to content

Design Pattern Help: SimpleFactory is overkill in my scenario?


alexweber15

Recommended Posts

Here's some brief context:

 

1) User fills out one of 2 forms (both share 10 common fields, each form has an additional 5 specific fields - so both have 15 fields total)

2) Form gets posted, validated, etc, etc

3) Objects are created according to a valid form submission for manipulation by other parts of the system and the database layer...

 

Each form (FinanceLead and InsuranceLead) corresponds to a different variant of a similar object (a Qualified Lead) and given their similarity and "is-a" relationship, I initially wanted to model this using Inheritance, with both FinanceLead and InsuranceLead inheriting from a Lead class (which would include all the common elements, leaving the specifics of each type to its own class).

 

However, I've been advised that this strong coupling could lead to problems in the future and with the help of posters on this board I've we've up with an alternative class model based on an interface and no inheritance, which should solve the strong coupling issue.

 

Ok, now for the interesting part:

With the initial inheritance-based model, I opted to use a SimpleFactory design pattern.  In short:

1) SimpleLeadFactory: creates leads on demand, based on class constants that determine the LeadStrategy (Insurance or Finance), so basically the final SimpleLeadFactory would use a static methods to create and retrieve the leads on demand.

 

but...

 

2) Without inheritance, this no longer seems necessary... so i'm going through a design pattern crisis here... I don't want to use one just for the sake of using one, but really, can my code scale well and be extensible and future-proof without adopting some proven design pattern or methodology????

 

Can anyone please comment, suggest, anything to help me out please?

 

Here's what I have so far (includes and such left out for readability):

interface LeadStrategy{
// list of required functions
public function doSomething();
}

class LeadSeguro implements LeadStrategy{
// specific insurance attributes
private $attribute;

### Constructor and Destructor ###
public function __construct(){
	// do something
}

public function __destruct(){
	// do something
}

### Implementation of Interface Functions ###

public function doSomething(){
	echo 'Lead Seguro doing something';
}
}

class LeadFinanciamento implements LeadStrategy{
// specific financiamento attributes
private $attribute;

### Constructor and Destructor ###
public function __construct(){
	// do something
}

public function __destruct(){
	// do something
}

### Implementation of Interface Functions ###

public function doSomething(){
	echo 'Lead Financiamento doing something';
}
}

class Lead{
// specific Lead object
private $theLead;
// generic attributes
private $attribute;

### Constructor and Destructor ###

public function __construct(LeadStrategy $type){
	// todo: check if data matches hinted type
	$this->theLead = $type;
}

public function __destruct(){
	// nothing yet
}

### Implementation of Interface Functions ###

public function doSomething(){
	$this->theLead->doSomething();
}
}

 

Thanks and sorry for the super long post! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.