How to call a method from another class in groovy? [closed] - groovy

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
For Instance, I have a class like this:
class firstOne{
....
def A (){
}
}
class secondOne{
// I need to call and use method A from class firstOne
// even I get error if I try to follow Java like calls
// firstOne method = new firstOne();
// method.A()
}
I already tried http://groovy.codehaus.org/Scripts+and+Classes and http://groovy.codehaus.org/Groovy+Beans but no way. Any kind of suggestion or examples would be really helpful.

I don't see any problem in this:
class FirstOne {
def a() {
println "a"
}
}
class SecondOne {
def b() {
new FirstOne().a()
println "b"
}
}
new FirstOne().a()
println("")
new SecondOne().b()
Output:
a
a
b

This is not specific to Groovy/Grails:
firstOne first = new firstOne()
first.A()
Also you should capitalize the first letter of classes, but not methods (as is best practice in Java).

Related

pass a string by a value in function call [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I would like to pass a string instead of the number. But don't know how to do that.
#define MODULE_A 5
#define MODULE_B 5
void function_2(unsigned moduleId, IdData)
{
if ( MODULE_A == moduleId) // want to compare the string instead of number 5 since both A and B are same.
{
// do something
}
if (MODULE_B == moduleId) //want to compare the string instead of number 5 since both A and B are same.
{
//do something
}
}
void function_1()
{
function_2(MODULE_B, IdData);
}
void function_3()
{
function_1(MODULE_A, IdData);
}
Welcome to do any workaround to archive the expected result. Basically, I have a function_2 that has the 1st element as unsigned integer, But when other function calling, I would like to pass the string inside each if condition but rather the value, since both module IDs are macro defined as the same number. Thank you.

Is this code a good candidate for a lambda function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I have 27 of these in my function:
auto i = m_lbLanguage.AddString(_T("Afrikaans (Afrikaans)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Afrikaans));
i = m_lbLanguage.AddString(_T("Cebuano (Cebuano)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Cebuano));
i = m_lbLanguage.AddString(_T("汉语(简化字)Chinese (Simplified)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::ChineseSimplified));
Is this type of code a good candidate to use a lambda function? I know I can add a regular function, eg:
void Cxxx::AddLanguageToLB(CString strLanguage, LanguageMSA eLanguage)
{
auto i = m_lbLanguage.AddString(strLanguage);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(eLanguage);
}
And the use:
AddLanguageToLB(_T("汉语(简化字)Chinese (Simplified)"), LanguageMSA::ChineseSimplified);
This type of code is a good candidate for a table-driven implementation. Something along these lines, perhaps:
struct {
LPCTSTR name;
LanguageMSA code;
} languages[] = {
{_T("Afrikaans (Afrikaans)"), LanguageMSA::Afrikaans},
{_T("Cebuano (Cebuano)"), LanguageMSA::Cebuano},
// ...
};
for (auto& lang : languages) {
auto i = m_lbLanguage.AddString(lang.name);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(lang.code));
}

static class method or pure functions in node js [duplicate]

This question already has answers here:
ES6 modules: Export single class of static methods OR multiple individual methods
(2 answers)
Closed 4 years ago.
due to my learning, I usually use static class methods and OOP more than pure function and functional programming. Some of my work mates dont understand why and I dont know wich way is better. This is a piece of code for example:
const DynamoDbHelper = class DynamoDbHelper {
static getTableProperties(tableName) {
...
}
static async updateTableReadAndWriteCapacities(tableName, readCapacityUnits, writeCapacityUnits) {
...
}
}
module.exports.DynamoDbHelper = DynamoDbHelper;
can also be write:
module.exports.getTableProperties = (tableName) => {
...
}
module.exports.updateTableReadAndWriteCapacities = async(tableName, readCapacityUnits, writeCapacityUnits) => {
...
}
Wich solution is the better in this case?
Static-only class is antipattern in JavaScript. It's valid only in languages that don't support functions as independent entities.
If a class isn't intended to be instantiated and acts as a namespace, this is what modules are for. The second snippet is how this should be done.

Get the name of the current invoking method at runtime [duplicate]

This question already has answers here:
Groovy, get enclosing function's name?
(5 answers)
Closed 8 years ago.
Is it possible to get the name of the current method in Groovy code?
def myMethod() {
// want to get the string myMethod here
}
Thanks
Tim_yates gives you a link to general solution.
This is shorter way to get method name. May be optimized, leawed as is for the sake of clarity.
class A {
def myMethod() {
for (m in new Throwable().stackTrace) {
if (m.className == this.class.name)
return m.methodName
}
}
}
println new A().myMethod()

best way to create domain design model? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
UPDATE:
what i am looking is should i go create each classes seprately instead of adding getter/setter prop in the class, what i mean by that is:
so in order to create a Visit i should have the following prop in VISIT
VisitName, Purpose, StartDate, EndDate, HostId, HostName, RequesterId, RequeserName
or should i have this:
VisitName, Purpose, StartDate, EndDate, IPerson Host, IPerson Requester
END UPDATE
i need advice/feedback if i am going on the right direction below is the domain model (part of the project not entirly).
i have class called "Visit" in that Visit model i will have basic of visit like name,purpose,start,end date etc... and in that class i also have who will be hosting the visit and who request the visit.
what do you think of the below class?
//aggreate class
public class Visit
{
IVisitBasic _visitBasic;
IPerson _host;
IPerson _requester;
public IVisitBasic VisitBasic
{
get { return _visitBasic; }
set { _visitBasic = value; }
}
public IPerson Host
{
get { return _host; }
set { _host = value; }
}
public IPerson Requester
{
get { return _requester; }
set { _requester = value; }
}
public Visit(IVisitBasic visitBasic, IPerson host, IPerson requester)
{
_visitBasic = visitBasic;
_host = host;
_requester = requester;
}
public Visit() { }
}
It looks ok as a start, but I wouldn't make a hard and fast domain model until you have actually started coding and testing the initial domain model as you will probably find that things don't quite work as expected or there is something that you have missed, or the requirements change, etc.
Other points from a quick look.
What is the purpose of VisitBasic, should it be an abstract base class or the name made clearer?
You may want to make the host into it's own class, it may have information not relevant to the person class, so possibly a sub class of person. But as stated above it may be better to develop it iteratively. And possibly requester as well but probably less likely.
UPDATE RESPONSE:
The standard design now for most things is to add a service layer, i.e. VisitService with a createVisit method, and the properties on the visit object should just link to the host and requester without having any business logic in them. (Hope that answers your question?)

Resources