十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这个应该比较简单一点。
创新互联建站是专业的琼中黎族网站建设公司,琼中黎族接单;提供网站设计制作、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行琼中黎族网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
某系统日志记录器要求支持多种日志记录方式,如文件记录、数据库记录等,且用户可以根据要求动态选择日志记录方式。现使用工厂方法模式设计该系统,并写出相应Java代码。
interface Log{
public void writeLog();
}
class FileLog implements Log{
public void writeLog(){
System.out.println("文件记录");
}
}
class DatabaseLog implements Log{
public void writeLog(){
System.out.println("数据库记录");
}
}
interface LogFactory{
public Log createLog();
}
class FileLogFactory implements LogFactory{
public Log createLog(){
return new FileLog();
}
}
class DatabaseLogFactory implements LogFactory{
public Log createLog(){
return new DatabaseLog();
}
}
public class Client{
public static void main(String[] args) {
try{
Log log;
LogFactory factory;
//这里可以改成使用DOM和Java反射机制读取XML文件,获取工厂类名
factory=new DatabaseLogFactory ();
log=factory.createLog();
log.writeLog();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
java中工厂模式分为:
简单工厂模式(Simple Factory)
2. 工厂方法模式(Factory Method)
3. 抽象工厂模式(Abstract Factory)
每种方法的实现不同
提供一个简单工厂模式的案例:
public abstract class Woman {
private String mySkill;
public String getMySkill() {
return mySkill;
}
public Woman() {
//System.out.println("我是女人");
}
public void setMySkill(String mySkill) {
this.mySkill = mySkill;
}
}
----------------------------
public class LovelinessWoman extends Woman{
/*
* 可爱型女人
*/
public LovelinessWoman()
{
String mySkill="撒过娇、出过轨、勾引领导下过水";
this.setMySkill(mySkill);
}
}
-----------------------------
public class SteelinessWoman extends Woman{
/*
* 冷酷型女人
*/
public SteelinessWoman()
{
String mySkill="装过神、弄过鬼,跟别人老公亲过嘴";
this.setMySkill(mySkill);
}
}
--------------------------------------
public class WomanMakeFactory {
public Woman findWoman(int typeID) {
switch (typeID) {
case 1:
return new LovelinessWoman();
case 2:
return new VirtuousWoman();
case 3:
return new SteelinessWoman();
default:
return null;
}
}
public Woman findWoman(String type) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
/*
* Type t = Type.GetType("SimpleFactory." + type);
* Woman wm =
* (Woman)Activator.CreateInstance(t); return wm;
*/
String string="cn.jbit.design.simplefactory."+type;
Class c = Class.forName(string);
Woman wm = (Woman) c.newInstance();
return wm;
}
}
-------------------------
调用
public class Test2 {
/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Scanner input=new Scanner(System.in);
boolean sel=false;
do {
System.out.println("请选择你要找的女人的类型");
System.out.println("LovelinessWoman:可爱型女人 VirtuousWoman:善良型女人 SteelinessWoman:冷酷型女人");
String typeid=input.next();
WomanMakeFactory factory = new WomanMakeFactory();
Woman wm=factory.findWoman(typeid);
System.out.println("该类型女人的新标准:");
System.out.println(wm.getMySkill());
System.out.println("还想看看别的类型吗?y代表想,n代表再也不想了");
sel=input.next().equals("y");
} while (sel);
}
}
interface Fruit { // 定义一个水果接口
public void eat(); // 吃水果
}
class Apple implements Fruit {
public void eat() {
System.out.println("** 吃苹果。");
}
};
class Orange implements Fruit {
public void eat() {
System.out.println("** 吃橘子。");
}
};
class Factory { // 定义工厂类
private static Factory factory;
private Factory(){}
public static Factory getInstance() {
if(factory==null){
factory=new Factory();
}
return factory;
}
public Apple newApplet(){
return new Apple();
}
public Orange newOrange(){
return new Orange();
}
};
class FactoryModel {
public static void main(String args[]) {
Fruit f = Factory.getInstance().newApplet();
if (f != null) { // 判断是否取得实例
f.eat();
}
f = Factory.getInstance().newOrange();
if (f != null) { // 判断是否取得实例
f.eat();
}
}
};