【正文】
icipant Correspondence: – The abstract ―painting‖ corresponds to the Component. A concrete painting corresponds to the ConcreteComponent. The frame and matte correspond to the Decorator. ? Consequences: – Adding or removing frames and mattes provide more flexibility than requiring all paintings to have the same frame. Paintings can be customized with the addition of mattes and frames. The cost of customization is determined by the framing and matting options chosen. The decorator and ponent remain separate. Decorator The graphics displays used on GUI desktops use decorators such as toolbars, status bars, and scroll bars. Decorator ? Purpose – attaches additional responsibilities to an object dynamically. ? Participant Correspondence: – The windowcorresponds to the Component. A specific window corresponds to the ConcreteComponent. The borders, status bars, and scroll bars correspond to the Decorator. ? Consequences: – Adding items such as scroll bars as needed provides more flexibility than requiring all windows to have scroll bars. If scrolling is not needed, the cost of a scroll bar is not incurred. The decorator and ponent remain separate. Decorator Code (JUnit) //這個就是抽象構(gòu)件角色 public interface Test { /** * Counts the number of test cases that will be run by this test. */ public abstract int countTestCases()。 /** * Runs a test and collects its result in a TestResult instance. */ public abstract void run(TestResult result)。 } //具體構(gòu)件對象,但是這里是個抽象類 public abstract class TestCase extends Assert implements Test { …… public int countTestCases() { return 1。 } …… public TestResult run() { TestResult result= createResult()。 run(result)。 return result。 } public void run(TestResult result) { (this)。 } …… } //裝飾角色 public class TestDecorator extends Assert implements Test { //這里按照上面的要求,保留了一個對構(gòu)件對象的實例 protected Test fTest。 public TestDecorator(Test test) { fTest= test。 } /** * The basic run behaviour. */ public void basicRun(TestResult result) { (result)。 } public int countTestCases() { return ()。 } public void run(TestResult result) { basicRun(result)。 } public String toString() { return ()。 } public Test getTest() { return fTest。 } } //具體裝飾角色,這個類的增強作用就是可以設(shè)置測試類的執(zhí)行次數(shù) public class RepeatedTest extends TestDecorator { private int fTimesRepeat。 public RepeatedTest(Test test, int repeat) { super(test)。 if (repeat 0) throw new IllegalArgumentException(Repetition count must be 0)。 fTimesRepeat= repeat。 } //看看怎么裝飾的吧 public int countTestCases() { return ()*fTimesRepeat。 } public void run(TestResult result) { for (int i= 0。 i fTimesRepeat。 i++) { if (()) break。 (result)。 } } public String toString() { return ()+(repeated)。 } } 使用的時候,就可以采用下面的方式: TestDecorator test = new RepeatedTest(new TestXXX() , 3)。 Facade When ordering from a catalog, consumers do not have direct contact with the Order Fulfillment, Billing, and Shipping departments. The Customer Service Representative acts as a Facade, or unified interface, to each of the departments involved in the transaction. Facade ? Purpose – defines a unified, higher level interface to a subsystem, that makes it easier to use. ? Participant Correspondence: – The customer service representative corresponds to the Fa231。ade. The individual departments correspond to the subsystem classes. ? Consequences: – Clients are shielded from individual departments. When ordering an item, it is not necessary to check the stock, generate an invoice, and arrange for shipping. All steps are acplished through the customer service representative. The internal structure of the anization can be changed without affecting the client. For example, the shipping department can be contracted to another anization, without impacting the client’s interface with the pany. ? Note: – Some catalog department stores require the customer to select the item, check to see if it is in stock, order it, go to a counter to pay for it, and then go to another counter to receive it. In this example, a fa231。ade is not used. Facade The headquarters of an ambulance service consists of several devices for different purposes, for instance ambulances with different levels of equipment and differently skilled crews, pediatric ambulances, helicopters, and so on. On the other hand easy use of this system – .. calling for help – is essential. Facade ? Purpose – defines a unified, higher level interface to a subsystem, that makes it easier to use. ? Participant Correspondence: – The emergency services operator (911 operator in North America) corresponds to the Fa231。ade. The individual emergency services correspond to the subsystem classes. ? Consequences: – Clients are shielded from individual departments. When emergency services are needed, it is not necessary to call the ambulance, police and fire departments separately. The emergency services operator dispatches services as needed. There is a weak coupling between services. In the event of a burglary, the fire brigade need not be dispatched. Regardless of who is dispatched, the client interface remains the same. ? Facade模式的一個典型應(yīng)用就是進行數(shù)據(jù)庫連接。一般我們在每一次對數(shù)據(jù)庫進行訪 問,都要進行以下操作:先得到 connect實例,然后打開 connect獲得連接,得到一個 statement,執(zhí)行 sql語句進