Scratz さんのプロフィールScratz 的小果园フォトブログリストその他 ![]() | ヘルプ |
|
2008/07/22 《三国志 X》快速修炼手册能力系列 内政系列 战斗系列 计略系列 单挑系列 舌战系列 称号系列 成都公交卡作弊用法二则 自从成都公交开始实行两小时内无限免费换乘以来,我们这些刷卡一族确实得到了极大的实惠。不过经过我最近的一些小小“实践”,找到了两条作弊用法。 第一条和空调车有关。刷卡乘车,普通车扣一次,而空调车要扣两次,但是如果先上普通车再转乘空调车的话,不会再扣。于是如果在首次乘车时,可以先上普通车随便坐一两站,甚至刷卡不上车,再换乘空调车,就能省一次了。当然可能会耽搁一点时间。此法也可用于对付晚上十点后多扣一次,只要在十点前在任意普通车上刷掉一次就行了。 如果说第一条有点谋私利的话,第二条就算得上“大公无私”了。你就在一个车站“蹲点”,每来一辆车就随便帮一个人刷卡,然后继续。两小时内只扣一次,却能帮很多人免费上车,因为系统只会认为你在不断转车。下班的时候不妨帮帮同事,赢得好名声呵呵。当然此法也有局限性,就是每辆车只能送走一个人。 2008/07/03 How to write a plug-in with Java? As you have guessed, plug-ins in Java are nothing more than some jar files and configurations (usually XML files). The main application reads the configurations and use reflection to load classes from plugins at runtime. In this article, we're going to write a simple movie manager to show how plug-ins work in Java. The movie manager manages wants to manage as many movies as it can, so we can't hard code movies into the manager itself. In this case, movies are very suitable be implemented as plug-ins. The manager reads some configurations and load these plug-ins at runtime. To connect the manager and the plug-ins, a movie interface is also needed. After loading plug-ins, the manager just manages a list of movie interfaces, and this is a good example of the design princple: "Program to an interface, not an implementation." The picture below shows the structure of the movie manager.
So it's time for coding. Let's first look at the movie interface. For simplicity, we write only one method to get the movie's name, and you can add your own as you wish. package com.zhyi.plugintest.lib; They are nothing special than some very simple implementations of the Movie interface. The following is a example for "The Terminator". By the way, our former super hero Arnold is more than 60 years old now, ohhhhh... package com.zhyi.plugintest.plugina; See the full source code for a full view of the plug-ins. I have written two plug-ins, and maybe you'd like to add more. But for now, let's turn to the confugurations. It's a simple XML file that is to be read by the manager. <?xml version="1.0" encoding="UTF-8"?><plugins> I won't bore you with useless words because the XML file explains itself very well. The last and the most important part is the manager. We have two classes there, one for the main class (MovieManagerTest) and one for the real management tasks - loading and playing movies (MovieManager). package com.zhyi.plugintest; As for MovieManager, it holds a list of movies that are read from plug-ins. private List<Movie> movies; The application needs only one movie manager, so we use the singlton pattern. This can also prevent plug-ins be loaded more than once. private static MovieManager movieManager = new MovieManager(); We load all movies when MovieManager is being constructed. The loadMovies() method is alittle verbose, but most part of the code is for parsing the XML configuration file. If you are comfortable with DOM, it's very straight forward. private void loadMovies() throws Exception { The loadPlugin() method does the actual job of reflection, and also very simple. private void loadPlugin(URL[] jars, List<String> classes) throws Exception { The real last and also the simplest method is to play all movies. You can write your own code to play a specific movie if you like. public void playAll() { I leave the task of running the movie manager for you. You need to be careful with the directory structure. Here's the out put on my machine: Playing: Indiana Jones Couldn't be simpler, right? Again, be sure to check out the the full source code. 我回来了!近段时间真是波折多多。不过总算还好。
刚刚把 SkyDrive 开通了,以后就不会继续在 BlogJava 的博客上写了。 |
|
|