请看例子:
[java] view plaincopy
public interface Sender {
public void Send();
}
两个实现类:
[java] view plaincopy
public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mailsender!");
}
}
[java] view plaincopy
public class SmsSender implements Sender {
@Override
public void Send() {
System.out.println("this is sms sender!");
}
}
两个工厂类:
[java] view plaincopy
public class SendMailFactory implements Provider {
@Override
public Sender produce(){
return new MailSender();
}
}
[java] view plaincopy
public class SendSmsFactory implements Provider{
@Override
public Sender produce() {
return new SmsSender();
}
}
在提供一个接口:
[java] view plaincopy
public interface Provider {
public Sender produce();
}
测试类:
[java] view plaincopy
public class Test {