最好大家能给出最明了的例子java与模式中的例子不易体会到它的好处
解决方案 »
- 如何能学好循环?
- 怎样算同一个包,import进来算不算?
- 请问,我想让窗口关闭时向服务器发送一段语句,该如何实现?
- 在JAVA中如何实现像VB中的多文档窗体(MDI)格式?在线等!!
- JMF 我装好了还需要配置不呢?
- 关于tomcat的问题
- java怎么编程其他平台软件呢,比如控制某个硬件设备,是用的java么
- javamail在linux下无法发送mail的问题
- 我是java初学者,请问jbuilder7怎么注册?
- javamail报错:javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25;
- 请问,在线程当中怎么传递消息或数据呀?
- 关于Cache的实现
--
public class PCTest {
public static void main(String[] args) {
CPU IBMcpu = (new IBMPC()).CPUfactory();
HD IBMhd = (new IBMPC()).HDfactory();
CPU HPcpu = (new HPPC()).CPUfactory();
HD HPhd = (new HPPC()).HDfactory();
System.out.println(IBMcpu.model());
System.out.println(IBMhd.model());
System.out.println(HPcpu.model());
System.out.println(HPhd.model());
}
}
--
public class IBMPC implements PC {
public CPU CPUfactory() {
return (new IBMCPU());
}
public HD HDfactory() {
return (new IBMHD());
}
}
--
public class HPPC implements PC {
public CPU CPUfactory() {
return (new HPCPU());
}
public HD HDfactory() {
return (new HPHD());
}
}
--
public class IBMCPU implements CPU {
private int speed;
private String model;
IBMCPU(){
speed = 20;
model = "IBM600";
}
IBMCPU(int speed, String model) {
this.speed = speed;
this.model = model;
}
public int speed() {
return speed;
}
public String model(){
return model;
}
}
--
public class HPCPU implements CPU {
private int speed;
private String model;
HPCPU(){
speed = 10;
model = "HP600";
}
HPCPU(int speed, String model) {
this.speed = speed;
this.model = model;
}
public int speed() {
return speed;
}
public String model(){
return model;
}
}
--
public class IBMHD implements HD {
private int capacity;
private String model;
IBMHD() {
capacity = 5400;
model = "IBM-HD-2";
}
IBMHD(int capacity, String model) {
this.capacity = capacity;
this.model = model;
}
public int capacity() {
return capacity;
}
public String model() {
return model;
}
}
--
public class HPHD implements HD {
private int capacity;
private String model;
HPHD() {
capacity = 5400;
model = "HP-HD-1";
}
HPHD(int capacity, String model) {
this.capacity = capacity;
this.model = model;
}
public int capacity() {
return capacity;
}
public String model() {
return model;
}
}
--
public interface CPU {
public int speed();
public String model();
}
--
public interface HD {
public int capacity();
public String model();
}
1.父类Sample有子类SampleA,SampleB,SampleC.......
public class SampleFactory{
public static Sample sampleFactory(int i){
if(i==1)
return new SampleA();
else if(i==2)
return new SampleB();
......
}
}
得到sampleA:(SampleA)SampleFactory.sampleFactory(1);2.有限的对象来进行分配。你看看ConnectionPools是怎么写的。
Hashtable Pools=new Hashtable(20);
public Connection getConnection(String name)
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
return pool.getConnection();
}
return null;
}