1. 程式人生 > >簡單工廠模式的優化

簡單工廠模式的優化

之前部落格簡單說了一下簡單工廠模式,http://blog.csdn.net/guideit/article/details/52227970。

但是有個不好地方,他的邏輯程式碼類使用了靜態物件針對這個,我對之前程式碼進行了優化,直接飆程式碼。

介面:

package com.example.hujhguiyhiu.test_demo.DaHuaMoShi_2;

/**
 * Created by hujhguiyhiu on 2016/8/16.
 */
public interface FengZhuang2 {
    //虛方法,主要就是在繼承者們那進行邏輯運算
public double yunSuan(double 
a,Integer b); }
之前這個是一個封裝類。但是優化的時候,覺得,資料還是直接傳比較方便就修改了。
邏輯類程式碼:
package com.example.hujhguiyhiu.test_demo.DaHuaMoShi_2;

import android.util.Log;


/**
 * Created by hujhguiyhiu on 2016/8/16.
 */
public class YunSuanIpm2 {
    public  final String TYPE_SY = "SY";
    public  final String TYPE_ZLX = "ZLX";
    public  final 
String TYPE_FP = "FP"; FengZhuang2 fz = null; public YunSuanIpm2(String type, double a, Integer b) { switch (type) { case TYPE_FP: fz = new fpYunSua(); fz.yunSuan(a, b); break; case TYPE_SY: fz = new
syYunSua(); fz.yunSuan(a, b); break; case TYPE_ZLX: fz = new zlxYunSua(); fz.yunSuan(a, b); break; } } public class syYunSua implements FengZhuang2 { @Override public double yunSuan(double a, Integer b) { Log.d("test", "a=" + a + ",b=" + b); double t = 0; t = a + b; Log.d("test", "sy總計:" + t); return t; } } public class zlxYunSua implements FengZhuang2 { @Override public double yunSuan(double a, Integer b) { Log.d("test", "a=" + a + ",b=" + b); double t = 0; t = a + b; Log.d("test", "zlx總計:" + t); return t; } } public class fpYunSua implements FengZhuang2 { @Override public double yunSuan(double a, Integer b) { Log.d("test", "a=" + a + ",b=" + b); double t = 0; t = a + b; Log.d("test", "fp總計:" + t); return t; } } }
這裡是重點,構造方法取代了普通方法,並且獲取資料,這樣一來,就可以去除靜態物件了。
主函式:
package com.example.hujhguiyhiu.test_demo.DaHuaMoShi_2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.hujhguiyhiu.test_demo.DaHuaMoShi_1.FengZhuang;
import com.example.hujhguiyhiu.test_demo.DaHuaMoShi_1.YunSuanIpm;
import com.example.hujhguiyhiu.test_demo.R;

/**
 * Created by hujhguiyhiu on 2016/8/16.
 */
public class DHMS_Activity2 extends AppCompatActivity {
    private FengZhuang2 fz;
    private EditText editText1;
    private EditText editText2;
    private Button bt1;
    public final String TYPE_SY = "SY";
    public final String TYPE_ZLX = "ZLX";
    public final String TYPE_FP = "FP";

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dhms1);
        editText1 = (EditText) findViewById(R.id.et1);
        editText2 = (EditText) findViewById(R.id.et2);
        bt1 = (Button) findViewById(R.id.bt1);
        //型別
bt1.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View view) {
                double numA = Double.valueOf(editText1.getText().toString());
                int numB = Integer.valueOf(editText2.getText().toString());
                YunSuanIpm2 yst = new YunSuanIpm2(TYPE_FP, numA, numB);
            }
        });
    }
}

優化之後,不單單是把靜態物件的隱患給消除了,而且還加強了解耦。