PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

[FIXED] Where to write the common logic in Java Strategy design pattern?

 October 26, 2022     design-patterns, java, oop, software-design, strategy-pattern     No comments   

Issue

This is related to Java Strategy design pattern.

In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object.

I have common code logic to be executed for all the strategies which is have implemented using Java Strategy design pattern. Which is the right place to write this common logics(something like validations and other stuffs).

Consider the below code. Here I want to do file validation which is common across any file type . Something like , the file should exist and its size should be greater than zero and file name validation. All these file related common stuff I want to keep in some place. Which could be a right design for this?

//BaseFileParser.java
public abstract class BaseFileParser{
  public abstract void parseFile();
}

//XMLFileParser.java
public class XMLFileParser extends BaseFileParser{
  public void parseFile(){
    //Logic for parsing an XML file goes here
  } 
}

//CSVFileParser.java
public class CSVFileParser extends BaseFileParser{
  public void parseFile(){
    //Logic for parsing a CSV file goes here
  } 
}

//Client.java
public class Client{
  private BaseFileParser baseFileParser;
  public Client(BaseFileParser baseFileParser){
    this.baseFileParser=baseFileParser;
  }  
  public void parseFile(){
    baseFileParser.parseFile();
  } 
  public static void main(String args[]){
    //Lets say the client needs to parse an XML file
    //The file type(XML/CSV) can also be taken as 
    //input from command line args[]
    Client client=new Client(new XMLFileParser());
    client.parseFile();
  }
}

Solution

If you have common behaviour, then abstract class or class is what we can use. So basic idea is to put common logic into some base common strategy class. Then we should create abstract method in abstract class. Why? By doing this, subclasses will have particular logic for concrete strategy.

I am sorry, I am not Java guy, but I've provided comments how it can be implemented in Java. Let me show an example via C#.

This is our abstract class which has common strategy:

public abstract class BaseStrategy
{
    // I am not Java guy, but if I am not mistaken, in Java,
    // if you do not want method to be overriden, you shoud use `final` keyword
    public void CommonBehaviourHere() 
    {  }

    public abstract void 
        UnCommonBehaviourHereShouldBeImplementedBySubclass();

}

And its concrete implementations:

public class StrategyOneSubclass : BaseStrategy // extends in Java
{
    public override void 
        UnCommonBehaviourHereShouldBeImplementedBySubclass()
    {
        throw new NotImplementedException();
    }
}

public class StrategyTwoSubclass : BaseStrategy // extends in Java
{
    public override void 
        UnCommonBehaviourHereShouldBeImplementedBySubclass()
    {
        throw new NotImplementedException();
    }
}

UPDATE:

This is your abstract class:

public abstract class BaseFileParser
{
    // I am not Java guy, but if I am not mistaken, in Java,
    // if you do not want method to be overriden, you shoud use `final` keyword
    public bool IsValid()
    {
        return true;
    }

    public abstract void ParseFile();
}

and its concrete implementations:

public class StrategyOneSubclass : BaseStrategy // extends in Java
{
    public override void ParseFile()
    {
        if (!IsValid())
            return;

        throw new NotImplementedException();
    }
}

public class StrategyTwoSubclass : BaseStrategy // extends in Java
{
    public override void ParseFile()
    {
        if (!IsValid())
            return;

        throw new NotImplementedException();
    }
}


Answered By - StepUp
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing