码迷,mamicode.com
首页 > 其他好文 > 详细

23种设计模式

时间:2017-07-11 11:50:30      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:send   out   int   cto   src   equals   this   模式   imp   

1.普通工厂模式。

首先由以下几个类,工厂类,接口,接口实现类,测试类。

接口:定义一个接口

接口实现类:实现上面的接口

工厂类:创建接口实现类的对象。

以下是下面的逻辑图

技术分享

定义一个接口:

      interface Sender{

        public void send();
      }

定义接口实现类:

  1. public class MailSender implements Sender {  
  2.     @Override  
  3.     public void Send() {  
  4.         System.out.println("this is mailsender!");  
  5.     }  

 

  1. public class SmsSender implements Sender {  
  2.   
  3.     @Override  
  4.     public void Send() {  
  5.         System.out.println("this is sms sender!");  
  6.     }  
  7. }  

建立工厂类:

  1. public class SendFactory {  
  2.   
  3.     public Sender produce(String type) {  
  4.         if ("mail".equals(type)) {  
  5.             return new MailSender();  
  6.         } else if ("sms".equals(type)) {  
  7.             return new SmsSender();  
  8.         } else {  
  9.             System.out.println("请输入正确的类型!");  
  10.             return null;  
  11.         }  
  12.     }  
  13. }  
      1. public class FactoryTest {  
      2.   
      3.     public static void main(String[] args) {  
      4.         SendFactory factory = new SendFactory();  
      5.         Sender sender = factory.produce("sms");  
      6.         sender.Send();  
      7.     }  
      8. }

 

23种设计模式

标签:send   out   int   cto   src   equals   this   模式   imp   

原文地址:http://www.cnblogs.com/raoziming18/p/7149764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!