实验三:抽象工厂模式与建造者模式 一、实验目的
- 掌握抽象工厂模式和建造者模式的概念,能够灵活使用抽象工厂模式和建造者模式解决相关问题。 二、实验内容 场景描述一: 某手机游戏软件公司欲推出一款新的游戏软件,该软件能够支持Symbian和Android等多个主流的手机操作系统平台,针对不同的手机操作系统,该游戏软件提供了不同的游戏操作控制类和游戏界面控制类,并提供相应的工厂类来封装这些类的初始化。软件要求具有较好的扩展性以支持新的操作系统平台,为了满足上述需求,采用抽象工厂模式进行设计。 要求: 1、画出UML图; 2、给出程序代码,程序代码有恰当的注释。 3、文档格式正确,排版清晰。 1、UML类图如下:
2、程序代码如下:
AbstractFactory 接口 :
public interface AbstractFactory {
public Controller produceController();
public Play producePlay();
}
AndroidController 类:
public class AndroidController implements Controller{
@Override
public void controller() {
System.out.println("Android Controller");
}
}
AndroidFactory 类:
public class AndroidFactory extends AndroidController implements AbstractFactory{
public Controller produceController(){
return new AndroidController();
}
public Play producePlay(){
return new AndroidPlay();
}
}
AndroidPlay类:
public class AndroidPlay implements Play{
@Override
public void play() {
System.out.println("Android Play");
}
}
Controller接口:
public interface Controller {
public void controller();
}
Play接口:
public interface Play {
public void play();
}
SymbianController 类:
public class SymbianController implements Controller{
@Override
public void controller() {
System.out.println("Symbian Controller");
}
}
SymbianFactory类:
public class SymbianFactory implements AbstractFactory{
public Controller produceController(){
return new SymbianController();
}
public Play producePlay(){
return new SymbianPlay();
}
}
SymbianPlay 类:
public class SymbianPlay implements Play{
@Override
public void play() {
System.out.println("Symbian Play");
}
}
XMLUtil类:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("homework-03/AbstractFactoryconfig.xml"));
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
AbstractFactoryconfig.xml配置文件
<?xml version="1.0"?>
<config>
<className>one.AndroidFactory</className>
</config>
Client类:
public class Client {
public static void main(String[] args) {
AbstractFactory factory=(AbstractFactory)XMLUtil.getBean();
Controller controller=factory.produceController();
controller.controller();
Play play= factory.producePlay();
play.play();
}
}
运行效果图: 3、回答问题:如果需要在上述设计方案中增加对Windows Mobile操作系统的支持,需对该设计方案进行哪些调整,简单说明实现过程。 场景描述二: 某软件公司欲开发一个音频和视频播放软件,为了给用户使用提供方便,该播放软件提供了多种界面显示模式,如完整模式、精简模式等。在不同的显示模式下主界面的组成元素有所差异,如在完整模式下将显示菜单、播放列表、主窗口、控制条等,在精简模式下只显示主窗口和控制条,现使用建造者模式设计该软件(要求用到钩子方法)。 1、画出UML图; 2、给出程序代码,程序代码有恰当的注释。 3、文档格式正确,排版清晰。 1、UML类图如下: 2、程序代码如下:
Model类:
public class Model {
private String meal;
private String list;
private String window;
private String controller;
public String getMeal() {
return meal;
}
public void setMeal(String meal) {
this.meal = meal;
}
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
public String getWindow() {
return window;
}
public void setWindow(String window) {
this.window = window;
}
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
}
ModelBuilder 抽象类:
public abstract class ModelBuilder {
protected Model model=new Model();
public abstract void buildMeal();
public abstract void buildList();
public abstract void buildWindow();
public abstract void buildController();
public Model getModel()
{
return model;
}
}
Player 类:
public class Player {
private ModelBuilder mb;
public void setModelBuilder(ModelBuilder mb){
this.mb=mb;
}
public Model construct() {
mb.buildMeal();
mb.buildList();
mb.buildWindow();
mb.buildController();
return mb.getModel();
}
}
SubModelBuilderBig类:
public class SubModelBuilderBig extends ModelBuilder{
@Override
public void buildMeal() {
model.setMeal("play meal");
}
@Override
public void buildList() {
model.setList("play list");
}
@Override
public void buildWindow() {
model.setWindow("play window");
}
@Override
public void buildController() {
model.setController("play controller");
}
}
SubModelBuilderSmall类:
public class SubModelBuilderSmall extends ModelBuilder{
@Override
public void buildMeal() {
model.setMeal("none");
}
@Override
public void buildList() {
model.setList("none");
}
@Override
public void buildWindow() {
model.setWindow("play window");
}
@Override
public void buildController() {
model.setController("play controller");
}
}
XMLUtil类:
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("homework-03/Builderconfig.xml"));
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
Builderconfig.xml
<?xml version="1.0"?>
<config>
<className>two.SubModelBuilderSmall</className>
</config>
Client 类:
public class Client {
public static void main(String[] args) {
ModelBuilder mb=(ModelBuilder)XMLUtil.getBean();
Player player=new Player();
player.setModelBuilder(mb);
Model model=player.construct();
System.out.println(model.getMeal());
System.out.println(model.getList());
System.out.println(model.getWindow());
System.out.println(model.getController());
}
}
运行效果图:
标签:return,String,软件设计,void,模式,class,model,public,体系结构 From: https://blog.51cto.com/histry/9098454