An example of a factory method pattern in Java using design pattern parsing _java

Source: Internet
Author: User

Definition of factory method pattern
the meaning of the factory method (Factory) pattern is to define a factory interface that creates product objects, deferring actual creation to subclasses. The Core factory class is no longer responsible for the creation of the product, so the core class becomes an abstract factory role that is responsible only for the interfaces that must be implemented by the specific factory subclass, so the benefit of further abstraction is that the factory method model allows the system to introduce new products without modifying the specific factory role.
It contains the following roles:

    • Abstract Products (product)
    • Specific Products (concreteproduct)
    • Abstract Factory (Factory)
    • Concrete Factory (Concretefactory)

UML Class diagrams for schemas

Instance
assume that there are three of the core, respectively, red, blue and black pen core. Users want to use ballpoint pens to clear the color of the core.
Description and use of the structure of the pattern
1. Abstract products (product): Pencore.java

Public abstract class pencore{
  String color;
  public abstract void Writeword (String s);

2. Specific products (concreteproduct) _1:redpencore.java

public class Redpencore extends pencore{
  redpencore () {
   color= "red";
  }
  public void Writeword (string s) {
    System.out.println ("Write the word +color+": "+s);
  }

Specific products (concreteproduct) _2:bluepencore.java

public class Bluepencore extends pencore{
  bluepencore () {
   color= "blue";
  }
  public void Writeword (string s) {
    System.out.println ("Write the word +color+": "+s);
  }

Specific products (concreteproduct) _3:blackpencore.java

public class Blackpencore extends pencore{
  blackpencore () {
   color= "black";
  }
  public void Writeword (string s) {
    System.out.println ("Write the word +color+": "+s);
  }

3. Constructor (Creator): Ballpen.java

Public abstract class ballpen{
  Ballpen () {
    System.out.println ("Production of one Fitted" +getpencore (). color+ "Pen-core ballpoint pen");
  }
  Public abstract Pencore Getpencore (); Factory Method
}

4. Concrete Constructor (Concretecreator):

Redballpen.java public
class Redballpen extends ballpen{public
  Pencore Getpencore () {return
    new Redpencore ();
  }
Blueballpen.java public
class Blueballpen extends ballpen{public
  Pencore Getpencore () {return
    new Bluepencore ();
  }
Blackballpen.java public
class Blackballpen extends ballpen{public
  Pencore Getpencore () {return
    new Blackpencore ();
  }

5. Application Application.java

public class application{public
  static void Main (String args[]) {
    pencore pencore;
    Ballpen ballpen=new Blueballpen ();
    Pencore=ballpen.getpencore ();
    Pencore.writeword ("Hello, nice to meet You");
    Ballpen=new Redballpen ();
    Pencore=ballpen.getpencore ();
    Pencore.writeword ("How to Are You");
    Ballpen=new Blackballpen ();
    Pencore=ballpen.getpencore ();
    Pencore.writeword ("Nice to Meet You");
  }
 

Summarize
the factory method pattern belongs to the class-creation pattern. In factory method mode, the factory parent class is responsible for defining a common interface for creating product objects. The factory subclass is responsible for generating specific product objects, so that the purpose is to defer the instantiation of the product class to the factory subclass, that is, through the factory subclass to determine which particular product class should be instantiated.
The factory method model is a further abstraction and generalization of the simple factory model. Because of the use of object-oriented polymorphism, the factory method pattern maintains the advantages of a simple factory model and overcomes its drawbacks. In the factory method model, the core factory class is no longer responsible for the creation of all the products, but rather the specific creation work is given to subclasses. This core class is only responsible for giving the interfaces that a specific factory must implement, and not for the details of the product class being instantiated, which allows the system to allow systems to introduce new products without modifying the role of the factory
The main advantage of the factory approach model is that adding new product classes requires no modification to the existing system. and encapsulates the creation details of the product object, the system has good flexibility and scalability; its disadvantage is that the addition of new products and the need for additional factories, resulting in the number of system classes to increase, to some extent, increased the complexity of the system

Related Article

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.