这是什么我已经做Grails中一个非常简单的例子:我在寻找这样做的更多的面向对象的方式,像依赖注入:条件
// this can be a service or normal class
public abstract class Person {
public final String introduceSelf() {
return "Hi, I'm " + getFullName()
}
protected abstract String getFullName()
}
// Service
class AlexService extends Person {
protected String getFullName() {
return "Alex Goodman"
}
}
// Service
class BobService extends Person {
protected String getFullName() {
return "Bob Goodman"
}
}
// Service
class CarlService extends Person {
protected String getFullName() {
return "Carl Goodman"
}
}
// Controller
class IntroduceController {
def alex
def bob
def carl
def index() {
if(params.person == "a")
render alex.introduceSelf()
if(params.person == "b")
render bob.introduceSelf()
if(params.person == "c")
render carl.introduceSelf()
}
}
:
// Controller
class IntroduceController {
def person
def index() {
// inject a proper person in a more object oriented way
render person.introduceSelf()
}
}
你能否建议如何以更加面向对象/动态的方式实现这一点?
对于简单地持有一个类而言,bean是一个好主意吗? – user809790