Reputation: 3165
This is the Skeleton of my current class
class JokeTemplates
{
public function CheckTemplateCategory()
{
}
public function SubmitUserTemplates()
{
}
Public function ShowUserUploadedTemplates()
{
}
public function ShowUSerFOrwardedTemplates()
{
}
public function ShowAllTemplates()
{
}
}
In future i may have to design many such classes like LoveTemplates,FriendshipTemplates,BirthdayTemplates..etc In which some of the functions like SubmitUserTemplates, ShowUserUploadedTemplates, ShowUSerFOrwardedTemplates may remain same While functionality of some of function Like ShowAllTemplates,CheckTemplateCategory etc. may change.
IF i create all the functions(copy-paste) in my all classes ,it will lead to huge code redundancy. How should i design my code to reduce redundancy.
Upvotes: 1
Views: 295
Reputation: 198204
If i create all the functions(copy-paste) in my all classes ,it will lead to huge code redundancy. How should i design my code to reduce redundancy.
That's what inherianceDocs is for:
You define the Templates
type per it's interfaceDocs:
Interface Templates
{
public function CheckTemplateCategory();
public function SubmitUserTemplates();
Public function ShowUserUploadedTemplates();
public function ShowUSerFOrwardedTemplates();
public function ShowAllTemplates();
}
You create a base templates (I call it TemplatesBase
) that is of type Templates
per the implementation of the Interface. You can start to write actual code here into the function:
class TemplatesBase implements Templates
or alternatively with abstractionDocs:
abstract class TemplatesBase implements Templates
{
public function CheckTemplateCategory()
{
...
}
public function SubmitUserTemplates()
{
}
Public function ShowUserUploadedTemplates()
{
}
public function ShowUSerFOrwardedTemplates()
{
}
public function ShowAllTemplates()
{
}
}
Finally you can extend from that class and only add the function you need:
class JokesTemplates extends TemplatesBase
{
public function ShowUSerFOrwardedTemplates()
{
throw new TemplatesException('Jokes Templates don't support USerFOrwardedTemplates.');
}
}
Upvotes: 3
Reputation: 14479
What you're asking for is the very reason for inheritance and function overriding. No need to over complicate things:
class TemplatesBase
{
public function checkTemplateCategory()
{
return "Base";
}
public function submitUserTemplates()
{
// ...
}
public function showUserUploadedTemplates()
{
// ...
}
public function showUserForwardedTemplates()
{
// ...
}
public function showAllTemplates()
{
// show all templates
}
}
class JokeTemplates extends TemplatesBase
{
public function checkTemplateCategory()
{
return "Jokes";
}
public function showAllTemplates()
{
// show all joke templates
}
}
class LoveTemplates extends TemplatesBase
{
public function checkTemplateCategory()
{
return "Love";
}
public function showAllTemplates()
{
// show all love templates
}
}
Upvotes: 0
Reputation: 4019
Use inheritance.
Create a BaseTemplate class. Implement the common logic (the redundant methods that is) in this Base Class. All other templates should extend the BaseTemplate class.
Upvotes: 0
Reputation: 751
if you use that as the main class. and use other classes to extend it. it will use the functions in the main class unless they are defined in itself.
e.g. if you put ShowallTempates() into the class that extends your example it would use that one rather than the the one in your example. if you dont define it would use the one from your example.
if they must define those fields use an abstract class.
Upvotes: 0
Reputation: 206
Same way you would with any other programming language. If the exact same chunk of code is used multiple time throughout a a program put it in its own function.
Upvotes: 0