package AbstractFac; public class AbstractFactory { public static void main(String[] args) { // TODO Auto-generated method stub Sender demo=new SendMailFactory().produce(); demo.Send(); demo=new SendSmsFactory().produce(); demo.Send(); } } interface Sender { public void Send(); } class MailSender implements Sender { @Override public void Send() { System.out.println("this is mail sender!"); } } class SmsSender implements Sender { @Override public void Send() { System.out.println("this is sms sender!"); } } interface Provider { public Sender produce(); } class SendMailFactory implements Provider { @Override public Sender produce(){ return new MailSender(); } } class SendSmsFactory implements Provider{ @Override public Sender produce() { return new SmsSender(); } }
2013华为第五届编程大赛 成都初赛题目和解答,布布扣,bubuko.com
原文:http://blog.csdn.net/simon_xia_uestc/article/details/23933507