Applications based on blockchain might need to use multiple instances of a standard contract with customizations.
Each contract instance is created by instantiating a contract template.
For example, in a business process management system, each of the business process instances might
be represented by a smart contract being generated from a contract template representing the business
process model.
The template can be stored off chain in a code repository or on chain within its own smart contract.
Keeping the contract template off chain cannot guarantee consistency between different smart contract
instances created from the same template.
Because the source code of the template can be independently modified, Factory contract pattern provides
the solution for the same where we maintain an on chain template contract which is used as a factory
to generate contract instances from the template.
Some of the forces which led to the creation of factory contract pattern are related to dependency management.
Storing the source code of smart contract off chain in a code repository introduces the issue of integrating
more systems into the blockchain based application.
So we want to create a solution where we are not dependent upon external services.
Another force which led to this design pattern is secure code sharing Blockchain can be used as a secure
platform to share code of smart contracts as opposed to a traditional code repository.
Changes of a code deployed on a smart contract can be strictly limited or prohibited.
Another force, which led to the factory contract pattern is about deployment.
If a public code repository like GitHub is used to store the source code of the Smart contract, a component
is needed to implement the function of deploying smart contract on blockchain.
Otherwise, the end users need to understand how to deploy smart contract by sending transactions with
the customized source code of the contract definition and factory contract pattern avoids such a scenario.
The solution provided by factory contract pattern is related to creation of smart contracts from a contract
factory deployed on blockchain.
The factory contract is deployed once from the off chain source code.
The factory may contain the definition of multiple smart contracts.
Smart contract instances are generated by passing parameters to the contract factory to instantiate
customized smart contract instances.
Factory contract is analogous to a class in an object oriented programming language.
Every transaction that generates a smart contract instance essentially instantiates an object of the
factory contract class.
This contract instance will maintain its own properties independently of the other instances, but with
a structure consistent with its original template.
Some of the known benefits for factory contract pattern are related to security.
Keeping the factory contract on chain guarantees the consistency of the contract definition.
Also, we don't have to interact with off chain services.
Another benefit which we get is efficiency.
If the contract definition is kept on chain in a factory contract, smart contract instances are generated
by calling a function defined in the factory contract instead of going through a number of steps if
the factory contract is carried out off chain.
There are also some drawbacks related to factory contract pattern.
One of the important drawback is about cost.
If a public blockchain is used, using factory contract requires extra cost to deploy the factory contract.
Another drawback is related to function call cost.
If a public blockchain is used, creating a new smart contract instance require extra cost to call a
function defined in the factory contract.
Some of the known implementations for factory contract pattern is with Azure.
Azure is a service over Ethereum blockchain to define marketplace for predictions.
Predictions could be related to voting weather or any game which you want to place in or your marketplace.
Azure creates two Erc20 tokens every time a new market is created through the factory contract pattern.
In this image, you can see the factory contract pattern in action.
As you can see, we have maintained on chain and off chain services here.
On chain are the one which are present over the blockchain and off chain are the one which are present
externally to the blockchain.
In the on chain services.
We have a factory contract which has the contract definition.
This acts as a template for all the future contracts which are going to be generated from the factory
contract.
If we want to generate a new contract, we only need to pass in the parameters to the function within
the factory contract and a new instance of contract is generated which we can use for performing different
operations.
This factory contract is present on chain, so we don't need to go externally to call any service to
create the instance from a template.
The source code of the factory contract is present off chain where we use the source code to deploy
it once over the blockchain, and from there we can keep on instantiating different smart contracts
as per the parameters which we are passing to the factory contract.
Another important design pattern under contractual structural patterns is incentive execution.
Now let's go and learn in detail about incentive execution design pattern.