15-46
 1.抽象工厂模式

//产品生产车间1

public interface Producer1{

public ... createProducer1Method1();

public ... createProducer1Method2();

}

//产品生产车间2

public interface Producer2{

public ... createProducer2Method1();

public ... createProducer2Method2();

}

//产品生产车间生产抽象工厂

public abstact class Factory{

public Producer1 genProducer1(){//默认实现};

public Producer2 genProducer2(){//默认实现};

}

public class FactoryImpl1 extends Factory{

public Producer1 genProducer1(){

super.genProducer1();

//附件实现

};

//默认采用父类genProducer2()实现

}

 

 

2.EJB中使用工厂模式


Factory Pattern
 Context ctx = new InitContext();

//调用工厂方法获取Home接口

 MyHome myHome = ctx.lookup("myHome");

//调用产品工厂方法生产产品

MyInteface myInterface = myHome.create(...);

 

3. JMS 中的工厂方法


Factory Pattern
 

Context ctx = new InitContext();

//获取工厂类实例

Topic topic = (Topic)ctx.lookup("topic");

TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("tpc");

//利用工厂类创建connection

TopicConnection tc = tcf.createTopicConnection();

//调用工厂方法创建session

TopicSession ts = tc.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

TopicPublisher tp = ts.createTopicPublisher(topic);

TextMessage tm = ts.createTextMessage("something");

tp.publish(tm);

 

相关文章: