提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.
基本的模型如下
具体的实现如下:
做一个产品由三类人完成,但是三类人又有高级和初级的分别,将高级别的组织在一起,低级别的组织在一起.
代码实现如下:
三类人:抽象概念:
package org.durian.durianabstractdesignmode.atom; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:33 * Email [email protected] * Action : durian */ public interface IDesigner { void design(); }
package org.durian.durianabstractdesignmode.atom; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:33 * Email [email protected] * Action : durian */ public interface IDevloper { void develop(); }
package org.durian.durianabstractdesignmode.atom; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:33 * Email [email protected] * Action : durian */ public interface IProductor { void product(); }
三类人分别有高级低级区分,这个是具体的:
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IDesigner; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:44 * Email [email protected] * Action : durian */ public class AdvanceDesigner implements IDesigner { @Override public void design() { System.out.println("I do advance design work only !"); } }
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IDevloper; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:44 * Email [email protected] * Action : durian */ public class AdvanceDeveloper implements IDevloper { @Override public void develop() { System.out.println("I do advance develop work only !"); } }
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IProductor; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:45 * Email [email protected] * Action : durian */ public class AdvanceProductor implements IProductor { @Override public void product() { System.out.println("I do advance product work only !"); } }
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IDesigner; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:45 * Email [email protected] * Action : durian */ public class BasicDesigner implements IDesigner { @Override public void design() { System.out.println("I do basic design work , as I'm a learner !"); } }
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IDevloper; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:46 * Email [email protected] * Action : durian */ public class BasicDeveloper implements IDevloper { @Override public void develop() { System.out.println("I do basic develop work , as I'm a learner !"); } }
package org.durian.durianabstractdesignmode.worker; import org.durian.durianabstractdesignmode.atom.IProductor; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:46 * Email [email protected] * Action : durian */ public class BasicProductor implements IProductor { @Override public void product() { System.out.println("I do basic product work , as I'm a learner !"); } }
然后组织成两个不同级别的大团体:
package org.durian.durianabstractdesignmode.factory; import org.durian.durianabstractdesignmode.atom.IDesigner; import org.durian.durianabstractdesignmode.atom.IDevloper; import org.durian.durianabstractdesignmode.atom.IProductor; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:31 * Email [email protected] * Action : durian */ public interface IWorkerFactory { IProductor createProductor(); IDesigner createDesigner(); IDevloper createDeveloper(); }
提供给项目使用的具体实现如下:
package org.durian.durianabstractdesignmode.factory; import org.durian.durianabstractdesignmode.atom.IDesigner; import org.durian.durianabstractdesignmode.atom.IDevloper; import org.durian.durianabstractdesignmode.atom.IProductor; import org.durian.durianabstractdesignmode.worker.AdvanceDesigner; import org.durian.durianabstractdesignmode.worker.AdvanceDeveloper; import org.durian.durianabstractdesignmode.worker.AdvanceProductor; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:47 * Email [email protected] * Action : durian */ public class AdvanceWorkerFactory implements IWorkerFactory { @Override public IProductor createProductor() { return new AdvanceProductor(); } @Override public IDesigner createDesigner() { return new AdvanceDesigner(); } @Override public IDevloper createDeveloper() { return new AdvanceDeveloper(); } }
package org.durian.durianabstractdesignmode.factory; import org.durian.durianabstractdesignmode.atom.IDesigner; import org.durian.durianabstractdesignmode.atom.IDevloper; import org.durian.durianabstractdesignmode.atom.IProductor; import org.durian.durianabstractdesignmode.worker.BasicDesigner; import org.durian.durianabstractdesignmode.worker.BasicDeveloper; import org.durian.durianabstractdesignmode.worker.BasicProductor; /** * Project name : DurianAbstractDesignMode * Created by zhibao.liu on 2016/2/2. * Time : 18:48 * Email [email protected] * Action : durian */ public class BasicWorkerFactory implements IWorkerFactory { @Override public IProductor createProductor() { return new BasicProductor(); } @Override public IDesigner createDesigner() { return new BasicDesigner(); } @Override public IDevloper createDeveloper() { return new BasicDeveloper(); } }
使用:
package org.durian.durianabstractdesignmode; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import org.durian.durianabstractdesignmode.atom.IDesigner; import org.durian.durianabstractdesignmode.atom.IDevloper; import org.durian.durianabstractdesignmode.atom.IProductor; import org.durian.durianabstractdesignmode.factory.AdvanceWorkerFactory; import org.durian.durianabstractdesignmode.factory.BasicWorkerFactory; public class MainActivity extends ActionBarActivity { private AdvanceWorkerFactory advance; private BasicWorkerFactory basic; private Button advanceButton; private Button basicButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); advance=new AdvanceWorkerFactory(); basic=new BasicWorkerFactory(); basicButton=(Button)findViewById(R.id.basic); basicButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IDesigner iDesigner=basic.createDesigner(); iDesigner.design(); IDevloper iDevloper=basic.createDeveloper(); iDevloper.develop(); IProductor iProductor=basic.createProductor(); iProductor.product(); } }); advanceButton=(Button)findViewById(R.id.advance); advanceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IDesigner iDesigner=advance.createDesigner(); iDesigner.design(); IDevloper iDevloper=advance.createDeveloper(); iDevloper.develop(); IProductor iProductor=advance.createProductor(); iProductor.product(); } }); } }
运行结果: