Java Design Pattern Factory Approach (Factory method)

Source: Internet
Author: User

Type: Object-Creation pattern

Intent: defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method causes the instantiation of a class to be deferred to its subclasses.

Applicability:

    • When a class does not know the class of the object it must create.
    • When a class wants to specify the object it creates by its subclass.
    • When a class delegates the responsibility of creating an object to one of several help subclasses, and you want the information to be localized for which helper subclasses are proxies.

Structure:

Participants:

Product (Document)
-Defines an interface for objects created by the factory method.
concreteproduct (MyDocument)
-Implements the product interface.
Creator (application)
-Declares a factory method that returns an object of type product. Creator can also define a factory side
The default implementation of the method, which returns a default Concreteproduct object.
-You can call the factory method to create a product object.
Concretecreator (MyApplication)
-Redefine the factory method to return a concreteproduct instance.

Example

Note: In the actual code, I have added several account interfaces. Factory method is: Getaccount ()

The code is as follows Copy Code

Account.java

Package Com.raysmond.factorymethod;

Public interface Account {
Interface to define objects created by the factory method
Public Roletype getrole ();
public void Setaccountid (Integer accountid);
Public Integer Getaccountid ();
public void Setaccountname (String name);
Public String getaccountname ();
}
User.java

Package Com.raysmond.factorymethod;

public class User implements account{
Private Integer userId;
Private String UserName;

@Override
Public Roletype Getrole () {
return roletype.user;
}

@Override
public void Setaccountid (Integer accountid) {
This.userid = AccountId;
}

@Override
Public Integer Getaccountid () {
return userId;
}

@Override
public void Setaccountname (String name) {
This.username = name;
}

@Override
Public String Getaccountname () {
return userName;
}


}
Admin.java

Package Com.raysmond.factorymethod;

public class Admin implements account{
Private Integer Adminid;
Private String AdminName;

@Override
Public Roletype Getrole () {
return roletype.admin;
}

@Override
public void Setaccountid (Integer accountid) {
This.adminid = AccountId;
}

@Override
Public Integer Getaccountid () {
return Adminid;
}

@Override
public void Setaccountname (String name) {
This.adminname = name;
}
@Override
Public String Getaccountname () {
return adminname;
}

}
Accountfactory.java

Package Com.raysmond.factorymethod;

Public interface Accountfactory {
Account Getaccount ();
}
Userfactory.java

Package Com.raysmond.factorymethod;

public class Userfactory implements accountfactory{

@Override
Public account Getaccount () {
return new User ();
}

}
Adminfactory.java

Package Com.raysmond.factorymethod;

public class Adminfactory implements accountfactory{

@Override
Public account Getaccount () {
return new Admin ();
}

}
Roletype.java

Package Com.raysmond.factorymethod;

public enum Roletype {
Admin,user
}
Test.java

Package Com.raysmond.factorymethod;

public class Test {
public static void Main (string[] args) {
Accountfactory userfactory = new Userfactory ();
User user = (user) userfactory.getaccount ();

Accountfactory adminfactory = new Adminfactory ();
Admin admin = (admin) adminfactory.getaccount ();

System.out.println (User.getrole ());
System.out.println (Admin.getrole ());
}
}

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.