//创建实现接口的实体类 public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } } public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class ShapeFactory { //使用 getShape 方法获取形状类型的对象 //若获取实例方法为Static(静态的)则是简单工厂模式 public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); }else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }
//创建接口 public interface Button {} public interface Border {}
//Mac系实体类 public class MacButton implements Button {} public class MacBorder implements Border {} //Win系实体类 public class WinButton implements Button {} public class WinBorder implements Border {}
//创建工厂 public class MacFactory { public static Button createButton() { return new MacButton(); } public static Border createBorder() { return new MacBorder(); } } public class WinFactory { public static Button createButton() { return new WinButton(); } public static Border createBorder() { return new WinBorder(); } }
public static void main(String[] args) { (new TableBuilderDemo()).demo(args); }
/** * Client code perspective. */ public void demo(String[] args) { // Name of the GUI table class can be passed to the app parameters. String class_name = args.length > 0 ? args[0] : "JTable_Builder";
interface Builder { void set_width_and_height(int width, int height);
void start_row();
void build_cell(String value);
Component get_result(); }
public static class JTable_Builder implements Builder { private JTable m_table; private TableModel m_model; private int x = 0, y = 0;
public void set_width_and_height(int width, int height) { m_table = new JTable(height, width); m_model = m_table.getModel(); }
public void start_row() { x = 0; ++y; }
public void build_cell(String value) { m_model.setValueAt(value, y, x++); }
public Component get_result() { return m_table; } }
public static class GridLayout_Builder implements Builder { private JPanel m_panel = new JPanel();
public void set_width_and_height(int width, int height) { m_panel.setLayout(new GridLayout(height, width)); m_panel.setBackground(Color.white); }
public void start_row() { }
public void build_cell(String value) { m_panel.add(new Label(value)); }
public Component get_result() { return m_panel; } }
public static class GridBagLayout_Builder implements Builder { private JPanel m_panel = new JPanel(); private GridBagConstraints c = new GridBagConstraints(); private int x = 0, y = 0;
public void set_width_and_height(int width, int height) { m_panel.setLayout(new GridBagLayout()); m_panel.setBackground(Color.white); }
/** Prototype Class **/ public class Cookie implements Cloneable { public Object clone() throws CloneNotSupportedException { //In an actual implementation of this pattern you would now attach references to //the expensive to produce parts from the copies that are held inside the prototype. return (Cookie) super.clone(); } }
1 2
/** Concrete Prototypes to clone **/ public class CoconutCookie extends Cookie { }
public final class FontData { /** * A weak hash map will drop unused references to FontData. * Values have to be wrapped in WeakReferences, * because value objects in weak hash map are held by strong references. * 一个WeakHashMap将自动GC未被引用的FontData * 值必须被包裹在WeakReference中 * 因为值在WeakHashMap中依旧还是强引用状态 */ private static final WeakHashMap<FontData, WeakReference<FontData>> FLY_WEIGHT_DATA = new WeakHashMap<FontData, WeakReference<FontData>>(); private final int pointSize; private final String fontFace; private final Color color; private final Set<FontEffect> effects;
public static FontData create(int pointSize, String fontFace, Color color, FontEffect... effects) { EnumSet<FontEffect> effectsSet = EnumSet.noneOf(FontEffect.class); for (FontEffect fontEffect : effects) { effectsSet.add(fontEffect); } // We are unconcerned with object creation cost, we are reducing overall memory consumption // 我们不关心对象创建成本,我们正在减少总体内存消耗 FontData data = new FontData(pointSize, fontFace, color, effectsSet);
// Retrieve previously created instance with the given values if it (still) exists //如果(仍然)存在,则使用给定值检索先前创建的实例 WeakReference<FontData> ref = FLY_WEIGHT_DATA.get(data); FontData result = (ref != null) ? ref.get() : null; // Store new font data instance if no matching instance exists // 如果不存在匹配的实例,则存储新的FontData实例 if (result == null) { FLY_WEIGHT_DATA.put(data, new WeakReference<FontData> (data)); result = data; } // return the single immutable copy with the given values // 返回具有给定值的单个不可变副本 return result; }
@Override public boolean equals(Object obj) { if (obj instanceof FontData) { if (obj == this) { return true; } FontData other = (FontData) obj; return other.pointSize == pointSize && other.fontFace.equals(fontFace) && other.color.equals(color) && other.effects.equals(effects); } return false; }
@Override public int hashCode() { return (pointSize * 37 + effects.hashCode() * 13) * fontFace.hashCode(); }
// Getters for the font data, but no setters. FontData is immutable. // 获取FontData的getters,但没有setter。 FontData是不可变的。 }
//on System A class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); }
public void displayImage() { System.out.println("Displaying " + filename); } }
//on System B class ProxyImage implements Image { private String filename; private Image image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if(image == null) image = new RealImage(filename); image.displayImage(); } }
1 2 3 4 5 6 7 8 9
class ProxyExample { public static void main(String[] args) { Image image1 = new ProxyImage("HiRes_10MB_Photo1"); Image image2 = new ProxyImage("HiRes_10MB_Photo2");
// The Window interface class public interface Window { // Draws the Window public void draw(); // Returns a description of the Window public String getDescription(); }
// implementation of a simple Window without any scrollbars public class SimpleWindow implements Window { public void draw() { // Draw window }
public String getDescription() { return "simple window"; } }
// abstract decorator class - note that it implements Window public abstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated
public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } @Override public void draw() { decoratedWindow.draw(); }
@Override public String getDescription() { return decoratedWindow.getDescription(); } }
// The first concrete decorator which adds vertical scrollbar functionality public class VerticalScrollBar extends WindowDecorator { public VerticalScrollBar(Window windowToBeDecorated) { super(windowToBeDecorated); }
@Override public void draw() { super.draw(); drawVerticalScrollBar(); }
private void drawVerticalScrollBar() { // Draw the vertical scrollbar }
@Override public String getDescription() { return super.getDescription() + ", including vertical scrollbars"; } }
// The second concrete decorator which adds horizontal scrollbar functionality public class HorizontalScrollBar extends WindowDecorator { public HorizontalScrollBar (Window windowToBeDecorated) { super(windowToBeDecorated); }
@Override public void draw() { super.draw(); drawHorizontalScrollBar(); }
private void drawHorizontalScrollBar() { // Draw the horizontal scrollbar }
@Override public String getDescription() { return super.getDescription() + ", including horizontal scrollbars"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Main { // for print descriptions of the window subclasses static void printInfo(Window w) { System.out.println("description:"+w.getDescription()); } public static void main(String[] args) { // original SimpleWindow SimpleWindow sw = new SimpleWindow(); printInfo(sw); // HorizontalScrollBar mixed Window HorizontalScrollBar hbw = new HorizontalScrollBar(sw); printInfo(hbw); // VerticalScrollBar mixed Window VerticalScrollBar vbw = new VerticalScrollBar(hbw); printInfo(vbw); } }
/** "Refined Abstraction" */ class CircleShape implements Shape { private double x, y, radius; private DrawingAPI drawingAPI; public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) { this.x = x; this.y = y; this.radius = radius; this.drawingAPI = drawingAPI; }
// low-level i.e. Implementation specific public void draw() { drawingAPI.drawCircle(x, y, radius); } // high-level i.e. Abstraction specific public void resizeByPercentage(double pct) { radius *= pct; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** "Client" */ class BridgePattern { public static void main(String[] args) { Shape[] shapes = new Shape[2]; shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1()); shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());
//定义目标调用接口 interface Shape { void draw(int x, int y, int z, int j); }
1 2 3 4 5 6 7 8 9 10 11 12
class Line { public void draw(int x1, int y1, int x2, int y2) { System.out.println("Line from point A(" + x1 + ";" + y1 + "), to point B(" + x2 + ";" + y2 + ")"); } }
class Rectangle { public void draw(int x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point (" + x + ";" + y + "), width: " + width + ", height: " + height); } }
//实现Adapter class LineAdapter implements Shape { private Line adaptee;
public LineAdapter(Line line) { this.adaptee = line; }
@Override public void draw(int x1, int y1, int x2, int y2) { adaptee.draw(x1, y1, x2, y2); } }
class RectangleAdapter implements Shape { private Rectangle adaptee;
public RectangleAdapter(Rectangle rectangle) { this.adaptee = rectangle; }
@Override public void draw(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x2 - x1); int height = Math.abs(y2 - y1); adaptee.draw(x, y, width, height); } }
1 2 3 4 5 6 7 8 9 10 11 12
//目标类的调用 public class AdapterDemo { public static void main(String[] args) { Shape[] shapes = {new RectangleAdapter(new Rectangle()), new LineAdapter(new Line())}; int x1 = 10, y1 = 20; int x2 = 30, y2 = 60; for (Shape shape : shapes) { shape.draw(x1, y1, x2, y2); } } }
//创建访问者对象实例 class UpVisitor implements Visitor { public void visit(FOO foo) { System.out.println("do Up on " + foo.getFOO()); }
public void visit(BAR bar) { System.out.println("do Up on " + bar.getBAR()); } }
class DownVisitor implements Visitor { public void visit(FOO foo) { System.out.println("do Down on " + foo.getFOO()); }
public void visit(BAR bar) { System.out.println("do Down on " + bar.getBAR()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Client 进行访问 public class VisitorDemo { public static void main( String[] args ) { Element[] list = {new FOO(), new BAR()}; UpVisitor up = new UpVisitor(); DownVisitor down = new DownVisitor(); for (Element element : list) { element.accept(up); } for (Element element : list) { element.accept(down); } } }
//Now we can extend this class in order to implement actual games:
class Monopoly extends Game { /* Implementation of necessary concrete methods */ void initializeGame() {/* ...*/ } void makePlay(int player) {/* ...*/} boolean endOfGame() {/* ...*/} void printWinner() { /* ...*/} /* Specific declarations for the Monopoly game. */ // ... }
class Chess extends Game { /* Implementation of necessary concrete methods */ void initializeGame() {/* ...*/} void makePlay(int player) {/* ...*/} boolean endOfGame() {/* ... */} void printWinner() {/* ...*/} /* Specific declarations for the chess game. */ // ... }
1 2 3 4 5 6 7
public class Player { public static void main(String[] args) { Game chessGame = new Chess(); chessGame.initializeGame(); chessGame.playOneGame(1); //call template method } }
// The context class uses this to call the concrete strategy interface Strategy { void execute(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Implements the algorithm using the strategy interface class FirstStrategy implements Strategy { public void execute() { System.out.println("Called FirstStrategy.execute()"); } }
class SecondStrategy implements Strategy { public void execute() { System.out.println("Called SecondStrategy.execute()"); } }
class ThirdStrategy implements Strategy { public void execute() { System.out.println("Called ThirdStrategy.execute()"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object class Context {
Strategy strategy;
// Constructor public Context(Strategy strategy) { this.strategy = strategy; }
public void execute() { this.strategy.execute(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class StrategyExample { public static void main(String[] args) {
Context context;
// Three contexts following different strategies context = new Context(new FirstStrategy()); context.execute();
context = new Context(new SecondStrategy()); context.execute();
context = new Context(new ThirdStrategy()); context.execute(); } }
public class StateDemo { public static void main(String[] args) { CeilingFanPullChain chain = new CeilingFanPullChain(); while (true) { System.out.print("Press ENTER"); getLine(); chain.pull(); } }
static String getLine() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = null; try { line = in.readLine(); } catch (IOException ex) { ex.printStackTrace(); } return line; } }
class HexObserver extends Observer { public HexObserver(Subject subject) { this.subject = subject; this.subject.add(this); }
public void update() { System.out.print(" " + Integer.toHexString(subject.getState())); } }
class OctObserver extends Observer { public OctObserver(Subject subject) { this.subject = subject; this.subject.add( this ); }
public void update() { System.out.print(" " + Integer.toOctalString(subject.getState())); } }
class BinObserver extends Observer { public BinObserver(Subject subject) { this.subject = subject; this.subject.add(this); }
public void update() { System.out.print(" " + Integer.toBinaryString(subject.getState())); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public class ObserverDemo { public static void main( String[] args ) { Subject sub = new Subject(); // Client configures the number and type of Observers new HexObserver(sub); new OctObserver(sub); new BinObserver(sub); Scanner scan = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.print("\nEnter a number: "); sub.setState(scan.nextInt()); } } }
public Memento(String state) { this.state = state; }
public String getState() { return state; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Originator { private String state; /* lots of memory consumptive private data that is not necessary to define the * state and should thus not be saved. Hence the small memento object. */
public void setState(String state) { System.out.println("Originator: Setting state to " + state); this.state = state; }
public Memento save() { System.out.println("Originator: Saving to Memento."); return new Memento(state); } public void restore(Memento m) { state = m.getState(); System.out.println("Originator: State after restoring from Memento: " + state); } }
1 2 3 4 5 6 7 8 9 10 11
class Caretaker { private ArrayList<Memento> mementos = new ArrayList<>();
public void addMemento(Memento m) { mementos.add(m); }
public Memento getMemento(int index) { return mementos.get(index); } }
class Producer implements Runnable { // 2. Producers are coupled only to the Mediator private Mediator med; private int id; private static int num = 1;
public Producer(Mediator m) { med = m; id = num++; }
@Override public void run() { int num; while (true) { med.storeMessage(num = (int)(Math.random()*100)); System.out.print( "p" + id + "-" + num + " " ); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Consumer implements Runnable { // 3. Consumers are coupled only to the Mediator private Mediator med; private int id; private static int num = 1;
public Consumer(Mediator m) { med = m; id = num++; }
@Override public void run() { while (true) { System.out.print("c" + id + "-" + med.retrieveMessage() + " "); } } }
public double evaluate(Map<String, Integer> context) { double result = 0; double a = left.evaluate(context); double b = right.evaluate(context); if (operation == '+') { result = a + b; } if (operation == '-') { result = a - b; } if (operation == '*') { result = a * b; } if (operation == '/') { result = a / b; } return result; } }
/* The test class or client */ public class PressSwitch { public static void main(String[] args){ Light lamp = new Light(); Command switchUp = new FlipUpCommand(lamp); Command switchDown = new FlipDownCommand(lamp);
Switch mySwitch = new Switch();
try { if ("ON".equalsIgnoreCase(args[0])) { mySwitch.storeAndExecute(switchUp); } else if ("OFF".equalsIgnoreCase(args[0])) { mySwitch.storeAndExecute(switchDown); } else { System.out.println("Argument \"ON\" or \"OFF\" is required."); } } catch (Exception e) { System.out.println("Arguments required."); } } }
public class ChainOfResponsibilityExample { public static void main( String[] args ) { // Build the chain of responsibility Logger l = new StdoutLogger( Logger.DEBUG).setNext( new EmailLogger( Logger.NOTICE ).setNext( new StderrLogger( Logger.ERR ) ) );
// Handled by StdoutLogger l.message( "Entering function y.", Logger.DEBUG );
// Handled by StdoutLogger and EmailLogger l.message( "Step1 completed.", Logger.NOTICE );
// Handled by all three loggers l.message( "An error has occurred.", Logger.ERR ); } }