6. Adapter mode (Adapter)
The adapter pattern transforms the interface of a class into another interface that the client expects, in order to eliminate compatibility issues with classes caused by mismatched interfaces. There are three main categories: The adapter mode of the class, the adapter mode of the object, and the adapter mode of the interface. First, let's take a look at the class's adapter pattern , first look at the class diagram:
The core idea is: There is a source class, has a method, to be adapted, the target interface when targetable, through the adapter class, the function of the source extension to targetable, see the code:
public class Source {public void method1 () {System.out.println ("This is original method!");}}
Public interface Targetable {/* is identical to the method in the original class */public void Method1 ();/* Method of the new class */public void Method2 ();}
public class Adapter extends Source implements targetable {@Overridepublic void Method2 () {System.out.println ("the Targetable method! ");}}
The adapter class inherits the source class, implements the Targetable interface, and the following is the test class:
public class Adaptertest {public static void main (string[] args) {targetable target = new Adapter (); Target.method1 (); targe T.METHOD2 ();}}
Output:
This is original method! This is the targetable method!
Thus, the implementation class of the Targetable interface has the function of the source class.
The adapter mode of the object
The basic idea is the same as the adapter pattern of the class, only the adapter class is modified, this time does not inherit the source class, but holds an instance of the source class to solve the compatibility problem. Look at the picture:
Only need to modify the source code of the adapter class:
public class Wrapper implements targetable {private source Source;public Wrapper (source source) {super (); This.source = Sou RCE;} @Overridepublic void Method2 () {System.out.println ("This is the targetable method!"); @Overridepublic void Method1 () {source.method1 ();}}
Test class:
public class Adaptertest {public static void main (string[] args) {Source Source = new Source (); targetable target = new Wrapper (source); Target.method1 (); Target.method2 ();}}
The output is the same as the first, except that the method of adaptation is different.
The third adapter mode is the adapter mode of the interface, the adapter of the interface is this: sometimes we write an interface with a number of abstract methods, when we write the implementation class of the interfaces, we must implement all the methods of the interface, which is obviously sometimes wasteful, because not all of the methods we need , sometimes only need some, here in order to solve this problem, we introduced the interface of the adapter pattern, with the help of an abstract class, the abstract class implementation of the interface, the implementation of all methods, and we do not deal with the original interface, only with the abstract class to get in touch, so we write a class, inherit the abstract class, Just rewrite the method we need. Look at the class diagram:
This is a good understanding, in practical development, we also often encounter this interface in the definition of too many methods, so that sometimes we do not need in some implementation classes. Look at the code:
Public interface sourceable {public void method1 ();p ublic void Method2 ();
Abstract class Wrapper2:
Public abstract class Wrapper2 implements Sourceable{public void Method1 () {}public void Method2 () {}}
public class SourceSub1 extends Wrapper2 {public void method1 () {System.out.println ("The Sourceable interface ' s first Sub1 !");}}
public class SourceSub2 extends Wrapper2 {public void Method2 () {System.out.println ("The Sourceable interface ' s second Sub 2! ");}}
public class Wrappertest {public static void main (string[] args) {sourceable source1 = new SourceSub1 (); sourceable source2 = new SourceSub2 (); Source1.method1 (); Source1.method2 (); Source2.method1 (); Source2.method2 ();}}
Test output:
The Sourceable interface ' s first sub1! The Sourceable interface ' s second sub2!
We have achieved the effect!
Having talked so much, summarize the application scenarios for three adapter modes:
Class's adapter pattern: When you want to convert a class to a class that satisfies another new interface , you can use the class's adapter pattern, create a new class, inherit the original class, and implement the new interface.
object's adapter pattern: When you want to convert an object to an object that satisfies another new interface, you can create a wrapper class that holds an instance of the original class, and in the method of the wrapper class, the method that invokes the instance is the line.
Interface Adapter Mode: When you do not want to implement all the methods in an interface, you can create an abstract class wrapper, implement all the methods, we write other classes, when we inherit the abstract class.
7. Decoration Mode (Decorator)
As the name implies, the decorative mode is to add some new features to an object, and is dynamic, requires decorative objects and objects are decorated to achieve the same interface, decorative objects hold the object of the decoration objects, the diagram is as follows:
The source class is a decorated class, and the decorator class is a decorative class that can dynamically add some functionality to the source class, with the following code:
Public interface Sourceable {public void method ();
public class Source implements sourceable {@Overridepublic void method () {System.out.println ("the original method!");}}
public class Decorator implements sourceable {private sourceable source;public Decorator (sourceable source) {super (); This.source = source;} @Overridepublic void Method () {System.out.println ("before decorator!"); Source.method (); System.out.println ("after decorator!");}}
Test class:
public class Decoratortest {public static void main (string[] args) {sourceable Source = new Source (); sourceable obj = new Decorator (source); Obj.method ();}}
Output:
Before decorator! The original method! After decorator!
Application Scenarios for Adorner mode:
1, need to extend the functionality of a class.
2, dynamic for an object to add functionality, but also can be dynamically revoked. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )
Cons: Produce too many similar objects, not easy to debug!
8, Agent mode (proxy)
In fact, each pattern name indicates the role of the mode, proxy mode is more than one proxy class out, for the original object to do some operations, such as we rent the house when back to find intermediary, why? Because you do not have comprehensive information about the housing in the area, I hope to find a more familiar person to help you do, the agent here is the meaning. If we have a lawsuit, we need a lawyer, because the lawyer has expertise in law and can act on our behalf and express our ideas. Take a look at the diagram first:
According to the above explanation, the proxy mode is easier to understand, we look at the code:
Public interface Sourceable {public void method ();
public class Source implements sourceable {@Overridepublic void method () {System.out.println ("the original method!");}}
public class Proxy implements sourceable {private source Source;public proxy () {super (); this.source = new Source ();} @Overridepublic void Method () {before (); Source.method (); Atfer ();} private void Atfer () {System.out.println ("after proxy!"); private void Before () {System.out.println ("before proxy!");}}
Test class:
public class Proxytest {public static void main (string[] args) {sourceable Source = new Proxy (); Source.method ();}}
Output:
Before proxy! The original method! After proxy!
Application Scenarios for Proxy mode:
If existing methods need to be improved when used, there are two ways to do this:
1, modify the original method to adapt. This violates the principle of "open for expansion, closed for modification".
2, is to use a proxy class to call the original method, and the resulting results are controlled. This approach is proxy mode.
Using the proxy mode, you can divide the function more clearly and help to maintain later!
9. Appearance mode (facade)
The appearance pattern is to solve the dependency of class and class, like spring, the relationship between classes and classes can be configured into the configuration file, and the appearance pattern is to put their relationship in a facade class, reducing the coupling between class classes, which does not involve interfaces, Look at the class diagram: (We take the startup process of a computer as an example)
Let's look at the implementation class first:
public class CPU {public void startup () {System.out.println ("CPU startup!");} public void shutdown () {System.out.println ("CPU shutdown!");}}
public class Memory {public void startup () {System.out.println ("Memory startup!");} public void shutdown () {System.out.println ("Memory shutdown!");}}
public class Disk {public void startup () {System.out.println ("Disk startup!");} public void shutdown () {System.out.println ("Disk shutdown!");}}
public class Computer {Private CPU Cpu;private memory memory;private Disk disk;public computer () {CPU = new CPU (); memory = New Memory ();d ISK = new disk (); public void Startup () {System.out.println ("Start the computer!"); Cpu.startup (); Memory.startup ();d isk.startup (); System.out.println ("Start computer finished!");} public void shutdown () {System.out.println ("begin to close the computer!"); Cpu.shutdown (); Memory.shutdown ();d isk.shutdown (); SYSTEM.OUT.PRINTLN ("Computer closed!");}}
The user class is as follows:
public class User {public static void main (string[] args) {Computer computer = new computer (); Computer.startup (); computer. Shutdown ();}}
Output:
Start the computer! CPU startup! Memory startup! Disk startup! Start Computer finished! Begin to close the computer! CPU shutdown! Memory shutdown! Disk shutdown! Computer closed!
If we do not have computer class, then, CPU, Memory, disk they will hold each other instance, the relationship, which will cause serious dependence, modify a class, may bring other classes of modification, this is not what we want to see, with the Computer class, The relationship between them is placed in the computer class, so that the understanding of the role of decoupling, which is the appearance of the pattern!
10. Bridging mode (bridge)
Bridging mode is the separation of things from their specific implementations so that they can change independently of each other. Bridging is intended to: the abstraction and implementation of decoupling, so that they can change independently , like our common JDBC Bridge DriverManager, JDBC to connect to the database, the database switch between the various databases, basically do not need to move too much code, Even without moving, the reason is that JDBC provides a unified interface, each database provides its own implementation, with a program called database-driven to bridge the line. Let's take a look at the diagram:
Implementation code:
Define the interface first:
Public interface Sourceable {public void method ();
Define two implementation classes, respectively:
public class SourceSub1 implements sourceable {@Overridepublic void method () {System.out.println ("This is the first sub!" );}}
public class SourceSub2 implements sourceable {@Overridepublic void method () {System.out.println ("This is the second sub!" );}}
Define a bridge that holds an instance of sourceable:
Public abstract class Bridge {private sourceable source;public void Method () {Source.method ();} Public sourceable GetSource () {return source;} public void SetSource (sourceable source) {This.source = source;}}
public class Mybridge extends Bridge {public void method () {GetSource (). method ();}}
Test class:
public class Bridgetest {public static void main (string[] args) {Bridge bridge = new Mybridge ();/* Call first object */sourceable sour Ce1 = new SourceSub1 (); Bridge.setsource (Source1); Bridge.method ();/* Call Second object */sourceable Source2 = new SourceSub2 (); Bridge.setsource (SOURCE2); Bridge.method ();}}
Output
This is the first sub! This is the second sub!
In this way, the call of the bridge class is implemented, and the implementation class SOURCESUB1 and SOURCESUB2 of the docking port sourceable are realized. Next I draw a diagram, we should understand, because this diagram is the principle of our JDBC connection, there is a database learning basis, a combination of all understand.
11. Combination Mode (Composite)
The combination mode is sometimes called part-the overall pattern is convenient when dealing with a tree-like structure, and look at the diagram:
Look directly at the code:
public class TreeNode {private String name;private TreeNode parent;private vector<treenode> children = new Vector< ; Treenode> ();p ublic TreeNode (String name) {this.name = name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public TreeNode getParent () {return parent;} public void SetParent (TreeNode parent) {this.parent = parent;} Add child node public void Add (TreeNode node) {children.add (node);} Delete child node public void Remove (TreeNode node) {children.remove (node);} Get child node public enumeration<treenode> GetChildren () {return children.elements ();}}
public class Tree {TreeNode root = null;public tree (String name) {root = new TreeNode (name);} public static void Main (string[] args) {Tree tree = new Tree ("A"); TreeNode NodeB = new TreeNode ("B"); TreeNode NodeC = new TreeNode ("C"); Nodeb.add (NODEC); Tree.root.add (NodeB); SYSTEM.OUT.PRINTLN ("Build the Tree finished!");}}
Use scene: Combine multiple objects together to manipulate them, often in a tree structure, such as a binary tree, a number, and so on.
12. Enjoy meta-mode (Flyweight)
The main purpose of the enjoy meta-mode is to share the shared pool, which can reduce the overhead of memory when there are many objects in the system, and is usually used in conjunction with the Factory mode.
Flyweightfactory is responsible for creating and managing the unit of entitlement, and when a client requests, the factory checks to see if there are any eligible objects in the current object pool, and if so, returns an already existing object, and if not, creates a new object, flyweight is a superclass. A reference to the shared pool, we can easily associate with the Java JDBC Connection pool, think of the characteristics of each connection, we can not be difficult to summarize: for the sharing of some objects, they have a number of common properties, take the database connection pool, URL, Driverclassname, Username, password and dbname, these properties are the same for each connection, so it is appropriate to use the enjoy meta-mode to process, build a factory class, the above-mentioned similar attributes as internal data, and other as external data, in the method call, as a parameter passed in, This saves space and reduces the number of instances.
Look at an example:
Look at the code for the database connection pool:
public class ConnectionPool {private vector<connection> pool;/* public properties */private String url = "Jdbc:mysql://localhost:3306/test";p rivate string username = "root";p rivate string password = "root";p rivate String driverclassname = "Com.mysql.jdbc.Driver";p rivate int poolsize = 100;private static ConnectionPool instance = NULL ; Connection conn = null;/* construction method, do some initialization work */private ConnectionPool () {pool = new vector<connection> (poolsize); for ( int i = 0; i < poolsize; i++) {try {class.forname (driverclassname); conn = drivermanager.getconnection (URL, username, password);p OOL.ADD (conn) ;} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ()}}} /* Return connection to connection pool */public synchronized void release () {POOL.ADD (conn);} /* Returns a database connection in the connection pool */public synchronized Connection getconnection () {if (pool.size () > 0) {Connection conn = pool.get (0) ;p Ool.remove (conn); return conn;} else {return null;}}}
Through the management of connection pool, it realizes the sharing of database connection, does not need to recreate the connection every time, saves the cost of database re-creation, and improves the performance of the system!
Design mode 2