Using the Strategy Pattern to Determine OS

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1270 Views - Last Post: 02 August 2011 - 07:32 PM Rate Topic: -----

#1 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 08:30 PM

Hello everyone, I've posted about this in the past but I've been working 2 jobs during the summer so I'm just now getting around to asking questions about the strategy method that was suggested to me.

For those of you who may not have been apart of the previous thread, the reason the Strategy method was suggested was because in my current code I was checking to see what operating system the user was using several times. So hopefully the strategy method will make it where I only have to run the check a single time. When I read up on the Strategy method I got a bit confused. I was reading about it here.

I'm mainly confused on how to initialize and implement it. Can someone give me some advice on where I should start?

Is This A Good Question/Topic? 0
  • +

Replies To: Using the Strategy Pattern to Determine OS

#2 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:11 PM

because i'm so used to programming in windows I can't offer to much advice other than this. I know the biggest difference (that I know of) is in directory structers between linux, mac, and windows. So when it comes to files I would imagine you would want to do something like this

public String getDirectory(String parent, String file)
{
  if (//test for windows)
     return new file("c:\\"+parent+"\\"+file); //yes i know the syntax is not right.
  else if (//test for mac)
     return new file("????"+mac+parent+file);//don't know mac addresses sorry
  else if (//test for linux)
    return new file("~/"+parent+"/"file); //again i know it's wrong sorry
  else
    return "can't find file..sorry"
}

String myFile = getDirectory("immeraufdemhund","somethingCOOL.java");
File coolFile = new File(myFile);



i know i didn't code that perfectly but I hope you get the jist of what i'm talking about. Have a function that can translate what you want into something OS specific and return the results. Then just call your normal functions and calls from their. That is what I would try atleast. I've not done much programming for other OS's though.
Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:14 PM

That's basically what I already have and it causes me to check the operating system multiple times.
Was This Post Helpful? 0
  • +
  • -

#4 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:18 PM

you could make a final string with a get method. Then when you initialize your code it sets up all your variables once then you use your get method to get it.

final File pictureDirectory;
final File dataDirectory;

public void setupVariables()
{
  pictureDirectory = new File("something os specific");
  dataDirectory = new File("dido");
}

public static void main(String[] args)
{
  setupVariables();
}

public File getPictureDirectory()
{
return pictureDirectory;
}




i had a program once that I did this for and it worked out really good when I set up all my folders in a File. because then I was able to just make a new File(pictureDirectory,"mouse.jpg"); and I didn't have to worry about OS stuff at all.

This post has been edited by immeraufdemhund: 01 August 2011 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:25 PM

found it

public class Main extends JFrame implements ActionListener
{
  public static final String APP_DIR,DAT_DIR,BAT_DIR,CHA_DIR,ATK_DIR,DEF_DIR;
  public static final String ADR_DIR,REP_DIR, GLR_DIR, ICO_DIR,GLD_DIR;
  static
  {
    //set up directories
    APP_DIR = System.getProperty("user.dir");
    DAT_DIR = new File(APP_DIR,"Data").getAbsolutePath(); //data path
    REP_DIR = new File(APP_DIR,"Reports").getAbsolutePath(); //report path
    ICO_DIR = new File(APP_DIR,"Icons").getAbsolutePath(); //icons

    BAT_DIR = new File(DAT_DIR,"Battles").getAbsolutePath(); //battle data
    CHA_DIR = new File(DAT_DIR,"Character").getAbsolutePath(); //character data
    GLD_DIR = new File(DAT_DIR,"Guild").getAbsolutePath(); //Guild data

    ATK_DIR = new File(BAT_DIR,"Attack").getAbsolutePath();
    DEF_DIR = new File(BAT_DIR,"Defense").getAbsolutePath();

    ADR_DIR = new File(REP_DIR,"Battle Reports").getAbsolutePath();
    GLR_DIR = new File(REP_DIR,"Weekly Reports").getAbsolutePath();
}
//and in a different file I called this

  public void savePlayer(String name,int[] levels)
  {
    File temp = new File(Main.CHA_DIR,name+".dat");
  //...lots more code stuffs


Was This Post Helpful? 0
  • +
  • -

#6 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:37 PM

I was thinking it might look something like this:

interface strategy()
{
    // Determine which operating system the user is running.
    String OSname = System.getProperty("os.name");
}

public class windowsStrategy()
{
    if (OSname.equals("Windows 7") || OSname.equals("Windows Vista"))
    {
        
    }
    
    else if (OSname.equals("Windows XP"))
    {

    } 
}

public class linuxStrategy()
{
    
}


Was This Post Helpful? 0
  • +
  • -

#7 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:44 PM

you can do that as well. someone correct me if i'm wrong, java will make the required corrections for OS specifics as long as you get it started in the right direction, such as figureing out the OS specific directory your program is in. in windows I know you can do System.getProperty("user.dir");

once you get that it is just a matter of making new files to setup your directories and make them into strings. then you can call what ever file from what ever directory you want, and you only had to call your OS once.
Was This Post Helpful? 0
  • +
  • -

#8 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:47 PM

I think you're a bit mixed up with what I'm trying to do. All I'm trying to do is determine the operating system then call the correct class depending on the operating system in use. I'm not using any directories at all. You've mentioned directories several times but I have yet to show any code where I am using directories.
Was This Post Helpful? 0
  • +
  • -

#9 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 09:57 PM

very sorry. unfortunatly this means it is over my head in terms of what you are trying to do.

but it looks like it is just making multiple classes for each instance and is returning a value of some sort.

and in which case you could do something like

if (windows)
context = new Context(new WindowsStrategy());
else if(mac)
context = new Context(new MacStrategy());
...

context.execute(funclass);

oh and your interface Strategy has to end in a ;

so

interface Strategy 
{
    int execute(int a, int B)/>; //method that has to be in everything that implments this
//maybe I'd sugest something like
    int exectute(String os);
}



Was This Post Helpful? 0
  • +
  • -

#10 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 01 August 2011 - 10:01 PM

I have a ; in my strategy see:

interface strategy()
{
    // Determine which operating system the user is running.
    String OSname = System.getProperty("os.name");
}



It's after the parenthesis.
Was This Post Helpful? 0
  • +
  • -

#11 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 02 August 2011 - 02:50 PM

View PostRandomlyKnighted, on 02 August 2011 - 12:01 AM, said:

I have a ; in my strategy see:

interface strategy()
{
    // Determine which operating system the user is running.
    String OSname = System.getProperty("os.name");
}



It's after the parenthesis.


It was late last night when I posted that, sorry about the strange response I gave you. I read over your previous thread, and this one and I have to clarify 2 things. With a interface every statement either has to declare a constant, or declares a method that HAS to be used in everything that implements it. If you plan out your program enough than you might be able to use it. someone had a awesome clarification for the difference for a extending and implementing something. It was neat because they all implemented washable (ie cloths, a car, a bike). and in the end he could just say MySweather.wash(); and myVan.wash(); and MyHuffy.wash(); although the call was the same it performed the same thing just in different ways. So for your example

you would want to make a WindowsStrategy that implements Stratgey something like this:

public interface Strategy
{
  void deleteHistory();
  void deleteTempFiles();
  void deleteCookies();
}



  private class WindowsStrategy implements Strategy
  {

    @Override
    public void deleteHistory()
    {
      //code for deleteing history for windows only
    }

    @Override
    public void deleteTempFiles()
    {
      //code for deleting all temp files for windows only
    }

    @Override
    public void deleteCookies()
    {
      //code for deleting cookies for windows only
    }
  }


so now you can do what was in the Strategy guide. You can run one test to see which operating system you have.

for the sake of arguments lets say it is windows. in the if windows part you put strategy = new WindowsStrategy();

now in your actionlistener all you have to do is strategy.deleteCookies(); strategy.deleteHistory(); strategy.deleteTemp(); and if that particular OS does'nt have said thing you just leave the overridden part blank. Now it is only called once, it looks clean, and is easy to read.

see were I'm coming from now?
Was This Post Helpful? 0
  • +
  • -

#12 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 02 August 2011 - 02:57 PM

Yes, but what about this. When you say strategy.deleteTemp(); how will it know which file path to use? wouldn't you have to run a OS check again to see which file path to use or would you make a strategy such as this?

public interface Strategy
{
  void deleteWin7History();
  void deleteWinXPHistory();
  void deleteWin7TempFiles();
  void deleteWinXPTempFiles();
}



would I need to do something like that? if i did that then I could just have a class for each different operating system.

This post has been edited by RandomlyKnighted: 02 August 2011 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 02 August 2011 - 03:42 PM

no you wouldn't. The interface Strategy is your verb. Let's go back to the washable interface example

interface washable
{
  void wash();
}



it doesn't matter what I wash, the verb i use is wash to describe what I am doing to that noun. ie I wash my car, I was my windows, I was my cloths. The way you do it is very specific to each one because you wouldn't put your car into a dishwasher, and you wouldn't put your computer in a car wash etc etc. so now with clothes you have different ways of washing clothes for darks, whites, and super soiled cloths. Now you wouldn't tell your washable interface to have a void washColors(), washDarks() etc because that isn't very portable. You just simply wash them. Now you could go about this in a few different ways. You could make a class called clothes and when it extends wash you can have it run a check to see if it is colored or whatever, or you could make x amount of classes for how ever detailed you want to be with your clothes. (which is preferred) but in the end it doesn't matter what class it is, because it implmements washable you are able to wash that particular object.

now for your situation keep the strategy as vague as possible. delete temp, history and cookies. Because in esscense that is what you want to do, doesn't matter which type of computer you are using. When you make your classes though you may want to make like 6 classes that all extend strategy, win7stratgy, winxpStrategy, winVistaStrategy, macStrategy, linuxStrategy, unixStrategy or any other operating system that you have in mind. Then for each one you can be as specific as you want as to how you delete your cookies, history, etc. because each stategy has its own way of deleting files just like each type of clothing has a different way of being washed.

does that help?
Was This Post Helpful? 0
  • +
  • -

#14 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,380
  • Joined: 14-January 10

Re: Using the Strategy Pattern to Determine OS

Posted 02 August 2011 - 04:22 PM

Ok, so this may seem like a dumb question at this point, but here it is. I know that this happens at runtime but how does it know what strategy to use? I mean all we're talking about doing is creating the strategies we haven't told it how to know which strategy to use.
Was This Post Helpful? 0
  • +
  • -

#15 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Using the Strategy Pattern to Determine OS

Posted 02 August 2011 - 05:31 PM

that's actually the E Z part! so you want to make a global variable in your class of type Strategy. Presumably in your main method when you first start the program you want to test for your OS and set that global variable to the appropriate Strategy. ex

public class SuperDeleter
{
  private Strategy MyOS;
  public static void main(String[] args)
  {
     String OperatingSystemString = System.getProperty("os.name");
     if (OperatingSystemString.equals("Windows 7")
       MyOS = new Windows7Strategy();
     else if (OperatingSystemString.equals("Windows Vista")
       MyOS = new WindowsVistaStrategy();
     //...and so on and so on
     else
     {
        System.out.println("Operating System not supported");
        System.exit(1);
     }
   }
}



now anytime you want to do something to said OS you just say MyOS.DoWhatITellYou("NOW!");

you are only calling to find out what OS you want once, you get scalability, you get simplicity, and easy to read!
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2