Factory Method Pattern :

提供產生物件的方法, 並讓其子類別決定產生何種類型的實體, Factory Method讓物件的產生延遲到子類別中.

 
Simple Factory Pattern 相同的例子, 同一種Dialog但在不同情況下必須有不同組合模式
Factory Method Pattern 和 Simple Factory Pattern 的不同在於 Simple Factory Pattern 將產生物件的動作全部放在單個類別中(Factory),
對於建構過程簡單的物件而言已經足夠,但缺點是若需要新增其他類別就必須修改Factory中的方法, 不符合 ocp,
解決方法可以把 Factory Method Pattern 當作 Simple Factory Pattern進階版, 建立 Factory 的子類別並將產生物件的動作寫在這些子類別中
這是原本的Dialog類別

package com.foxx.simplefactory;
import android.app.Dialog;
import android.content.Context;
import android.widget.Button;
import android.widget.TextView;
public class CustomDialog extends Dialog
{
    private String mDescription;
    private TextView mUpTextView;
    private TextView mDownTextView;
    private Button mUpButton;
    private Button mDownButton;
    /**
     * Used in one button
     */
    public CustomDialog(Context context, Button upButton) {
        super(context);
    }
    /**
     * Used in two button
     */
    public CustomDialog(Context context, Button upButton, Button downButton) {
        super(context);
    }
    /**
     * Used in one button and one description
     */
    public CustomDialog(Context context, Button upButton, String description) {
        super(context);
    }
}

 
首先把原本的Factory抽象化

package com.foxx.factorymethod;
import com.foxx.simplefactory.CustomDialog;
public abstract class CustomDialogFactory
{
    protected CustomDialog mCustomDialog;
    public CustomDialog createDialog(){
        return mCustomDialog;
    }
}

 
建立對應第一個建構式的SubFactory

package com.foxx.factorymethod;
import android.content.Context;
import android.widget.Button;
import com.foxx.simplefactory.CustomDialog;
public class OneButtonDialogFactory extends CustomDialogFactory
{
    public OneButtonDialogFactory(Context context, Button upButton) {
        mCustomDialog = new CustomDialog(context, upButton);
    }
}

 
建立對應第二個建構式的SubFactory

package com.foxx.factorymethod;
import android.content.Context;
import android.widget.Button;
import com.foxx.simplefactory.CustomDialog;
public class TwoButtonDialogFactory extends CustomDialogFactory
{
    public TwoButtonDialogFactory(Context context, Button upButton, Button downButton) {
        mCustomDialog = new CustomDialog(context, upButton, downButton);
    }
}

 
建立對應第三個建構式的SubFactory

package com.foxx.factorymethod;
import android.content.Context;
import android.widget.Button;
import android.widget.TextView;
import com.foxx.simplefactory.CustomDialog;
public class OneButtonOneDescriptionDialogFactory extends CustomDialogFactory
{
    public OneButtonOneDescriptionDialogFactory(Context context, Button upButton, String description) {
        mCustomDialog = new CustomDialog(context, upButton, description);
    }
}

 
最後是MainActivity

package com.foxx.factorymethod;
import com.foxx.simplefactory.CustomDialog;
public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        CustomDialogFactory oneButtonFactory = new OneButtonDialogFactory(context, upButton);
        CustomDialog oneDialog = oneButtonFactory.createDialog();
        CustomDialogFactory twoButtonFactory = new TwoButtonDialogFactory(context, upButton, downButton);
        CustomDialog twoDialog = twoButtonFactory.createDialog();
        CustomDialogFactory oneButtonOneDescriptionFactory = new OneButtonOneDescriptionDialogFactory(context, upButton, description);
        CustomDialog oneButtonOneDescriptionDialog = oneButtonOneDescriptionFactory.createDialog();
    }
}

若之後還有第4種,第5種Dialog 可以另外新增SubFactory繼承CustomDialogFactory, 而不需要修改已經寫好的部份
任何的Dialog若有變動需要修改的話, 可到各對應的SubFactory修改即可, 不會改動到其他SubFactory
 

Class Diagram

 
factorymethod