工厂模式
作者:互联网
UML图如下:
BDconvert、TxtConvert、XmlConvert分别为转换器接口的是实现类,其中方法为返回对应的执行接口的执行类。
具体代码如下
package Demo.Interface; public interface Convertor { public Excute getExcute(); } package Demo.Interface; public interface Excute { public void Excute(); } package Demo.ConvertImp; import Demo.ExcuteImp.DBExcute; import Demo.Interface.Convertor; import Demo.Interface.Excute; public class DBConvert implements Convertor { public Excute getExcute() { // TODO Auto-generated method stub return new DBExcute(); } } package Demo.ConvertImp; import Demo.ExcuteImp.TxtExcute; import Demo.Interface.Convertor; import Demo.Interface.Excute; public class TxtConvert implements Convertor { public Excute getExcute() { // TODO Auto-generated method stub return new TxtExcute(); } } package Demo.ConvertImp; import Demo.ExcuteImp.XmlExcute; import Demo.Interface.Convertor; import Demo.Interface.Excute; public class XmlConvert implements Convertor { public Excute getExcute() { // TODO Auto-generated method stub return new XmlExcute(); } } package Demo.ExcuteImp; import Demo.Interface.Excute; public class DBExcute implements Excute{ public void Excute() { // TODO Auto-generated method stub System.out.println("BD Excuting"); } } package Demo.ExcuteImp; import Demo.Interface.Excute; public class TxtExcute implements Excute { public void Excute() { // TODO Auto-generated method stub System.out.println("Txt Excuting"); } } package Demo.ExcuteImp; import Demo.Interface.Excute; public class XmlExcute implements Excute { public void Excute() { // TODO Auto-generated method stub System.out.println("Xml Excuting"); } } package Demo.Test; import Demo.ConvertImp.DBConvert; import Demo.ConvertImp.TxtConvert; import Demo.ConvertImp.XmlConvert; import Demo.ExcuteImp.DBExcute; import Demo.ExcuteImp.TxtExcute; import Demo.ExcuteImp.XmlExcute; public class TestALl { public static void main(String[] args) { DBConvert convert1 =new DBConvert(); DBExcute dbExcute=(DBExcute) convert1.getExcute(); dbExcute.Excute(); TxtConvert convert2 =new TxtConvert(); TxtExcute txtExcute=(TxtExcute) convert2.getExcute(); txtExcute.Excute(); XmlConvert convert3 = new XmlConvert(); XmlExcute xmlExcute=(XmlExcute)convert3.getExcute(); xmlExcute.Excute(); } }
标签:Excute,package,Demo,模式,工厂,Interface,import,public 来源: https://www.cnblogs.com/hongsener/p/12543096.html