Abstract Factory mode in JAVA and 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

public interface Cpu {
public void calculate();
}
Public class IntelCpu implements Cpu {
/**
* Number of CPU pins
*/
Private int pins = 0;
Public IntelCpu (int pins ){
This. pins = pins;
}
@ Override
Public void calculate (){
// TODO Auto-generated method stub
System. out. println ("Intel CPU pins:" + pins );
}

}
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

public interface Mainboard {
public void installCPU();
}
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 );
}

}
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

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;
}
}
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:

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 ();
}
}

 

public class Client {
public static void main(String[]args){
ComputerEngineer cf = new ComputerEngineer();
cf.makeComputer(1,1);
}
}

The running result is as follows:


The above implementation, although solved through a simple factory method: For installation engineers, only the CPU and motherboard interfaces are known, but not the specific implementation problems.But there is another problem that cannot be solved, that is, these CPU objects and the main board objects are actually related and need to match each other.In the above implementation, this association is not maintained, and the CPU and motherboard are chosen by the customer at will, which is problematic. For example, when the client calls makeComputer, the input parameter is (1, 2) and the running result is as follows:

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.