0
0
Blockchain / Solidityprogramming~10 mins

Factory pattern in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Factory pattern
Start
Call Factory
Factory creates new contract
Return new contract address
Use new contract
End
The factory pattern creates new contracts on demand and returns their addresses for use.
Execution Sample
Blockchain / Solidity
contract SimpleContract {
    uint public value;
    constructor(uint _value) { value = _value; }
}

contract Factory {
    SimpleContract[] public contracts;
    function create(uint _value) public {
        SimpleContract c = new SimpleContract(_value);
        contracts.push(c);
    }
}
This code shows a factory contract creating new SimpleContract instances with a value.
Execution Table
StepActionInputNew Contract Created?Contracts Array LengthOutput
1Call Factory.createvalue=10Yes1Address of new SimpleContract #1
2Call Factory.createvalue=20Yes2Address of new SimpleContract #2
3Call Factory.createvalue=30Yes3Address of new SimpleContract #3
4Read contracts array length--33
5Access SimpleContract #2 value---20
6Stop---No more actions
💡 Execution stops after creating contracts and reading their data.
Variable Tracker
VariableStartAfter 1After 2After 3Final
contracts.length01233
SimpleContract #1.value-10101010
SimpleContract #2.value--202020
SimpleContract #3.value---3030
Key Moments - 3 Insights
Why does the factory store contract addresses instead of contract objects?
In blockchain, contracts are separate deployed entities. The factory stores their addresses to interact with them later, as shown in execution_table rows 1-3 where new addresses are added.
How does the factory create a new contract instance?
The factory uses the 'new' keyword to deploy a new contract on the blockchain, as seen in execution_table steps 1-3 where each call creates a new contract.
What happens if we try to access a contract that was not created?
Accessing a non-existent contract would fail or return an error because its address is not stored, as the contracts array length limits access, shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the contracts array length after the second contract creation?
A1
B2
C3
D0
💡 Hint
Check the 'Contracts Array Length' column at step 2 in the execution_table.
At which step does the factory create the third SimpleContract?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'New Contract Created?' column in execution_table where it says 'Yes' for the third time.
If the factory did not store the new contract addresses, what would change in the variable_tracker?
ASimpleContract values would be accessible
Bcontracts.length would increase
Ccontracts.length would remain 0
DNo contracts would be created
💡 Hint
Refer to the 'contracts.length' row in variable_tracker and think what happens if addresses are not stored.
Concept Snapshot
Factory Pattern in Blockchain:
- Factory contract creates new contracts using 'new'
- Stores addresses of created contracts in an array
- Allows tracking and interacting with multiple contracts
- Useful for deploying many similar contracts easily
- Access contracts by their stored addresses
Full Transcript
The factory pattern in blockchain programming helps create new contracts dynamically. The factory contract has a function that deploys new contracts using the 'new' keyword and stores their addresses in an array. Each time the factory creates a contract, the array length increases. Later, you can access each contract's data by its address stored in the array. This pattern is useful when you want to manage many similar contracts from one place. The execution table shows each step of creating contracts and reading their values. The variable tracker shows how the contracts array and contract values change after each creation. Key points include understanding why addresses are stored, how contracts are created, and what happens if you try to access contracts that do not exist.