1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tarun
  • 3,162
  • 3
  • 29
  • 45

5 Answers5

3

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 inheriance­Docs is for:

You define the Templates type per it's interface­Docs:

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 abstraction­Docs:

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.');
      }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 2
    You may want to define `TemplatesBase` as `abstract`. Depends on whether the base template may be used without more specific functionality provided by subclasses. – Vitamin Feb 23 '12 at 17:46
  • Can you please make it bit more clear why did you opt for abstract class rather than a base class – Tarun Feb 23 '12 at 19:23
  • Abstract allows more control over the classes that extend from it, but you don't need to make the base class abstract. You can fully decide on your own. Often it's common to do so, so it remains a base type that never can be instantiated directly. This often is the kind of control that's wanted. It's clear it's a base, it's clear it is there for extension, so it's clear it contains base code like a *template*. And that's a pattern as well: [Template method pattern](http://en.wikipedia.org/wiki/Template_method_pattern) – hakre Feb 23 '12 at 19:41
  • thanks a ton hakre but one last question over this,what if my jokestemplate class was extending another class named Model. – Tarun Feb 25 '12 at 14:54
  • PHP only supports single inheritance. If a template is a `Model` as well, then you need to make a base class extending the `Model` as well `class TemplatesModelBase extends Model implements Templates`. But as you can see, things can become uneasy with extends. You're probably looking for more loose coupling like the [Decorator Pattern](http://en.wikipedia.org/wiki/Decorator_pattern) and better even think twice before using extend: Does a Template really needs to be a Model? Why is that so? You probably don't want any `Model`s but `ValueObject`s you can pass along... ;) – hakre Feb 25 '12 at 15:01
  • See [SOLID](http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) and probably specifically [*Single responsibility principle* (SRP)](http://en.wikipedia.org/wiki/Single_responsibility_principle) – hakre Feb 25 '12 at 15:03
0

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.

Working Title
  • 254
  • 7
  • 28
  • That same chunk of code is being repeated through out all other classes as well,if i create functions in all those classes it will lead to code redundancy – Tarun Feb 23 '12 at 17:22
0

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.

encodes
  • 741
  • 4
  • 18
0

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.

Ravi
  • 3,719
  • 6
  • 28
  • 40
0

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
    }
}
Travesty3
  • 14,351
  • 6
  • 61
  • 98