正在学习中!!!!!
upupupupupup
upupupupupup
解决方案 »
- Sun Download Manager运行报错
- 懂swing的请进
- 新手,请问在jsp页面中要用javabean,那javabean那段代码放在什么地方啊?是直接放在jsp页面中吗?
- 怎么打包成 jar 并且指明清单的主文件??用一条语句
- 时区转换问题,急!!!
- 有个问题向高手请教。
- 白送分100,借宝地一用,很快的
- realplay(代码录入员)请进,thanks
- JBuilder与数据库备份
- 求解,,, 为什么输出结果为 good gbc 10 只有数组改变了?
- Eclipse 3.0 M8 中文显示问题?
- 求具体代码:怎么判断一个字符串是个数字,如果是数字,是int/long/float那一种?
sm = new smsdeal.SmsMethod(this);
public SmsMethod(IOPut ioPut,DataDeal dataDeal, TableDeal tableDeal, PublicFlow pf) {
}
public PublicFlow(DataDeal dataDeal, IOPut ioPut, Util util) {
}在Main中装配
{
datadeal = new DataDeal();
ioPut = new djb.util.IOPut();
util = new djb.util.Util();
tabledeal = new djb.database.TableDeal(); pf = new PublicFlow(dataDeal,ioPut,util);
sm = new smsdeal.SmsMethod(ioPut,dataDeal,tableDeal,pf);
}
比依赖Main清晰
请将这两个名词简单讲解,谢了!
意思怎么解释呢?举个例子吧
如果一个组件A需要B,C,D,E等组件,那么采用Contructor Injector就是
class A {
A(B b, C c,D d, E e) {
}
}
把需要的组件在Constructor中声明来,然后使用PicoContainerPicoContainer container = new DefaultPicoContainer();
container.registerComponent(A.class);
container.registerComponent(B.class);
container.registerComponent(C.class);
container.registerComponent(D.class);
container.registerComponent(E.class);
A a = (A) container.getComponentInstance(A.class);pico会根据A的声明,自动装配出A的实例
container.registerComponent(A.class);
container.registerInstance(B.class, BInstance);
container.registerInstance(C.class, CInstance);
container.registerInstance(D.class, DInstance);
container.registerInstance(E.class, EInstance);
------------------------------------------------------
public class test {
A a;
B b;
C c;
public test() {
a = new A(this);
b = new B(this);
c = new C(this);
}
public static void main(String args[]) {
test t = new test();
}}class A {
B b;
C c;
public A(test t) {
b = t.b;
Print.print(b);
c = t.c;
Print.print(c);
}
}class B {
A a;
C c;
public B(test t) {
a = t.a;
Print.print(a);
c = t.c;
Print.print(c);
}
}class C {
A a;
B b;
public C(test t) {
a = t.a;
Print.print(a);
b = t.b;
Print.print(b);
}
}
class Print{
public static void print(Object obj){
System.out.println(obj);
}
}
------------------------------------------------------
结果:
[email protected]@1a125f0smsdeal.B@181afa3
------------------------------------------------------
而要是将需要调用的对象前移,就不会出现上述问题
------------------------------------------------------
public class test {
A a;
B b;
C c;
D d;
E e;
public test() {
d = new D();
e = new E();
a = new A(this);
b = new B(this);
c = new C(this);
}
public static void main(String args[]) {
test t = new test();
}}class A {
D d;
E e;
public A(test t) {
d = t.d;
Print.print(d);
e = t.e;
Print.print(e);
}
}class B {
D d;
E e;
public B(test t) {
d = t.d;
Print.print(d);
e = t.e;
Print.print(e);
}}class C {
D d;
E e;
public C(test t) {
d = t.d;
Print.print(d);
e = t.e;
Print.print(e);
}}
class D{
public D(){}
}
class E{
public E(){}
}class Print{
public static void print(Object obj){
System.out.println(obj);
}
}------------------------------------------------------
结果:
[email protected]@[email protected]@[email protected]@181afa3
------------------------------------------------------
结论:
构造函数创建对象需要经过两个过程
1:给该对象分配内存空间
2:调用该对象的初始化函数(即调用父类缺省构造函数,然后调用自己的构造函数)
当我们将this传入各类(如类A)构造函数的时候,由于对主类test对象t的构造只进行了第一步,所以this只代表了对象t的内存地址,所以此时b和c都是null。
所以,如果要用到主类中其它对象时,必须得等到该对象(并非主类对象)完成创建后(即两步都完成),才能调用。
把constructor injector改为setter injector,也就是mediator patterninterface IMediatorAware {
void setMediator(Mediator m);
}class A implements IMediatorAware;
class B implements IMediatorAware;
class C implements IMediatorAware;class Mediator {
A a;
B b;
C c;
Mediator() {
A a = new A();
B b = new B();
C c = new C();
a.setMediator(this);
b.setMediator(this);
c.setMediator(this);
}
}然后ABC在setMediator中拿数据,可以取消顺序耦合
把constructor injector改为setter injector,也就是mediator pattern
=〉
把constructor injector改为setter injector,实现mediator patternsetter injector和mediator pattern没有必然联系,这里用setting injector实现mediator pattern