Brief introduction to the simple factory model of Java design pattern _java

Source: Internet
Author: User

The concept of a simple factory model

is to create a factory class that creates instances of classes that implement the same interface. The essence of a simple factory pattern is that a factory class dynamically determines which instance of the product class (these product classes inherit from a parent class or interface) should be created based on the parameters passed in.

UML diagrams for simple factory patterns

Simple Factory mode Code
when I was learning a simple factory model, I used a human-related example. Humans are divided into men and women in the world, first defining an abstract interface for a human product

/**
 * This is factory patter package * * Package
com.roc.factory;

/**
 * Product Abstract interface Human
 * @author liaowp
 * *
 /Public
interface Human {public
 
 void say ();

}

Then the definition of men and women also has a way of speaking.

/**
 * This is factory patter package * * Package
com.roc.factory;

/** *
 man Men * @author LIAOWP * * * * * * * * * * * * * *
implements Human {
 * * Say method * @see Com.roc.factory.human#say ()
 */
 @Override public
 Void Say () {
 System.out.println ("man");
 }

}

/**
 * This is factory patter package * * Package
com.roc.factory;

/** woman
 * @author liaowp
 */public
class Woman implements Human {/*

 say method
 * @see COM.ROC.F Actory. Human#say ()
 */
 @Override public
 Void Say () {
 System.out.println ("woman");
 }



Finally write a factory class that is used to create men and women. The first approach is achieved by using logical judgments.

Package com.roc.factory;
/** *
 Simple Factory
 * @author LIAOWP * */Public
class Samplefactory {public
 static Human Makehuman (String type) {
 if (type.equals ("Man")) {
  Human mans = New Man ();
  return mans;
 } else if (type.equals ("Womman")) {
  Human woman = new Woman ();
  return woman;
 } else{
  System.out.println ("Production does not come Out");
  return null;}}}


The second way is to use the Java radiation implementation, I think this kind of implementation is better.

Package com.roc.factory;
/**
 * Simple factory emission Implementation
 * @author LIAOWP
 * */public
class SampleFactory1 {public
 static Human Makehuman (Class c) {
 Human Human = null;
 try {
  human = (human) class.forname (C.getname ()). newinstance ();
 \ catch (instantiationexception e) {
  // TODO auto-generated Catch block
  System.out.println ("does not support abstract classes or interfaces");
  E.printstacktrace ();
 } catch (Illegalaccessexception e) {
  //TODO auto-generated catch block
  e.printstacktrace ();
  System.out.println ("Do not have sufficient permissions to access private objects");
 } catch (ClassNotFoundException e) {
  //TODO auto-generated catch block
  System.out.println ("class does not exist");
  E.printstacktrace ();
 } 
 Return human
 }
}

Finally, the client's code

Package com.roc.factory;
/**
 * Simple factory test
 * @author LIAOWP
 *
 *
/public class Client {public
 static void Main (string[] args) {
//Human man = Samplefactory.makehuman ("mans");
Man.say ();
Human Womman = Samplefactory.makehuman ("Womman");
Womman.say ();
Human test = Samplefactory.makehuman ("tttt");
 
 Human man = Samplefactory1.makehuman (Man.class);
 Man.say ();
 Human woman = Samplefactory1.makehuman (Woman.class);
 Woman.say ();
 }


Simple Factory mode application scenario

Advantage: The factory class is the key to the whole model. Contains the necessary logical judgments, depending on the information given to the outside world, to determine exactly which particular class object should be created. By using factory classes, outsiders can get rid of embarrassing situations that directly create specific product objects and simply need to be responsible for "consumption" Object is OK. Without having to worry about how these objects are created and how they are organized. Clear their respective responsibilities and rights, conducive to the optimization of the entire software architecture.

Disadvantages: because the factory class centralizes the creation logic of all instances, it violates the cohesion well-structured classes allocation principle, centralizes all the creation logic into a factory class, the classes it can create can only be considered beforehand, and if you need to add a new class, you need to change the factory class. When the specific product classes in the system are increasing, there may be a requirement that the factory class create different instances based on different conditions. The judgment of the condition and the judgment of the specific product type are staggered together, it is very difficult to avoid the spread of module function, which is very unfavorable to the maintenance and expansion of the system;

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.