JAVA and mode 3rd-Abstract Factory Mode

Source: Internet
Author: User

Scenario Problems
A common example in our daily life is assembling a computer. When assembling a computer, we usually need to select a series of accessories, such as CPU, hard disk, memory, motherboard, power supply, and chassis. For the sake of simplicity, we only need to consider the choice of CPU and motherboard.

In fact, when selecting a CPU, there are a series of problems, such as the brand, model, number of pins, and clock speed. Only by determining these problems can we determine the specific CPU.

Similarly, when selecting a motherboard, there are also a series of problems, such as brand, chipset, integrated chip, bus frequency, and so on. Only these problems are identified can the specific motherboard be determined.

Choosing a different CPU and motherboard is the requirement that each customer puts forward to the installation company when assembling a computer, that is, the installation solution developed by each of us.

Before final determining the installation scheme, we also need to consider the compatibility between various accessories as a whole. For example, CPU and motherboard cannot be assembled if Intel CPU and AMD motherboard are used. Because the number of Intel CPU pins is not compatible with the CPU plug-in provided by the AMD motherboard, that is to say, if Intel's CPU is used, it cannot be inserted into the AMD motherboard, so the installation solution is holistic, the selected accessories are associated.

For the installation engineer, he only knows how to assemble a computer and the relevant accessories, but the customer has to say what kind of accessories to use. That is to say, the installation engineer is only responsible for assembly, and the customer is responsible for selecting the specific accessories required for assembly. Therefore, when the installation engineers assemble computers for different customers, they only need to obtain the corresponding accessories according to the customer's installation scheme and then assemble the computers.

Simple factory solution
Considering the customer's functions, You need to select the CPU and motherboard you need, and then tell the installation engineer their choice, and then wait for the installation engineer to assemble the computer.

For installation engineers, they only know the interfaces of CPU and motherboard, but do not know the specific implementation. Obviously, they can use the simple factory mode or factory method mode. For simplicity, a simple factory is used here. The customer informs the installation engineer of his or her choice, and then the installation engineer obtains the corresponding instance object through the corresponding factory.

 

Source code
CPU interface and implementation

 

[Java]
Package com. bankht. abstractFactory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:39:35
*
* @ Class description: Cpu interface class
*/
Public interface Cpu {
Public void calculate ();
}
Package com. bankht. abstractFactory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:39:35
*
* @ Class description: Cpu interface class
*/
Public interface Cpu {
Public void calculate ();
}

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:45:34
*
* @ Class description: IntelCpu
*/
Public class IntelCpu implements Cpu {
/**
* Number of CPU pins
*/
Private int pins = 0;
 
Public IntelCpu (int pins ){
This. pins = pins;
}
 
@ Override
Public void calculate (){
System. out. println ("Intel CPU pins:" + pins );
}
 
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:45:34
*
* @ Class description: IntelCpu
*/
Public class IntelCpu implements Cpu {
/**
* Number of CPU pins
*/
Private int pins = 0;

Public IntelCpu (int pins ){
This. pins = pins;
}

@ Override
Public void calculate (){
System. out. println ("Intel CPU pins:" + pins );
}

}

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:53:12
*
* @ Class description: AmdCpu
*/
Public class AmdCpu implements Cpu {
/**
* Number of CPU pins
*/
Private int pins = 0;
 
Public AmdCpu (int pins ){
This. pins = pins;
}
 
@ Override
Public void calculate (){
// TODO Auto-generated method stub
System. out. println ("amd cpu pins:" + pins );
}
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:53:12
*
* @ Class description: AmdCpu
*/
Public class AmdCpu implements Cpu {
/**
* Number of CPU pins
*/
Private int pins = 0;

Public AmdCpu (int pins ){
This. pins = pins;
}

@ Override
Public void calculate (){
// TODO Auto-generated method stub
System. out. println ("amd cpu pins:" + pins );
}
}

Motherboard interface and implementation

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:53:57
*
* @ Class description: main board interface
*/
Public interface Mainboard {
Public void installCPU ();
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:53:57
*
* @ Class description: main board interface
*/
Public interface Mainboard {
Public void installCPU ();
}
 

 

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:56:02
*
* @ Class description: Intel motherboard class
*/
Public class IntelMainboard implements Mainboard {
/**
* Number of holes in the CPU slot
*/
Private int cpuHoles = 0;
 
/**
* Constructor: number of holes in the CPU slot
*
* @ Param cpuHoles
*/
Public IntelMainboard (int cpuHoles ){
This. cpuHoles = cpuHoles;
}
 
@ Override
Public void installCPU (){
// TODO Auto-generated method stub
System. out. println ("the number of CPU slot holes on the Intel motherboard is:" + cpuHoles );
}
 
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:56:02
*
* @ Class description: Intel motherboard class
*/
Public class IntelMainboard implements Mainboard {
/**
* Number of holes in the CPU slot
*/
Private int cpuHoles = 0;

/**
* Constructor: number of holes in the CPU slot
*
* @ Param cpuHoles
*/
Public IntelMainboard (int cpuHoles ){
This. cpuHoles = cpuHoles;
}

@ Override
Public void installCPU (){
// TODO Auto-generated method stub
System. out. println ("the number of CPU slot holes on the Intel motherboard is:" + cpuHoles );
}

}

 

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:56:51
*
* @ Class description: Amd motherboard class
*/
Public class AmdMainboard implements Mainboard {
/**
* Number of holes in the CPU slot
*/
Private int cpuHoles = 0;
 
/**
* Constructor: number of holes in the CPU slot
*
* @ Param cpuHoles
*/
Public AmdMainboard (int cpuHoles ){
This. cpuHoles = cpuHoles;
}
 
@ Override
Public void installCPU (){
// TODO Auto-generated method stub
System. out. println ("the number of CPU slot holes on the AMD motherboard is:" + cpuHoles );
}
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:56:51
*
* @ Class description: Amd motherboard class
*/
Public class AmdMainboard implements Mainboard {
/**
* Number of holes in the CPU slot
*/
Private int cpuHoles = 0;

/**
* Constructor: number of holes in the CPU slot
*
* @ Param cpuHoles
*/
Public AmdMainboard (int cpuHoles ){
This. cpuHoles = cpuHoles;
}

@ Override
Public void installCPU (){
// TODO Auto-generated method stub
System. out. println ("the number of CPU slot holes on the AMD motherboard is:" + cpuHoles );
}
}

CPU and motherboard Factory

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:57:56
*
* @ Class description: Cpu factory class
*/
Public class CpuFactory {
Public static Cpu createCpu (int type ){
Cpu cpu = null;
If (type = 1 ){
Cpu = new IntelCpu (755 );
} Else if (type = 2 ){
Cpu = new AmdCpu (938 );
}
Return cpu;
}
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:57:56
*
* @ Class description: Cpu factory class
*/
Public class CpuFactory {
Public static Cpu createCpu (int type ){
Cpu cpu = null;
If (type = 1 ){
Cpu = new IntelCpu (755 );
} Else if (type = 2 ){
Cpu = new AmdCpu (938 );
}
Return cpu;
}
}
 

 

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:58:27
*
* @ Class description: main board factory type
*/
Public class MainboardFactory {
Public static Mainboard createMainboard (int type ){
Mainboard mainboard = null;
If (type = 1 ){
Mainboard = new IntelMainboard (755 );
} Else if (type = 2 ){
Mainboard = new AmdMainboard (938 );
}
Return mainboard;
}
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:58:27
*
* @ Class description: main board factory type
*/
Public class MainboardFactory {
Public static Mainboard createMainboard (int type ){
Mainboard mainboard = null;
If (type = 1 ){
Mainboard = new IntelMainboard (755 );
} Else if (type = 2 ){
Mainboard = new AmdMainboard (938 );
}
Return mainboard;
}
}

The running results of installation engineers and customers are as follows:

[Java]
Package com. bankht. abstractFactory;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:59:14
*
* @ Class description: installation engineer
*/
Public class ComputerEngineer {
/**
* Define the CPU required for assembly
*/
Private Cpu cpu = null;
/**
* Define the motherboard required for assembly
*/
Private Mainboard mainboard = null;
 
Public void makeComputer (int cpuType, int mainboard ){
/**
* Basic steps for machine assembly
*/
// 1: first, prepare the accessories required for installation.
PrepareHardwares (cpuType, mainboard );
// 2: assemble the machine
// 3: test the machine
// 4: Delivery customer
}
 
Private void prepareHardwares (int cpuType, int mainboard ){
// Here we need to prepare the specific implementation of the CPU and motherboard. For the sake of simple examples, we only need to prepare the two
// However, the installation engineer does not know how to create it. What should I do?
 
// Directly find the corresponding factory
This. cpu = CpuFactory. createCpu (cpuType );
This. mainboard = MainboardFactory. createMainboard (mainboard );
 
// Test whether the accessories are easy to use
This. cpu. calculate ();
This. mainboard. installCPU ();
}
}
Package com. bankht. abstractFactory;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 04:59:14
*
* @ Class description: installation engineer
*/
Public class ComputerEngineer {
/**
* Define the CPU required for assembly
*/
Private Cpu cpu = null;
/**
* Define the motherboard required for assembly
*/
Private Mainboard mainboard = null;

Public void makeComputer (int cpuType, int mainboard ){
/**
* Basic steps for machine assembly
*/
// 1: first, prepare the accessories required for installation.
PrepareHardwares (cpuType, mainboard );
// 2: assemble the machine
// 3: test the machine
// 4: Delivery customer
}

Private void prepareHardwares (int cpuType, int mainboard ){
// Here we need to prepare the specific implementation of the CPU and motherboard. For the sake of simple examples, we only need to prepare the two
// However, the installation engineer does not know how to create it. What should I do?

// Directly find the corresponding factory
This. cpu = CpuFactory. createCpu (cpuType );
This. mainboard = MainboardFactory. createMainboard (mainboard );

// Test whether the accessories are easy to use
This. cpu. calculate ();
This. mainboard. installCPU ();
}
}
 

 

[Java]
Package com. bankht. abstractFactory;
 
Import org. junit. Test;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 05:01:04
*
* @ Class description: Customer test class
*/
Public class Client {
 
@ Test
Public void test (){
ComputerEngineer cf = new ComputerEngineer ();
Cf. makeComputer (1, 1 );
}
}
Package com. bankht. abstractFactory;

Import org. junit. Test;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 05:01:04
*
* @ Class description: Customer test class
*/
Public class Client {

@ Test
Public void test (){
ComputerEngineer cf = new ComputerEngineer ();
Cf. makeComputer (1, 1 );
}
}

The running result is as follows:

 
 

 

Author: m13666425773

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.