Tarun
Tarun

Reputation: 3165

How should I avoid code redundancy with in multiple classes

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

Answers (5)

hakre
hakre

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 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.');
      }
}

Upvotes: 3

Travesty3
Travesty3

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

Ravi
Ravi

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

encodes
encodes

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

Working Title
Working Title

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

Related Questions