Design Pattern _ factory pattern

Source: Internet
Author: User

Design Pattern _ factory pattern
Define an interface for creating on object, but let subclasses decide which class to instantiate Factory Method lets a class defer instantiation to subclasses. defines an interface used to create objects, so that the subclass determines which class to instantiate.
Abstract Product: Product abstraction and Generalization

Abstract creation class Creator: Based on the abstraction layer, only the abstraction layer is concerned, not the subclass. Similar to Interface Programming

 

Public abstract class Product {// public method of the Product class public void method1 () {// business logic processing} // abstract method public abstract void method2 ();}
Public abstract class Creator {/** creates a product object. The input parameter type can be set by yourself * usually String, Enum, Class, and so on. Of course, it can also be blank */public abstract
 
  
T createProduct (Class
  
   
C );}
  
 
Public class ConcreteCreator extends Creator {public
 
  
T createProduct (Class
  
   
C) {Product product = null; try {product = (Product) Class. forName (c. getName ()). newInstance ();} catch (Exception e) {// Exception Handling} return (T) product ;}}
  
 


Example: humans are generally divided into white, black, and yellow people. These three kinds of people are collectively referred to as Human, abstracted as interface Human, Human skin color, language, and culture, currently, there are two public business methods: talk () and getColoe (). On this basis, the product abstract class has been conceived. The factory abstract class is here, using reflection and polymorphism, generic technology is designed for interface programming.

 

Ad

 

public interface Human { public void getColor(); public void talk();}
Public class WhiteMan implements Human {public void getColor () {System. out. println (white);} public void talk () {System. out. println (English );}}
public abstract class AbstractFactory { public abstract 
 
   T createHuman(Class
  
    c);}
  
 
Public class HumanFactory extends AbstractFactory {@ Override public
 
  
T createHuman (Class
  
   
C) {Human human = null; try {human = (Human) Class. forName (c. getName ()). newInstance ();} catch (Exception e) {System. out. println (producer failed);} return (T) human ;}}
  
 

 

 

 public static void main(String[] args) {  AbstractFactory factory=new HumanFactory();  WhiteMan whiteMan= factory.createHuman(WhiteMan.class);  whiteMan.getColor();  whiteMan.talk(); }

 

Advantages: good scalability, interface-oriented programming, embracing changes, and clear code structure.
Core Idea: summarize a type of product, in which the public services, those differences, those same, write abstract interfaces based on these, and then the extension becomes very simple.
Extension 1: replace Singleton mode extension 2: copywriting JDBC connection pool (I am confused)

 

Public class SingletonFactory {private static Emperor emperor; // refer to the previous blog static {try {Class cl = Class. forName (Emperor. class. getName (); // obtain the class object Constructor constructor = cl. getDeclaredConstructor (); // obtain the constructor. setAccessible (true); // set the constructor to be accessible. newInstance (); // instantiate} catch (Exception e) {e. printStackTrace () ;}} public static Emperor getEmperor () {return emperor ;}}

Public class JDBCPool {private static final Map
 
  
PrMap = new HashMap (); private final static int MaxConections = 5; // maximum number of connections public static synchronized Connection createConnection (String connName) {Connection con = null; if (prMap. size ()
  
   

 

 

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.