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.氣質(zhì)是關(guān)鍵。如果時尚學不好,寧愿純樸。
?
? 3.與人握手時,可多握一會兒。真誠是寶。
?
? 4.不必什么都用“我”做主語。
?
? 5.不要向朋友借錢。
?
? 6.不要“逼”客人看你的家庭相冊。
?
? 7.與人打“的”時,請搶先坐在司機旁。
?
? 8.堅持在背后說別人好話,別擔心這好話傳不到當事人耳朵里。
?
? 9.有人在你面前說某人壞話時,你只微笑。
?
? 10.自己開小車,不要特地停下來和一個騎自行車的同事打招呼。人家會以為你在炫耀。
?
? 11.同事生病時,去探望他。很自然地坐在他病床上,回家再認真洗手。
?
? 12.不要把過去的事全讓人知道。
?
? 13.尊重不喜歡你的人。
?
? 14.對事不對人;或?qū)κ聼o情,對人要有情;或做人第一,做事其次。
?
? 15.自我批評總能讓人相信,自我表揚則不然。
?
? 16.沒有什么東西比圍觀者們更能提高你的保齡球的成績了。所以,平常不要吝惜你的喝彩聲。
?
? 17.不要把別人的好,視為理所當然。要知道感恩。
?
? 18.榕樹上的“八哥”在講,只講不聽,結(jié)果亂成一團。學會聆聽。
?
? 19.尊重傳達室里的師傅及搞衛(wèi)生的阿姨。
?
? 20.說話的時候記得常用“我們”開頭。
?
? 21.為每一位上臺唱歌的人鼓掌。
?
? 22.有時要明知故問:你的鉆戒很貴吧!有時,即使想問也不能問,比如:你多大了?
?
? 23.話多必失,人多的場合少說話。
?
? 24.把未出口的“不”改成:“這需要時間”、“我盡力”、“我不確定”、“當我決定后,會給你打電話”……
?
? 25.不要期望所有人都喜歡你,那是不可能的,讓大多數(shù)人喜歡就是成功的表現(xiàn)。
?
? 26.當然,自己要喜歡自己。?
?
? ps:27.如果你在表演或者是講演的時候,如果只要有一個人在聽也要用心的繼續(xù)下去,即使沒有人喝采也要演,因為這是你成功的道路,是你成功的搖籃,你不要看的人成功,而是要你成功。
?
? 28.如果你看到一個貼子還值得一看的話,那么你一定要回復,因為你的回復會給人繼續(xù)前進的勇氣,會給人很大的激勵,同時也會讓人感激你!

本人寫的分頁的標簽,和.net中的datagrid標簽相似,只需要設(shè)置幾個屬性就可以實現(xiàn)分頁功能了,簡單方便,并且效率很高。
我寫的有兩種分頁的標簽,一種是全部select 出來,然后再分頁的,這個系統(tǒng)在第一次訪問的時候可能有點慢,但是在隨后的訪問可是很快的,因為數(shù)據(jù)已經(jīng)都緩存以來的,并且是在pageContext中的,所以不會占用很多的資源。第二種是部分select 出來的,效率很高。如果有需要的朋友,請留下Email地址。
想要源代碼的朋友請和我聯(lián)系。
大家可以把用的情況給我說一下,以便我做出調(diào)整。
本程序作了升級,請大家下載下邊的這個連接進行下載,以便使用最新的版本。
這次升級修復了幾個bugs.請下載Splitpage.rar