接口型模式
1.Adapter(适配器)模式
对象适 配器(Object Adapter)
public interface IPeg {void insertIntoHole();}public class SquarePeg implements IPeg {@Overridepublic void insertIntoHole() {System.out.println("I'm inserting into square hole...");// other logic...}}public interface IRoundPeg {void insertIntoRoundHole();}public class RoundPeg implements IRoundPeg {@Overridepublic void insertIntoRoundHole() {System.out.println("I'm inserting into round hole...");// other logic...}}public class RoundPegAdapter implements IPeg {private IRoundPeg roundPeg;public RoundPegAdapter(IRoundPeg roundPeg) {this.roundPeg = roundPeg;}@Overridepublic void insertIntoHole() {roundPeg.insertIntoRoundHole();}}
public class RoundPegAdapter2 extends RoundPeg implements IPeg {@Overridepublic void insertIntoHole() {super.insertIntoRoundHole();}}
public class TwoWayPegAdapter implements IRoundPeg, IPeg {private IPeg squarePeg;private IRoundPeg roundPeg;public TwoWayPegAdapter(IPeg squarePeg) {this.squarePeg = squarePeg;}public TwoWayPegAdapter(IRoundPeg roundPeg) {this.roundPeg = roundPeg;}@Overridepublic void insertIntoRoundHole() {squarePeg.insertIntoHole();}@Overridepublic void insertIntoHole() {roundPeg.insertIntoRoundHole();}}public class TestDrive {public static void main(String[] args) {TestDrive test = new TestDrive();IPeg squarePeg = new SquarePeg();IRoundPeg roundPeg = new RoundPeg();RoundPegAdapter adpater = new RoundPegAdapter(roundPeg);System.out.println("Testing square peg...");test.testPeg(squarePeg);System.out.println("\nTesting square adapter peg...");test.testPeg(adpater);TwoWayPegAdapter roundPeg2 = new TwoWayPegAdapter(roundPeg);TwoWayPegAdapter squarePeg2 = new TwoWayPegAdapter(squarePeg);System.out.println("\nTesting a 2-way square adapter peg...");test.testPeg(roundPeg2);System.out.println("\nTesting 2-way round adapter peg...");test.testRoundPeg(squarePeg2);}private void testPeg(IPeg peg) {peg.insertIntoHole();}private void testRoundPeg(IRoundPeg peg) {peg.insertIntoRoundHole();}}public class HotelReceptionist { public void subscribe() { System.out.println("Subscribe a table..."); }}public class Cook { public void cookDish() { System.out.println("Cooking dishes..."); }}public class Waitress { public void serveDishes() { System.out.println("Serving dishes..."); } public void waitForAnOrder() { System.out.println("Waiting for the order..."); }}public class Cashier { public void check() { System.out.println("Check the bill..."); }}public class Assistant { private HotelReceptionist hotelReceptionist; private Cook cook; private Waitress waitress; private Cashier cashier; public Assistant(HotelReceptionist hotelReceptionist, Cook cook, Waitress waitress, Cashier cashier) { this.hotelReceptionist = hotelReceptionist; this.cook = cook; this.waitress = waitress; this.cashier = cashier; } public void prepareDinner() { hotelReceptionist.subscribe(); waitress.waitForAnOrder(); cook.cookDish(); } public void endDinner() { waitress.serveDishes(); cashier.check(); }}public class Boss { private Assistant assistant; public Boss(Assistant assistant) { this.assistant = assistant; } public void treat() { assistant.prepareDinner(); address(); assistant.endDinner(); } private void address() { System.out.println("Boss is bitching : "Tomorrow is going to be better, we will make blah blah...""); }}public class FacadeTestDrive { public static void main(String[] args) { Cashier cashier = new Cashier(); Cook cook = new Cook(); HotelReceptionist hotelReceptionist = new HotelReceptionist(); Waitress waitress = new Waitress(); Assistant assistant = new Assistant(hotelReceptionist, cook, waitress, cashier); Boss boss = new Boss(assistant); boss.treat(); }}
public abstract class BranchComponent { public String getName() { throw new UnsupportedOperationException(); } public String getDiscription() { throw new UnsupportedOperationException(); } public void display() { throw new UnsupportedOperationException(); }}import java.util.ArrayList;import java.util.List;public class BranchComposite extends BranchComponent { private String name; private String discription; private List<BranchComponent> childrenBranch; public BranchComposite(String name, String discription) { this.name = name; this.discription = discription; childrenBranch = new ArrayList<BranchComponent>(); } public void display() { System.out.printf("%s: %s\n", name, discription); for (BranchComponent child : childrenBranch) { child.display(); } } public String getName() { return name; } public String getDiscription() { return discription; } public void add(BranchComponent child) { childrenBranch.add(child); } public void remove(BranchComponent child) { childrenBranch.remove(child); } public BranchComponent getChild(int index) { return childrenBranch.get(index); }}public class BranchLeaf extends BranchComponent { private String name; private String discription; public BranchLeaf(String name, String discription) { this.name = name; this.discription = discription; } public void display() { System.out.printf("\t%s: %s\n", name, discription); } public String getName() { return name; } public String getDiscription() { return discription; }}public class TestDrive { public static void main(String[] args) { BranchComposite china = new BranchComposite("CN", "China Branch"); BranchComposite shanghai = new BranchComposite("Sh", "Shanghai Branch"); BranchLeaf huangpu = new BranchLeaf("Hp", "Huangpu Branch"); BranchLeaf yangpu = new BranchLeaf("Yp", "Yangpu Branch"); BranchLeaf pudong = new BranchLeaf("Pd", "Pudong Branch"); BranchComposite beijing = new BranchComposite("Bj", "Beijing Branch"); BranchLeaf dongcheng = new BranchLeaf("Dc", "Dongcheng Branch"); BranchLeaf xicheng = new BranchLeaf("Xc", "Xicheng Branch"); BranchLeaf haidian = new BranchLeaf("Hd", "Haidian Branch"); shanghai.add(huangpu); shanghai.add(yangpu); shanghai.add(pudong); beijing.add(dongcheng); beijing.add(xicheng); beijing.add(haidian); china.add(shanghai); china.add(beijing); System.out.println("Displaying the head bank information"); display(china); System.out.println("\nDisplaying Shanghai bank branch information"); display(shanghai); System.out.println("\nDisplaying Pudong bank branch information in Shanghai"); display(pudong); } private static void display(BranchComponent branch) { branch.display(); }}
public abstract class BranchComponent { public String getName() { throw new UnsupportedOperationException(); } public String getDiscription() { throw new UnsupportedOperationException(); } public void display() { throw new UnsupportedOperationException(); } public void add(BranchComponent child) { throw new UnsupportedOperationException(); } public void remove(BranchComponent child) { throw new UnsupportedOperationException(); } public BranchComponent getChild(int index) { throw new UnsupportedOperationException(); }}import java.util.ArrayList;import java.util.List;public class BranchComposite extends BranchComponent { private String name; private String discription; private List<BranchComponent> childrenBranch; public BranchComposite(String name, String discription) { this.name = name; this.discription = discription; childrenBranch = new ArrayList<BranchComponent>(); } public void display() { System.out.printf("%s: %s\n", name, discription); for (BranchComponent child : childrenBranch) { child.display(); } } public String getName() { return name; } public String getDiscription() { return discription; } public void add(BranchComponent child) { childrenBranch.add(child); } public void remove(BranchComponent child) { childrenBranch.remove(child); } public BranchComponent getChild(int index) { return childrenBranch.get(index); }}public class BranchLeaf extends BranchComponent { private String name; private String discription; public BranchLeaf(String name, String discription) { this.name = name; this.discription = discription; } public void display() { System.out.printf("\t%s: %s\n", name, discription); } public String getName() { return name; } public String getDiscription() { return discription; }}public class TestDrive { public static void main(String[] args) { BranchComposite china = new BranchComposite("CN", "China Branch"); BranchComposite shanghai = new BranchComposite("Sh", "Shanghai Branch"); BranchLeaf huangpu = new BranchLeaf("Hp", "Huangpu Branch"); BranchLeaf yangpu = new BranchLeaf("Yp", "Yangpu Branch"); BranchLeaf pudong = new BranchLeaf("Pd", "Pudong Branch"); BranchComposite beijing = new BranchComposite("Bj", "Beijing Branch"); BranchLeaf dongcheng = new BranchLeaf("Dc", "Dongcheng Branch"); BranchLeaf xicheng = new BranchLeaf("Xc", "Xicheng Branch"); BranchLeaf haidian = new BranchLeaf("Hd", "Haidian Branch"); shanghai.add(huangpu); shanghai.add(yangpu); shanghai.add(pudong); beijing.add(dongcheng); beijing.add(xicheng); beijing.add(haidian); china.add(shanghai); china.add(beijing); System.out.println("Displaying the head bank information"); display(china); System.out.println("\nDisplaying Shanghai bank branch information"); display(shanghai); System.out.println("\nDisplaying Pudong bank branch information in Shanghai"); display(pudong); } private static void display(BranchComponent branch) { branch.display(); }}