1. 媒体播放器的实现
(1)案例背景:
Windows Media Player 和 RealPlayer 是两种常用的媒体播放器,它们的 API 结构和调用方法存在差别,现在你的应用程序需要支持这两种播放器 API,并且将来可能还需要支持新的媒体播放器,根据给出的类图,回答该应用程序使用了哪些设计模式,并编程实现这个应用程序。
(2)实现步骤:
Ø 根据类图,回答这个应用程序使用了哪几种设计模式
工厂方法模式,适配器模式
- 根据类图,实现这个应用程序的具体代码以及工具类 XMLUtil,使应用程序能够使用两种播放器的 API。
- 编译并运行代码,实现应用程序对两种播放器的调用。
- 设计模式的选用需要根据具体要求来进行
- 多种设计模式可以进行混合使用
(3)案例总结:
代码结构:
详细代码:
PlayerFactory.java:
package playerAPI.Factory;import playerAPI.API.PlayerList;import playerAPI.Window.MainWindow;public interface PlayerFactory {public MainWindow createMainWindow();public PlayerList createPlayerList();}
RealPlayerFactory.java:
package playerAPI.Factory;import playerAPI.API.PlayerList;import playerAPI.API.RealPlayerList;import playerAPI.Window.MainWindow;import playerAPI.Window.RealPlayerWindow;public class RealPlayerFactory implements PlayerFactory {@Overridepublic MainWindow createMainWindow(){return new RealPlayerWindow();}@Overridepublic PlayerList createPlayerList(){return new RealPlayerList();}}
MediaPlayerFactory.java:
package playerAPI.Factory;import playerAPI.API.MediaPlayerList;import playerAPI.API.PlayerList;import playerAPI.Window.MainWindow;import playerAPI.Window.MediaPlayerWindow;import javax.annotation.Resource;public class MediaPlayerFactory implements PlayerFactory{@Overridepublic MainWindow createMainWindow(){return new MediaPlayerWindow();}@Overridepublic PlayerList createPlayerList(){return new MediaPlayerList();}}
MainWindow.java:
package playerAPI.Window;public interface MainWindow {public void window();}
MediaPlayerWindow.java:
package playerAPI.Window;import playerAPI.API.MediaPlayerAPI;public class MediaPlayerWindow implements MainWindow{public MediaPlayerAPI mediaplayerAPI;public MediaPlayerWindow() {mediaplayerAPI=new MediaPlayerAPI();}@Overridepublic void window() {System.out.println("MediaPlayerWindow");}}
RealPlayerWindow.java:
package playerAPI.Window;import playerAPI.API.RealPlayerAPI;public class RealPlayerWindow implements MainWindow{private RealPlayerAPI realplayerAPI=new RealPlayerAPI();@Overridepublic void window() {System.out.println("RealPlayerWindow");}}
PlayerList.java:
package playerAPI.API;public interface PlayerList {public void player();}
MediaPlayerAPI.java:
package playerAPI.API;public class MediaPlayerAPI {public void window() {System.out.println("MediaPlayer window");}public void player() {System.out.println("MediaPlayer player");}}
RealPlayerAPI.java:
package playerAPI.API;public class RealPlayerAPI {public void window() {System.out.println("RealPlayer window");}public void player() {System.out.println("RealPlayer player");}}
MediaPlayerList.java:
package playerAPI.API;public class MediaPlayerList implements PlayerList{public MediaPlayerAPI mediaplayerAPI;public MediaPlayerList() {mediaplayerAPI=new MediaPlayerAPI();}@Overridepublic void player() {System.out.println("MediaPlayerList");}}
RealPlayerList.java:
package playerAPI.API;public class RealPlayerList implements PlayerList {private RealPlayerAPI realplayerAPI=new RealPlayerAPI();public RealPlayerList(){realplayerAPI = new RealPlayerAPI();}@Overridepublic void player() {System.out.println("RealPlayerList");}}
XMLUtil.java:
package playerAPI.Utils;import org.w3c.dom.*;import javax.xml.parsers.*;import org.xml.sax.SAXException;import java.io.*;public class XMLUtil {public static Object getBean(){try {//创建文档对象DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = dFactory.newDocumentBuilder();Document doc;doc = builder.parse(new File("src/playerAPI/Utils/config.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;}}}
config.xml:
<?xml version="1.0" ?><config><className>playerAPI.Factory.RealPlayerFactory</className></config>
ClientClass.java:
package playerAPI.Client;import playerAPI.API.PlayerList;import playerAPI.Factory.PlayerFactory;import playerAPI.Utils.XMLUtil;import playerAPI.Window.MainWindow;public class ClientClass {public static void main(String[] args) {System.out.println("软件1904班 白旭君 2019005368");PlayerFactory pfactory;MainWindow window;PlayerList list;pfactory = (PlayerFactory)XMLUtil.getBean();window = pfactory.createMainWindow();window.window();list = pfactory.createPlayerList();list.player();}}
实验结果: