::Z::Thinking::

::Simple::
文章 - 124,收藏 - , 评论 - 49, trackbacks - 0

Martin Fowler

起因:

轻量级容器的兴起。

组件的内聚:轻量级容器能够帮助开发者将来自不同项目的组件组装成一个内聚的应用程序。

轻量级容器的一个共同模式:反转控制(IOC),从模式来分析,更确切描述 依赖注入(DI)

类似模式:Service Locator

目标:将组件的配置使用分离

component和service区别:

组件是本地使用(jar,Dll,src)的,服务是远程使用的(webservice,messaging,RPC,socket)


依赖注入的集中形式:

  • Setter Injection
  • Constructor Injection
  • Interface Injection


轻量级容器:

  • PicoContainer  (Constructor Injection)
  • HiveMind (Constructor Injection)
  • Spring (Setter Injection)
  • Xwork

 PicoContainer:

实现了注册接口的单例服务:

优点:

1,注册组建生命周期控制。start(),stop(),dispose().

2,非强制使用pico 的api在用户的code里面。可以通过自己编写一个类来启动,注册组件,查询组件。

3,轻量级,很容易嵌套在别的应用程序中。

========================Test Code For PicoContainer================================

import org.picocontainer.*;
import org.picocontainer.defaults.ComponentParameter;
import org.picocontainer.defaults.DefaultPicoContainer;

public class PicoTester {
 MutablePicoContainer pico = null;

 public void init() {
  pico = new DefaultPicoContainer();
  pico.registerComponentImplementation(Workable.class, Student.class);
  // Parameter[] paramss = { new ComponentParameter(Workable.class) };
  // pico.registerComponentImplementation(Sellable.class, Teacher.class,
  // paramss);
  pico.registerComponentImplementation(Intern.class, Intern.class);

  pico.registerComponentImplementation(Sellable.class, Teacher.class);
  pico.start();  
  pico.stop();
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  PicoTester tester = new PicoTester();
  tester.init();
  tester.doSyncTest();
  // Sellable seller = (Sellable) pico
  // .getComponentInstanceOfType(Sellable.class);
  // seller.sell();
 }

 public void doSyncTest() {
  Runnable r1 = new Runnable() {
   public void run() {
    print("thread 1 start");
    Workable worker = (Workable) pico
      .getComponentInstanceOfType(Workable.class);
    worker.work();
    print("thread 1 end");
   }
  };

  Runnable r2 = new Runnable() {
   public void run() {
    print("thread 2 start");
    Workable worker = (Workable) pico
      .getComponentInstanceOfType(Workable.class);
    worker.work();
    print("thread 2 end");
   }
  };

  new Thread(r1).start();
  new Thread(r2).start();
 }

 public static void print(String str) {
  System.out.println(str);
 }
}
===========================================


public class Intern implements Workable {
 private String name="intern";
 
 public void work() {
  System.out.println("intern :: work work work!!!");
 }

}
============

public interface Sellable {
 public void sell();
}

==============

import org.picocontainer.*;

public class Student implements Workable,Startable{
 private String name = "";

 public Student() {
  super();
 }

 public synchronized void work() {
  
  System.out.println(Thread.currentThread()+this.toString()+"students: work work work!!! begin!!!");
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println(Thread.currentThread()+this.toString()+"students: work work work!!! end!!!");
 }

 public void start() {
  System.out.println("start called ...");
 }

 public void stop() {
  System.out.println("stop called ...");
 }
}
==========================

public class Teacher implements Sellable {
 private String name = "";

 private Workable stu = null;

 public Teacher() {
  super();
 }

 public Teacher(Workable stu) {
  this.stu = stu;
 }

 public void sell() {
  System.out.println("teacher: recruit student ...");
  stu.work();
 }
}

==================

public interface Workable {
 public void work();
}

===============



Trackback: http://tb.donews.net/TrackBack.aspx?PostId=467042


[点击此处收藏本文]  发表于2005年07月15日 2:50 PM




正在读取评论……