Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 8th, 2006, 2:38 AM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
variable type

Is there any variable type to possibly hold commands? I know it sounds like an odd request... Something like this:

WhateverThatTypeWouldBeCalled someCommands=new WhateverThatTypeWouldBeCalled{
    here's a command;
    here's another;
    and yet another!;
};

I know it looks a lot like just defining a method, but I want to be able to handle that variable like any other variable--like be able to put it into lists or copy it into another variable.

Once again, I know this sounds like an odd request, but any help would be appreciated.
Writlaus is offline   Reply With Quote
Old Jan 8th, 2006, 6:03 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
I don't think so, and if it was possible I would never use it. To do something like it, put the class in the same class that function is in. You will only need a few extra characters .
Polyphemus_ is offline   Reply With Quote
Old Jan 8th, 2006, 9:39 AM   #3
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
I'm curious as to why you would want to? I'm not hacking on you, but I can't see any need to do that. There was a timw when Iwanted to be able to dynamically name instances (which sounds sort of similar to what you want to so. You basically want to dynamically name methods, whether or not you realize it), until it was pointed out to me that my design had a flaw, and I was stupid.

So maybe if you explain what you are trying to do, or why, we can help you figure out a more conventional method of accomplishing it.

EDIT: Just to clarify.. I am not calling you stupid. I was merely reporting what I was told.
__________________
HijackThis Team-SFDC

Last edited by groovicus; Jan 8th, 2006 at 10:07 AM.
groovicus is offline   Reply With Quote
Old Jan 8th, 2006, 9:45 AM   #4
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
Could you tell us a little more about what you are doing and why? It might help us make alternate suggestions. From what you've written it seems like you want something to hold a list of command strings. Is that correct? Are you testing some input to see if it matches any command strings or something else?
alcdotcom is offline   Reply With Quote
Old Jan 10th, 2006, 12:43 AM   #5
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Quote:
Just to clarify.. I am not calling you stupid. I was merely reporting what i was told
No worries; I understand =)
Quote:
Could you tell us a little more about what you are doing and why?
Yeah, I guess I should.

What I'm trying to do is make a mostly passive screensaver-like program that can switch randomly between different methods. That's the reason I want to be able to put the methods into lists--So I can choose randomly from them.

I know I could use a switch/case loop, but... I don't know. I guess it just seems to me that there would be a variable to handle commands.
Writlaus is offline   Reply With Quote
Old Jan 10th, 2006, 5:39 PM   #6
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
Quote:
Originally Posted by Writlaus
No worries; I understand =)
Yeah, I guess I should.

What I'm trying to do is make a mostly passive screensaver-like program that can switch randomly between different methods. That's the reason I want to be able to put the methods into lists--So I can choose randomly from them.

I know I could use a switch/case loop, but... I don't know. I guess it just seems to me that there would be a variable to handle commands.
Hmm. You're saying "commands", but I'm still not clear on what you mean by that. I think I understand what you want the program to do though, You want it to randomly call different methods. If it were a screensaver, these different methods could output different types of graphics (e.g. flying toasters, marquee, starfield simulation, etc) that change randomly at a predetermined interval or upon a command from the user. There is not a way to hold a method in a variable in Java. You could hold the String value of that method, but there is no way (that I know of) to execute it as a method. As I mentioned in another post, a great way to do what you are trying to do is via the Strategy design pattern.
From the site:
Quote:
The Strategy pattern
In Chapter 1 of the Gang of Four's (GOF) Design Patterns, the authors discuss several OO design principles comprising the core of many patterns. The Strategy pattern embodies two such principles—encapsulate the concept that varies and program to an interface, not an implementation. The Design Patterns authors define the Strategy pattern as:

Define a family of algorithms, encapsulate each one, and make them interchangeable. [The] Strategy [pattern] lets the algorithm vary independently from clients that use it.

The Strategy pattern lets you build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes your software much more extensible, maintainable, and reusable.
The boldface was added by me. You may feel that design patterns are a bit too advanced (or not), but I really wish that my early OOP classes had put more emphasis on them (or even mentiond that the exist for that matter!). Those classes taught me the basics of OOP, but they didn't tell me that it's still not enough to write good programs. OOP is not a perfect model. Design paterns are basically lessons learned over the last 20 somthing years about OOP and how to make it work for you without having a maintenence nightmare. I highly recommend looking into them as early as you feel you can because they can help you immensely.

Edit: Some might recommend making all of your different methods static (which is basically global) and then calling them. I'm not going to lie - that would work. But it's poor programming practice, in my opinion.
__________________
Java Blog
alcdotcom is offline   Reply With Quote
Old Jan 10th, 2006, 5:53 PM   #7
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
public void MyFun(String ab, String cd, String ef)
{
    String z = ab;
    String x = cd;
    String y = ef;
}
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Jan 10th, 2006, 11:05 PM   #8
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Quote:
You could hold the String value of that method
What is the String value of a method?
Writlaus is offline   Reply With Quote
Old Jan 11th, 2006, 5:31 AM   #9
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
The string representation of a method is another way of saying it. Here's an example:

Here's the method
public void doStuff
{
   //do stuff
}

Here's the String representation
String methodString = 
   "public void doStuff() \n" + 
   "{\n" +
   "   // do stuff \n" +
   "}";

It's really beside my point though.
__________________
Java Blog
alcdotcom is offline   Reply With Quote
Old Jan 11th, 2006, 7:39 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Writlaus
I know it looks a lot like just defining a method, but I want to be able to handle that variable like any other variable--like be able to put it into lists or copy it into another variable.
Anonymous objects?

public interface Command {
    public void invoke();
}

public void main(String[] args) {
    List someCommands = new Vector<Command>();

    // Add a command that prints out "Hello World"
    someCommands.add(new Command()
    {
        public void invoke() {
            System.out.println("Hello World");
        }
    });

    // Execute all commands
    for (Command command : someCommands) {
        command.invoke();
    }
}
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC