First-appearance mode
Definition: Provides a consistent interface for a group of subsystems, and the appearance pattern defines a high-level interface that makes this subsystem easier to use.
Pros: Loose coupling is easy to use to better divide the level of access
Cons: Should not be overused
Essence: Encapsulation Interaction simple invocation
Public InterfaceTestA { Public voidtest ();} Public InterfaceTESTB { Public voidtest ();} Public InterfaceTESTC { Public voidtest ();} Public classConcretetestaImplementsTestA {@Override Public voidTest () {//TODO auto-generated Method StubSystem.out.println ("Test A"); }} Public classConcretetestbImplementsTestb {@Override Public voidTest () {//TODO auto-generated Method StubSystem.out.println ("Test B"); }} Public classConcretetestcImplementsTESTC {@Override Public voidTest () {//TODO auto-generated Method StubSystem.out.println ("Test C"); }} Public classFacade { Public voidTest () {TestA ta=NewConcretetesta (); Ta.test (); Testb TB=NewConcretetestb (); Tb.test (); TESTC TC=NewCONCRETETESTC (); Tc.test (); }} Public classUser { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFacade FC =Newfacade (); Fc.test (); }}
Second Type adapter mode
Definition: Transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
Pros: Better reusability for better scalability
Disadvantage: Excessive use, will make the system more messy, not easy to grasp the whole
Essence: Conversion matching multiplexing function
Public InterfaceTarget { Public voidrequest ();} Public classAdaptee { Public voidrequest () {System.out.println ("Adaptee Test"); }} Public classAdapterImplementstarget {adaptee apt=NULL; Publicadapter (adaptee apt) { This. Apt =apt; } @Override Public voidrequest () {//TODO auto-generated Method Stubapt.request (); }} Public classUser { Public Static voidMain (string[] args) {adaptee ADP=Newadaptee (); Target Tar=Newadapter (ADP); Tar.request (); }}
The third type of single case mode
Definition: Guarantees that a class has only one instance and provides a global access point to access him. The lazy type is the time to change the space, the A hungry man type is the space change time. In terms of thread security, non-synchronized lazy-a hungry man are threads that are unsafe and thread-safe. The essence of the singleton mode is to control the number of instances, while the use of Singleton mode in a cluster requires attention to security issues. Promotion: The implementation of the cache-space change time.
Lazy Type:
Public classSingleton {Private StaticSingleton instance =NULL; PrivateSingleton () {} Public StaticSingleton getinstance () {if(Instance = =NULL) {instance=NewSingleton (); } returninstance; } }
A Hungry man type:
Public class Singleton { privatestaticnew Singleton (); Private Singleton () { } publicstatic Singleton getinstance () { return instance; } }
Basic implementation of the cache
public class Javacache { private static map<string, object> instance = null ; public Object getinstance (String key) { Object Object = Instance.get (key); if (object = = null " {Object = key + ", value" return object; } }
The
Enumeration type is also a choice for implementing singleton patterns. In addition, a singleton pattern can be implemented by means of a class-level inner class.
public class singleton { private static class Instanceholder { private static Singleton instance = new Singleton (); private Singleton () {} public static Singleton getinstance () { return Instanceholder.instance; } }
Here's an explanation to add: class-level internal classes
-A class-level inner class is a member-style inner class that is decorated with static, and is called an object-style inner class without static adornments. -Class-level inner classes can be created directly, and object-level inner classes need to be bound to the member variables of the external class. -Class-level inner classes can implement static methods, and static methods can use static methods and static member variables of external classes. -Class-level inner classes are quite similar to members of external classes, so they are loaded at the first use. Security issues: The JVM automatically implements synchronization when the static initializer initializes the data-when the final field is accessed-when the object is created before the thread is created-the thread can see the object it is about to work with, thus using class-level inner classes to implement lazy loading. Ensuring thread safety through the JVM itself is the basic idea of implementation.
Java object-oriented and design pattern (ii)