0
0
Blockchain / Solidityprogramming~10 mins

Why design patterns improve quality in Blockchain / Solidity - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a reusable contract template.

Blockchain / Solidity
contract [1] {
    // common functions here
}
Drag options to blanks, or click blank then click option'
ATemplate
BMyContract
CMain
DToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using specific contract names instead of a generic template.
2fill in blank
medium

Complete the code to implement a singleton pattern in a smart contract.

Blockchain / Solidity
contract Singleton {
    address private staticInstance;

    function getInstance() public view returns (address) {
        return [1];
    }
}
Drag options to blanks, or click blank then click option'
Amsg.sender
BstaticInstance
Cowner
Daddress(this)
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the caller's address instead of the stored instance.
3fill in blank
hard

Fix the error in the code to properly use the factory pattern for contract creation.

Blockchain / Solidity
contract Factory {
    function create() public returns (address) {
        [1] newContract = new [1]();
        return address(newContract);
    }
}

contract Product {}
Drag options to blanks, or click blank then click option'
AContract
BFactory
CInstance
DProduct
Attempts:
3 left
💡 Hint
Common Mistakes
Using the factory contract name as the new contract type.
4fill in blank
hard

Fill both blanks to implement an observer pattern in Solidity.

Blockchain / Solidity
contract Subject {
    address[] public observers;

    function [1](address observer) public {
        observers.[2](observer);
    }
}
Drag options to blanks, or click blank then click option'
AregisterObserver
Badd
Cpush
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function or array method names.
5fill in blank
hard

Fill all three blanks to complete a decorator pattern example in Solidity.

Blockchain / Solidity
contract Base {
    function getValue() public pure virtual returns (uint) {
        return [1];
    }
}

contract Decorator is Base {
    Base base;
    constructor(Base _base) {
        base = _base;
    }
    function getValue() public view override returns (uint) {
        return base.getValue() [2] [3];
    }
}
Drag options to blanks, or click blank then click option'
A10
B+
C5
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition.
Returning wrong base value.