优化代码原则--学习笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oqzuser1q2w3e4r5t/article/details/73177666

一、单一职责原则

总结来说,一个类中应该是一组相关性秀高的函数 、数据的封装。也就是说,这一个类,应该仅有一个引起它变化的原因。

以我刚刚写出的发送邮件的一个demo为例

private Thread thread = new Thread() { 

@Override public void run() { 

try { send1(); protocol(); //handler.sendEmptyMessage(0); } 

catch (MessagingException e) { e.printStackTrace(); } } }; 

//定义Handler对象 private Handler handler = new Handler() { 

@Override public void handleMessage(android.os.Message m) { super.handleMessage(m); } }; 

private void protocol() {

 try {

 session = Session.getInstance(props,null); 

Transport transport = session.getTransport("smtp"); 

transport.connect("smtp.qq.com","1171546977", "beqaofqqwyeqgbff"); 

Address[] allRecipients = msg.getAllRecipients(); transport.sendMessage(msg,allRecipients); 

runOnUiThread(new Runnable() { 

@Override public void run() {

 ((Button)findViewById(R.id.bt_sendEmailByMail)).setText("发送成功"); } }); } 

catch (MessagingException e) { e.printStackTrace(); } finally { } }

 private void send1() throws MessagingException { 

props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.qq.com");

 props.setProperty("mail.smtp.port", "587"); props.setProperty("mail.smtp.starttls.enable", "true"); 

props.setProperty("mail.smtp.password", "beqaofqqwyeqgbff"); //

 props.setProperty("mail.smtp.timeout", "25"); 不能轻易设置超时 props.put("mail.smtp.auth", "true"); 

final String username = "1171546977"; final String password = "beqaofqqwyeqgbff";

 Session session = Session.getDefaultInstance(props, new Authenticator() { 

protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); 

// -- Create a new message -- session.setDebug(true);

 msg = new MimeMessage(session); msg.setContent("Hello", "text/plain"); 

msg.setHeader("Content-Type", "text/html; charset=UTF-8"); // -- Set the FROM and TO fields -- try { 

msg.setFrom(new InternetAddress(username + "@qq.com"));

 msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse( "[email protected]", false)); 

msg.setSubject("Title"); msg.saveChanges(); } catch (MessagingException e) { e.printStackTrace(); } }



刚写完的代码 用javamail发送邮件主要也就这么几个步骤 ,定义Email各种属性,发送成功或失败应该执行什么。当然中间是开启了线程的,根据单一模式原则我决定 

修改如下 



结果一分为二 分出一个JavaMailHelper类来专门处理javamail的属性配置  然后加入回调  使得activity得到它完成发送后是成功还是失败的信号


代码如下:

public class JavaMailHelper {

private Session session; 

 private Message msg; 

 private Properties props; 

 private Transport transport;

 private OnSendFinishClickListener mListener; 

 public static boolean connected; public JavaMailHelper() {

 transport = null; thread.start(); } 

 private Thread thread = new Thread(new Runnable() { 

 @Override public void run() { init(); } }); 

 private void init() {

 try { props = System.getProperties();

 props.setProperty("mail.smtp.host", "smtp.qq.com");

 props.setProperty("mail.smtp.port", "587"); 

 props.setProperty("mail.smtp.starttls.enable", "true"); 

 props.setProperty("mail.smtp.password", "beqaofqqwyeqgbff"); 

 // props.setProperty("mail.smtp.timeout", "25"); 不能轻易设置超时 

 props.put("mail.smtp.auth", "true"); 

 final String username = "*****************";

 final String password = "******************";

 session = Session.getDefaultInstance(props, new Authenticator() { 

 protected PasswordAuthentication getPasswordAuthentication() { 

 return new PasswordAuthentication(username, password); } });

 // -- Create a new message -- 

 session.setDebug(true); 

 msg = new MimeMessage(session); 

 msg.setContent("Hello", "text/plain");

 msg.setHeader("Content-Type", "text/html; charset=UTF-8");

 // -- Set the FROM and TO fields -- 

 try { msg.setFrom(new InternetAddress(username + "@qq.com"));

 msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse( "***********@qq.com", false)); 

 msg.setSubject("Title"); msg.saveChanges(); session = Session.getInstance(props, null); setTransport(); } catch (MessagingException e) { e.printStackTrace(); } finally { } } catch (MessagingException e1) { e1.printStackTrace(); } }

 public void setTransport() { 

 try { transport = session.getTransport("smtp");

 transport.connect("smtp.qq.com", "*************", "***************"); 

 Address[] allRecipients = msg.getAllRecipients();

 transport.sendMessage(msg, allRecipients);

 if (mListener != null){ 

 mListener.onSendFinishClick(transport.isConnected()); } } catch (MessagingException e) { e.printStackTrace(); } } //这里,我们定义一个接口 public interface 

OnSendFinishClickListener {

 public void onSendFinishClick(boolean connected); } //写一个设置接口监听的方法

 public void setOnObserveSendStateListener(

OnSendFinishClickListener listener) { 

 mListener = listener; }



MainActivity


代码如下: //两种发送邮件方式 

 private JavaMailHelper javaMailHelper; 

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 

 setContentView(R.layout.activity_main); 

 javaMailHelper = new JavaMailHelper(); javaMailHelper.setOnObserveSendStateListener(new JavaMailHelper.OnSendFinishClickListener() { 

 @Override public void onSendFinishClick(final boolean connected) { runOnUiThread(new Runnable() { 

 @Override public void run() { 

 if (connected){ ((Button) findViewById(R.id.bt_sendEmailByMail)).setText("邮件发送成功"); }

else{ ((Button) findViewById(R.id.bt_sendEmailByMail)).setText("邮件发送失败"); } } }); } }); }


代码进行梳理后 自己都感觉看着顺眼多了,而且自我感觉分工比较明确






猜你喜欢

转载自blog.csdn.net/oqzuser1q2w3e4r5t/article/details/73177666