public class helloTest {
public static void main(String[] args) {
DebugInterfaceFactory factory = new DebugInterfaceFactory();
Wwan w = new Wwan(factory.getPrintDebug());
w.wwanDebugMsg("this is a wwan msg");
return;
}
}
class Wwan {
/* 定义一个操作集成员 */
DebugInterface debug;
/* 初始化时 初始化操作集 */
Wwan(DebugInterface d){
this.debug = d;
}
/* 使用 */
void wwanDebugMsg(String str) {
this.debug.debugout(str);
}
}
/* 接口 定义操作集 */
interface DebugInterface {
void debugout(String str);
}
/* 操作集的一种实现 */
class PrintDebug implements DebugInterface {
@Override
public void debugout(String str) {
// TODO Auto-generated method stub
System.out.println("hello " + str);
return;
}
}
/* 操作集的另一种实现 */
class InfoDebug implements DebugInterface {
@Override
public void debugout(String str) {
// TODO Auto-generated method stub
System.out.println("info" + str);
return;
}
}
/* 操作集的工厂 不同方法返回不同操作集的具体对象 多态 */
class DebugInterfaceFactory {
DebugInterface getPrintDebug() {
return new PrintDebug();
}
DebugInterface getInfoDebug() {
return new InfoDebug();
}
}
工厂模式
最新推荐文章于 2024-04-13 16:38:06 发布