Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.
/*System.out.println("==========Bayonet=========="); Weapon weapon1 = new Bayonet(new Pistol()); weapon1.aimAndFire(); Weapon weapon2 = new Bayonet(new AK47()); weapon2.aimAndFire(); Weapon weapon3 = new Bayonet(new AWP()); weapon3.aimAndFire(); System.out.println("==========Sniperscope=========="); Weapon weapon4 = new Sniperscope(new Pistol()); weapon4.aimAndFire(); Weapon weapon5 = new Sniperscope(new AK47()); weapon5.aimAndFire(); Weapon weapon6 = new Sniperscope(new AWP()); weapon6.aimAndFire(); System.out.println("==========Cartridge=========="); Weapon weapon7 = new Cartridge(new Pistol()); weapon7.aimAndFire(); Weapon weapon8 = new Cartridge(new AK47()); weapon8.aimAndFire(); Weapon weapon9 = new Cartridge(new AWP()); weapon9.aimAndFire();*/
System.out.println("====================Pistol===================="); Weapon weapon1 = new Pistol(); weapon1 = new Bayonet(weapon1); weapon1 = new Sniperscope(weapon1); weapon1 = new Cartridge(weapon1); weapon1.aimAndFire(); System.out.println("====================AK47===================="); Weapon weapon2 = new AK47(); weapon2 = new Cartridge(weapon2); weapon2 = new Bayonet(weapon2); weapon2 = new Sniperscope(weapon2); weapon2.aimAndFire(); System.out.println("====================AWP===================="); Weapon weapon3 = new AWP(); weapon3 = new Sniperscope(weapon3); weapon3 = new Cartridge(weapon3); weapon3 = new Bayonet(weapon3); weapon3.aimAndFire(); } }