18.Command
19.bridge
18.Command Description: Command mode is used in a lot of, very good models in the Java menu, and everyone often uses. The following Java code is very clear, I will not talk nonsense.
Command Definition Incoming a request from the client, does not need to understand the action of the request activation or the details of the processing accepting this request.
This is a mode of communication contact nature between two machines, similar to the traditional process language Callback function.
Advantages: Unintended between senders and recipients. The sender calls an operation, the recipient accepts the request to perform the corresponding action because the sender does not need to know any interface of the recipient using the Command mode.
The code of many Command mode is for the graphical interface. It is actually the menu command, when we select a command in a drop-down menu, then do some actions.
Packing these commands into a class, then the user (caller) operates on this class, this is the Command mode, in other words, the original user (caller) is directly called these commands, such as the menu open the document (The caller), directly point to the code to open the document, use the Command mode, which is increasing between the two, and the direct relationship is broken, and both are isolated, it is basically no relationship.
Obviously, the benefits of doing this are in line with the characteristics of the package, reduce the coupling, and Command is a typical mode that will encapsulate behavior. Factory is a mode that will be created, from the Command mode, I also found a "Tongxia": Good I like to complicate simple questions. I like to increase the third party in different classes, of course, doing the robustness that is conducive to the code is still reused.
Example: Specific Command Mode code is a wide variety of how to encapsulate commands, different systems, and different practices. The following example is to encapsulate the command in a collection of list, and once any object is added, it is actually loaded. In a closed black box, the characteristics of the object disappeared, only when it is taken out, it is possible to distinguish it:
Typical Command mode needs to have an interface. There is a unified method in the interface. This is "Packaging commands": public interface command {public abstract void execute ();}
The specific different command / request code is implementing the interface Command, and there are three specific commands of the public class engliclic class engager imports command {
Public void execute () {// do engineer's command}} public class programer imports command {
Public void execute () {// do programer's command}}
Public class politicalian imports command {
Public void execute () {// do politician's command}}
According to the usual practice, we can call these three Command directly, but use the Command mode, we have to pack them, throw it into the black box list:
public class producer {public static List produceRequests () {List queue = new ArrayList (); queue.add (new DomesticEngineer ()); queue.add (new Politician ()); queue.add (new Programmer ()); return Queue;}
}
After these three orders entered the list, they have lost their appearance characteristics. After removing, they may not distinguish who is engineer who is Programmer, see how the following client calls Command mode:
Public class testCommand {public static void main (string [] args) {list queue = product.ProduceRequests (); for (Iterator it = queue.ITerator (); it.hasnext ();) // Client directly calls Execute method There is no need to know other more types of method names being called by the caller. ((Command) IT.NEXT ()). Execute ();}}
It can be seen that the caller is basically only dealing with the interface, which does not agree to interact, which also reflects a principle that programming, so that when the fourth specific command is added later, it is not necessary to modify the code in the caller TestCommand.
Understand the core principle of the above code, in use, you should have its own method, especially how to separate the caller and specific commands, there are many implementation methods, the above code is "from list" The practice is just to demonstrate this approach.
A good reason to use the Command mode is also because it can implement the UNDO function. Each specific command can remember the action it just execute and recovered when needed.
Command mode Application in interface design. Java's swing menu command is used using Command mode. Since Java is still lacking in the performance of interface design, we will not discuss the interface design specific code, there are many examples on the Internet. .
19.Bridge Description: The abstraction and behavior are separated, independent, but can dynamically bind. For an alteration, the 2 bit of the computer can represent 4 states, and the two interfaces (each two states) have several combinations? In addition, in C #, the following abstraction class is replaced with an interface to implement it. Look at the example below, there is really nothing to say.
Anything objects have abstractions and behaviors, such as people, people are abstractions, people and women, etc .; people have behavior, behavior has a variety of specific performance, so "people" and "people's behavior" two The concept also reflects the abstraction and behavior.
In the basic concept of object-oriented design, the concept is actually consisting of two parts of the attribute and behavior. We can think that it is a stationary, an abstraction, in general, behavior is included in an object However, in some cases, we need to categorize these behaviors to form a total behavior interface, which is the use of bridge mode.
Why use? Do not want abstract parts and behavior there is a fixed binding relationship, but should be able to touch.
If an abstract or interface has multiple specific implementation (subclasses, concrete subclass), the relationship between these subclasses may have the following cases: 1. Concept of this multiple subclasses is parallel, as in front of example, piling There are two concrete classes: square piles and circular piles; the piles on both shapes are parallel, and there is no conceptual repetition.
2. The contents of this multiple subclasses overlap. Then we need to separate the abstract common partial and behaviors. It is ready to put it in an interface. Now you need to design two interfaces: abstract interface and behavior The interface is placed and placed separately.
For example, a cup of coffee is an example, the subclasses are achieved as four: medium cups and milk, large cups and milk, the cup does not add milk, the cup does not add milk.
However, we note that there is a concept overlap in the four subclasses above, and can be considered from another angle. These four classes are actually a combination of two roles: abstraction and behavior, where abstraction is: medium cup and big cup; behavior For: adding milk and can't add milk (such as add orange juice plus apple juice).
Implementing the four subclasses have a fixed binding relationship between abstraction and behavior. If the behavior of adding grape juice is increased in the future, it must add two classes: Cup plus grape juice and cup of grape juice. Obviously confusion, the spread is extremely poor.
Then we use the Bridge mode from the perspective of separation abstraction and behavior. How to achieve? Coffee mentioned above as an example. We originally designed an interface (abstract class), after using Bridge mode, we need to separate abstracts and behaviors, plus, and do not add milk, we will abstract them A special behavior interface.
First look at the interface code of the abstract part:
Public Abstract Class Coffee {CoffeeImp CoffeeImp;
Public void setCoffeeImp () {this.coffeemep = coffeeImpsingleton.getthecoffeimp ();
Public coffeeimp getCoffeeImp () {Return this.coffeeImp;
Public Abstract void PourCoffee ();
Among them, CoffeeImp is an active behavior interface that does not add milk, and the code is as follows:
Public Abstract Class CoffeeImp {Public Abstract Void PourCoffeeImp ();
Now we have two abstract classes, and we will inherit them, implement the Concrete Class:
// Medium Cup Public Class MediumCoffee Extends Coffee {PUBLIC MediumCoffee () {setCoffeeImp ();
Public void PourCoffee () {coffeeImp coffeeimp = this.getCoffeeImp (); // We use the number of repetitions to be a cup or a big cup, repeating 2 times is Medium Cup for (int i = 0; i <2; i ) {
CoffeeImp.PourCoffeeImp ();}}}
// Mug of public class supersizecoffee extends coffee {public supersizecoffee () {setCoffeeImp ();
Public void PourCoffee () {coffeeImp coffeeimp = this.getCoffeeImp (); // We use the number of repetitions to be a cup or a big cup, repeat 5 times is a big cup for (INT i = 0; i <5; i ) {
CoffeeImp.PourCoffeeImp ();}}}
The above is the specific implementation of the middle cup and the cup. The following will inherit the behavior CoffeeImp:
/ / Milk PUBLIC CLASS MILKCOFFEEIMP EXTENDS COFFEEIMP {MilkCoffeeEimp () {}
Public void PourCoffeeImp () {system.out.println ("Add Delicious Milk");}}
// Do not add PUBLIC CLASS FRAGRANTCOFEEIMP EXTENDS COFFEEIMP {FragrantCoffeeImp () {}
Public void PourCoffeeImp () {system.out.println ("Nothing, Sceon");}}
Basic framework of Bridge mode We have already touched, don't forget to have a definition: dynamic combination, we can now drink at least four coffee: 1. Central Cup plus milk 2. Central Cup does not add milk 3. Cup plus Milk 4. Cup does not add milk
See how you are dynamically combined, before use, we are prepared, design a single-state class (Singleton) used to Hold CoffeeImp:
Public class coffeeimpsingleton {private static coffeeImp coffeeimp;
Public coffeeimpsingleton (CoffeeImp CoffeeImpin) {this.coffeeImp = coffeeImpin;} public static coffeeimp getthecoffeeImp () {Return coffeeimp;}}
Take a look at how Cup plus milk and cup adds:
// Take out the milk coffeeimpsingleton coffeeimpsingleton = New CoffeeImpSingleton (New MilkCoffeeImp ());
// Medium Cup Made Made MediumCoffee MediumCoffee = New MediumCoffee (); MediumCoffee.PourCofee ();
// Cup of milk supersizecoffee supersizecoffee = new supersizecoffee (); supersizeCoffee.PourCofee ();
Note: The execution class of Bridge mode such as CoffeeImp and Coffee are one-on-one relationship, and correctly create CoffeeImp is the key to this mode.
Bridge mode There is a Data Access Object (DAO) mode in EJB in EJB, which is separate from business logic and specific data resources because different databases have different database operations. Acting different databases will be independently abstract A behavior interface DAO. As follows:
1.Business Object (Similar Coffee)
Implement some abstract business operations: If you look for all orders under one user
DAOMPLEMENTOR is used in database operations.
2.Data Access Object (Similar to CoffeeImp)
Some abstraction of database resource operations
3. DaoImplement, such as OrderDaocs, OrderDaooracle, ORDERDAOSYBASE (similar MilkCoffeeImp FragrantCoffeeImp)
For specific database operations, such as "Insert INTO", ORDERDAOORACLE is Oracle ORDERDAOSYBASE is a Sybase database.
4. Database (Cloudscape, Oracle, or Sybase Database Via JDBC API)
------------------------------ Write here I am going to take a break, according to the "design model" book, there is also Several modes, they are Flyweight / Visitor / Interpreter / Chain of Responsibility / Memento, these modes are not as good as the previous mode, everyone is looking for it online, or wait for me to come again. The above places are only personal understanding, not to ensure correct, I will take a good look at the masterpiece of several design patterns, if there is a new experience.