Factory Method)

Source: Internet
Author: User

This article will introduce some usage of Factory Method in java design pattern to Java hobbies in detail, which will be helpful to you.

Type:Object Creation Mode

Intent:Defines an interface used to create objects, so that the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to its subclass.

Applicability:

  • When a class does not know the class of the object it must create.
  • When a class wants its subclass to specify the object it creates.
  • When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.

Structure:

Participants:

Product (Document)
-Define the interface of the object created by the factory method.
• ConcreteProduct (MyDocument)
-Implement the Product interface.
• Creator (Application)
-Declare the factory method. This method returns an object of the Product type. Creator can also define a factory
It returns a default ConcreteProduct object.
-You can call the factory method to create a Product object.
• ConcreteCreator (MyApplication)
-Redefines the factory method to return a ConcreteProduct instance.

Example

Note: In the actual code, I added several Account interfaces. Factory method: getAccount ()

The Code is as follows: Copy code

Account. java

Package com. raysmond. factorymethod;

Public interface Account {
// Define the interface of the object 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.