, and if the encapsulation is not important. --------------------------------------------------------------------------------------------- ----- Seven design principles: 1. Single RESPONSIBILITY PRINCIPLE: one class is responsible for one responsibility.2, the Richter replacement principle "LISKOV SUBSTITUTION PRINCIPLE": Inheritance and derivation of the rules.3, the dependency inversion principle "dependence inversion PRINCIPLE": high-level mo
deserializing.Ultimate single-Case mode F: Public class singletonf implements Serializable { Private Static class singletonholder { /** * Singleton object instance * / Static Finalsingletonf INSTANCE =NewSingletonf (); } Public StaticSingletonfgetinstance() {returnSingletonholder.instance; }/** * Private constructor is used to avoid external instantiation of objects directly using new */ Private singletonf() { }The /** * Readresolve method should be used when
give a code to implement the Registrar. The benefit of creating a singleton pattern with the registrar mechanism is that it is easy to manage and can control multiple different types of singleton instances at the same time.
Summary
1. The singleton model can be easily expanded to produce a specified number of instances.
2. In the design Patterns Java Companio
layer, they have to communicate through the interface, this layered mode in the software design with a lot of, must be paid attention to.3) In each layer, there are many small modules, a small module external should also be a whole, then a module external should also provide interfaces, other places need to use the function of this module, should be through this interface.2. Parameters and return values in the interface definition:1) The interface mu
, each additional element class needs to modify the visitor Class (also includes the visitor class subclass or the implementation Class), the modification is quite troublesome. In other words, the visitor pattern should be used with caution in cases where the number of element classes is uncertain. Therefore, the visitor pattern is suitable for the reconstruction of the existing function, for example, the basic function of a project has been determined, the element class data has been basically
The last one mainly introduced several created design patterns Abstractfactroy,factorymethod and Singliton. Their common features are used to create objects. The next thing that comes up here is a couple of structural patterns. The so-called structural pattern is used to solve the process of creating a system structure by making a reasonable and effective combina
that makes external objects not new related instances, * This particular note, be sure to provide a default privatization method to override the default constructor method otherwise, if the user goes directly to the new object * , there is no guarantee of a single case ~ ~ ~ 3. Design mode of a hungry man in the process of initializing the class will complete the initialization of the relevant instance, generally think that this way to be more
. /***/Publicclass Singleton {privatestatic New Singleton (); Private Singleton () {} Public Static synchronized Singleton Getsingleton () { return Singleton; }}The creation of a single piece in the static initializer guarantees thread safety. Of course, this approach works for applications that always create and use a single instance, or that the burden of creating and running is not too heavy.3, a relatively better way is: with "Double check lock", in Getsingleton ()
("Internet browsing information! "); } } Class Proxy implements network {//proxy Internet private network network; Public Proxy (Network network) {//Set the real operation of the agent this.network = Network;//Set the subclass of the proxy } public void Check () {//Body Verification Action System.out.println ("Check if the user is legal! "); } public void browse () { this.check ();//Invoke specific proxy business Op
; } Public void setData (String data) { this. data = data; }} 5. The Observer object (Observer) "pull" the data to get the data response. The code is as follows: Public class Implements iobserver { @Override publicvoid refresh (Isubject obj) { // TODO auto-generated method stub Subject Subject = (Subject) obj; System.out.println ("Data is being updated to:" +Subject.getdata ());} }Finally, write a test class to test: Public class Test { public
First, IntroductionThe singleton mode is one of the Java23 design patterns, and the main function of the singleton mode is to ensure that only one instance of a class is present in a Java application.Second, the characteristicsThe singleton mode has the following characteristics:1, the Singleton class can have only one instance.2, the Singleton class must create
for(inti =0; I 5; i++) {Fansobserver F =New Fansobserver("Name"+ (i +1)); Fans.Add(f); Vs.Addfans(f); }//This tweet, 5 fans have received pushVs.Publish("My girlfriend is so nice!" ");//Incredibly show love, the first 2 fans, can not look down, take off! for(inti =0; I 2; i++) {vs.Removefans(fans.)Get(i)); }//This time the Micro Bo, those two take off the person will not seeVs.Publish("I want to send all my fans an iphone X"); }}Run results大V发布微博:我的女朋友真好看!name1 收到大V的微博发
{void吃饭();void看电影();void逛街();void啪啪啪();}abstractclass恋爱中的妹子 implements 谈恋爱{@Overridepublicvoid吃饭() {// TODO Auto-generated method stub}@Overridepublicvoid看电影() {// TODO Auto-generated method stub}@Overridepublicvoid逛街() {// TODO Auto-generated method stub}@Overridepublicvoid啪啪啪() {// TODO Auto-generated method stub}}class阿乐 extends 恋爱中的妹子{publicvoid啪啪啪() {// TODO Auto-generated method stubSystem.out.println("阿乐:亚美爹,好害羞");}}}
Here I have written an interface that defines several methods
coupling between classes, improve the stability of the system, reduce the risk caused by concurrent development, and improve the readability and maintainability of the code. Let's then use the example in the JDK to illustrate:Look at the following section of code:publicclass Test { publicstaticvoidmain(String[] args) { List list=new ArrayList(); list.add("11"); System.out.println(list.get(0)); }}This code we run is definitely output 11; Next we modify the codepubliccl
Hungry man mode and the lazy mode, and according to the implementation details of the specific project, we can apply the template code to the application of the singleton design pattern.Advantages of a singleton design patternFinally, let's summarize the advantages of a singleton design pattern: can be controlled access to unique instances; The ability to redu
construction methodPrivateSingletontest () {}Defines a static private variable (uninitialized, does not use the final keyword, and volatile guarantees the visibility of the instance variable when multi-threaded access is avoided, and is called by another thread when the other variable property is not assigned when instance initialized)Privatestatic volatile singletontest instance;//defines a common static method that returns an instance of this type public Span style= "COLOR: #0000ff" >static S
, do not need to create factory objects, this pattern is more extensive, the code example is as follows: Package Com.factory.factorymethod;public class Senderfactory {public static sendable Producetextsender () { return New Textsender (); } public static sendable Producephotosender () { return to new Photosender ();} public static sendable Producevideosender ( { return new Videosender ();} } Factory Method Model Summary When there are a large number of products that need to be created and
the randomness of the thread, Maybe T1 just went in to execute if control statement, this time, was T2 grabbed the CPU execution, this time, T2 began to execute, found that this time t is still null, so T2 also go in to execute if the statement of control, then there will be more than one object is created, so lazy type has a thread safety problem.Therefore, in the development we generally choose the A hungry man-type singleton mode without the thread safety problem.What kind of singleton mode
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.