Java Factory model and single example mode

Source: Internet
Author: User
Tags readline reflection
How can the code that instantiates specific classes be drawn away from the application or encapsulated so that they do not interfere with other parts of the application?
1: Simple Factory mode: its role is to instantiate an object without requiring the customer to understand that the object belongs to that particular subclass.
Using System; Using System.Collections;
The public class MyClass {public static voidmain () {//) instantiates subclasses by argument.        Ivehicle vehicle =factoryvehicle.createvehicle ("car");        Vehicle.go ();        Console.ReadLine (); }      }
public class Factoryvehicle {public static ivehiclecreatevehicle (string vehiclename) {switch (vehiclename.            ToLower ()) {case ' car ': return Newcar ();           Case "Boat": Return Newboat ();                   Default:return Newcar (); }     }
public interface Ivehicle {void Go ():} public class Car:ivehicle {public void Go () {Console.Write     Line ("Car");     } public class Boat:ivehicle {public void Go () {Console.WriteLine ("Boat"); } 2: Abstract Factory: The Factory method is also abstracted, by the specific subclass to instance the chemical plant. The Product Creation section is the same as the simple factory pattern.
Using System; Using System.Collections;
The public class MyClass {public static voidmain () {//) is instantiated by a defined factory. The disadvantage is that when a lot of products need to add a lot of factories.        Code duplication.        Factoryvehicle factory = new Factorycar (); Ivehicle vehicle =factory.        Createvehicle ();        Vehicle.go ();        Console.ReadLine (); }      }

Public interface Factoryvehicle {ivehicle createvehicle ();
}
public class Factorycar:factoryvehicle {public ivehiclecreatevehicle () {The Return of new car (); } }
public class Factoryboat:factoryvehicle {public ivehiclecreatevehicle () {The return new boat (); } }
public interface Ivehicle {void Go ();}
public class Car:ivehicle {public void Go () {Console.WriteLine (' Car '); } }
public class Boat:ivehicle {public void Go () {Console.WriteLine ("Boat"); }3: Reflection Factory Mode: In fact, the reflection of the way to get concrete instantiation is that class.
Using System; Using System.Collections; Using System.Reflection;
public class MyClass {public static voidmain () {////using reflection method to obtain instantiated class Ivehicle Vechicle =reflectfactory .        Createvehiclebyreflect ("Car");        Vechicle.go ();                Console.ReadLine (); }      }
public class Reflectfactory {public static ivehiclecreatevehiclebyreflect (string typeName) {Type type = T Ype.        GetType (TypeName); ConstructorInfo ci =type.        GetConstructor (System.Type.EmptyTypes); Return (ivehicle) CI.     Invoke (NULL);     } public class Factoryboat:factoryvehicle {public ivehiclecreatevehicle () {The return new boat (); } }
public class Factorycar:factoryvehicle {public ivehiclecreatevehicle () {The Return of new car (); } }
Public interface Factoryvehicle {ivehiclecreatevehicle ();
public interface Ivehicle {void Go ();}
public class Car:ivehicle {public void Go () {Console.WriteLine (' Car '); } }
public class Boat:ivehicle {public void Go () {Console.WriteLine ("Boat"); } }

Concept:
The single case pattern in Java is a common design pattern, the single example mode is divided into three kinds: the lazy type single case, the A Hungry man type single case, the registration type single example three kinds.

A single case pattern has some features:
1, Singleton class can only have one instance.
2. A singleton class must create its own unique instance itself.
3. A singleton class must provide this instance to all other objects.
The singleton pattern ensures that a class has only one instance and instantiates it itself and supplies the instance to the entire system. In computer systems, the driver objects of thread pools, caches, log objects, dialog boxes, printers, and graphics cards are often designed as single examples. These applications have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent two print jobs from being exported to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to avoid a single communication port being invoked simultaneously by two requests. In short, the choice of single case mode is to avoid inconsistent state, to avoid the administration of the Bulls.

Simple mode: Package singleton;
02
03
13
Class locksingleton{
Volatilestatic private Locksingleton Singleton;
Privatelocksingleton () {}
19
20//Details: http://www.ibm.com/developerworks/cn/java/j-dcl.html
Public Staticlocksingleton getinstance () {
if (singleton==null) {
Synchronized (Locksingleton.class) {
if (singleton==null) {
Singleton=new Locksingleton ();
26}
27}
28}
return singleton;
30}
31
32}
33
Enum enumsingleton{
Panax Notoginseng INSTANCE;
Voiddosomething public () {
39}
40}
41
Class internalsingleton{
private static classsingletonholder{
Private final static Internalsingleton Instance=newinternalsingleton ();
47}
Privateinternalsingleton () {}
Staticinternalsingleton getinstance () {
return singletonholder.instance;
51}
52}
53
Class hungrysingleton{
Singleton=new private Statichungrysingleton Hungrysingleton ();
Privatehungrysingleton () {}
Statichungrysingleton public getinstance () {
return singleton;
61}
62}
63
Class lazysingleton{
Private Staticlazysingleton Singleton;
Privatelazysingleton () {
69}
GetInstance public Staticlazysingleton () {
if (singleton==null) {
Singleton=new Lazysingleton ();
73}
Singleton return;
75}
76}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.