Factory Pattern

本文介绍抽象工厂模式的基本概念及其在产品生产车间的实现方式,并展示了如何在EJB环境中通过上下文查找服务定位组件及创建实例,同时讨论了JMS中主题会话与消息发布的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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中使用工厂模式



 Context ctx = new InitContext();

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

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

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

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

 

3. JMS 中的工厂方法



 

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);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值