首页 > 其他 > 详细

工厂模式

时间:2020-03-21 23:12:12      阅读:66      评论:0      收藏:0      [点我收藏+]

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();
	}

}

  

技术分享图片

 

工厂模式

原文:https://www.cnblogs.com/hongsener/p/12543096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!