Encapsulating creation
Although only the Simple Factory Method is a true singleton, you’ll find that each specify
factory class in the more general types of factories will only have a single instance.
- Simple Factory method
One approach is to make the factory a static method of the base class:
//: factory:shapefact1:ShapeFactory1.java
// A simple static factory method.
package factory.shapefact1;
import java.util.*;
import junit.framework.*;
abstract class Shape {
public abstract void draw();
public abstract void erase();
public static Shape factory(String type) {
if(type.equals("Circle")) return new Circle();
if(type.equals("Square")) return new Square();
throw new RuntimeException(
"Bad shape creation: " + type);
}
}
class Circle extends Shape {Circle() {} // Package-access constructor
public void draw() {
System.out.println("Circle.draw");
}
public void erase() {
System.out.println("Circle.erase");
}
}
class Square extends Shape {
Square() {} // Package-access constructor
public void draw() {
System.out.println("Square.draw");
}
public void erase() {
System.out.println("Square.erase");
}
}
public class ShapeFactory1 extends TestCase {
String shlist[] = { "Circle", "Square",
"Square", "Circle", "Circle", "Square" };
List shapes = new ArrayList();
public void test() {
Iterator it = Arrays.asList(shlist).iterator();
while(it.hasNext())
shapes.add(Shape.factory((String)it.next()));
it = shapes.iterator();
while(it.hasNext()) {
Shape s = (Shape)it.next();
s.draw();
s.erase();
}
}
public static void main(String args[]) {
junit.textui.TestRunner.run(ShapeFactory1.class);
}
} ///:~
To encourage creation to only happen in the factory( ), the constructors for the specific
types of Shape are give package access, so factory( ) has access to the constructors but they
are not available outside the package.
- Polymorphic factories
different types of factories can be subclassed from the basic factory,for example
interface Shape {
void draw();
void erase();
}
abstract class ShapeFactory {
protected abstract Shape create();
private static Map factories = new HashMap();
public static void
addFactory(String id, ShapeFactory f) {
factories.put(id, f);
}
// A Template Method:
public static final
Shape createShape(String id) {
if(!factories.containsKey(id)) {
try {
// Load dynamically
Class.forName("factory.shapefact2." + id);
} catch(ClassNotFoundException e) {
throw new RuntimeException(
"Bad shape creation: " + id);
}
// See if it was put in:
if(!factories.containsKey(id))
throw new RuntimeException(
"Bad shape creation: " + id);
}
return
((ShapeFactory)factories.get(id)).create();
}
}
class Circle implements Shape {
private Circle() {}
public void draw() {
System.out.println("Circle.draw");
}
public void erase() {
System.out.println("Circle.erase");
}
private static class Factory
extends ShapeFactory {
protected Shape create() {
return new Circle();
}
}
static {
ShapeFactory.addFactory(
"Circle", new Factory());
}
}
.......
- Abstract factories
The Abstract Factory pattern looks like the factory objects we’ve seen previously, with not
one but several factory methods. Each of the factory methods creates a different kind of
object. The idea is that at the point of creation of the factory object, you decide how all the
objects created by that factory will be used.
As another example suppose you are creating a general-purpose gaming environment and you
want to be able to support different types of games
interface Obstacle {
void action();
}
interface Player {
void interactWith(Obstacle o);
}
class Kitty implements Player {
public void interactWith(Obstacle ob) {
System.out.print("Kitty has encountered a ");
ob.action();
}
}
class KungFuGuy implements Player {
public void interactWith(Obstacle ob) {
System.out.print("KungFuGuy now battles a ");
ob.action();
}
}
class Puzzle implements Obstacle {
public void action() {
System.out.println("Puzzle");
}
}
class NastyWeapon implements Obstacle {
public void action() {
System.out.println("NastyWeapon");
}
}
// The Abstract Factory:
interface GameElementFactory {Player makePlayer();
Obstacle makeObstacle();
}
// Concrete factories:
class KittiesAndPuzzles
implements GameElementFactory {
public Player makePlayer() {
return new Kitty();
}
public Obstacle makeObstacle() {
return new Puzzle();
}
}
class KillAndDismember
implements GameElementFactory {
public Player makePlayer() {
return new KungFuGuy();
}
public Obstacle makeObstacle() {
return new NastyWeapon();
}
}
class GameEnvironment {
private GameElementFactory gef;
private Player p;
private Obstacle ob;
public GameEnvironment(
GameElementFactory factory) {
gef = factory;
p = factory.makePlayer();
ob = factory.makeObstacle();
}
public void play() { p.interactWith(ob); }
}
public class Games extends TestCase {
GameElementFactory
kp = new KittiesAndPuzzles(),
kd = new KillAndDismember();
GameEnvironment
g1 = new GameEnvironment(kp),
g2 = new GameEnvironment(kd);
// These just ensure no exceptions are thrown:
public void test1() { g1.play(); }
public void test2() { g2.play(); }
public static void main(String args[]) {
junit.textui.TestRunner.run(Games.class);
}
} ///:~
In this environment, Player objects interact with Obstacle objects, but there are different
types of players and obstacles depending on what kind of game you’re playing. You determine
the kind of game by choosing a particular GameElementFactory, and then the
GameEnvironment controls the setup and play of the game. In this example, the setup and
play is very simple, but those activities (the initial conditions and the state change) can
determine much of the game’s outcome. Here, GameEnvironment is not designed to be
inherited, although it could very possibly make sense to do that.
1.长相不令人讨厌,如果长得不好,就让自己有才气;如果才气也没有,那就总是微笑。
2.气质是关键。如果时尚学不好,宁愿纯朴。
3.与人握手时,可多握一会儿。真诚是宝。
4.不必什么都用“我”做主语。
5.不要向朋友借钱。
6.不要“逼”客人看你的家庭相册。
7.与人打“的”时,请抢先坐在司机旁。
8.坚持在背后说别人好话,别担心这好话传不到当事人耳朵里。
9.有人在你面前说某人坏话时,你只微笑。
10.自己开小车,不要特地停下来和一个骑自行车的同事打招呼。人家会以为你在炫耀。
11.同事生病时,去探望他。很自然地坐在他病床上,回家再认真洗手。
12.不要把过去的事全让人知道。
13.尊重不喜欢你的人。
14.对事不对人;或对事无情,对人要有情;或做人第一,做事其次。
15.自我批评总能让人相信,自我表扬则不然。
16.没有什么东西比围观者们更能提高你的保龄球的成绩了。所以,平常不要吝惜你的喝彩声。
17.不要把别人的好,视为理所当然。要知道感恩。
18.榕树上的“八哥”在讲,只讲不听,结果乱成一团。学会聆听。
19.尊重传达室里的师傅及搞卫生的阿姨。
20.说话的时候记得常用“我们”开头。
21.为每一位上台唱歌的人鼓掌。
22.有时要明知故问:你的钻戒很贵吧!有时,即使想问也不能问,比如:你多大了?
23.话多必失,人多的场合少说话。
24.把未出口的“不”改成:“这需要时间”、“我尽力”、“我不确定”、“当我决定后,会给你打电话”……
25.不要期望所有人都喜欢你,那是不可能的,让大多数人喜欢就是成功的表现。
26.当然,自己要喜欢自己。
ps:27.如果你在表演或者是讲演的时候,如果只要有一个人在听也要用心的继续下去,即使没有人喝采也要演,因为这是你成功的道路,是你成功的摇篮,你不要看的人成功,而是要你成功。
28.如果你看到一个贴子还值得一看的话,那么你一定要回复,因为你的回复会给人继续前进的勇气,会给人很大的激励,同时也会让人感激你!
本人写的分页的标签,和.net中的datagrid标签相似,只需要设置几个属性就可以实现分页功能了,简单方便,并且效率很高。
我写的有两种分页的标签,一种是全部select 出来,然后再分页的,这个系统在第一次访问的时候可能有点慢,但是在随后的访问可是很快的,因为数据已经都缓存以来的,并且是在pageContext中的,所以不会占用很多的资源。第二种是部分select 出来的,效率很高。如果有需要的朋友,请留下Email地址。
想要源代码的朋友请和我联系。
大家可以把用的情况给我说一下,以便我做出调整。
本程序作了升级,请大家下载下边的这个连接进行下载,以便使用最新的版本。
这次升级修复了几个bugs.请下载Splitpage.rar