contributte / planette-site

💀 [DISCONTINUED] All the roads go through the Planette
https://planette.vercel.app
MIT License
9 stars 3 forks source link

2013-04-24: dedicnost-vs-kompozice #28

Open paveljanda opened 7 years ago

paveljanda commented 7 years ago

Dovolil bych si na krátké ukázce kódu srovnat klasické dědění tříd a kompozici, za využití Nette\ComponentModel. Ukázky kódu nejsou identické, jde jen o demonstraci síly kompozice.

Dědičnost

class PersonForm extends Nette\Application\UI\Form
{
        public function __construct()
        {
                parent::__construct();

                $this->addText('name', 'Jméno');
                $this->addSubmit('submit', 'Odeslat');
        }
}

class PersonWithAddressForm extends PersonForm
{
        public function __construct()
        {
                parent::__construct();

                $this->addText('city', 'Město');
                $this->addText('street', 'Ulice');
        }
}

Kompozice

class PersonContainer extends Nette\Forms\Container
{
        public function __construct()
        {
                parent::__construct();

                $this->addText('name', 'Jméno');
                $this->addText('surname', 'Příjmení');
        }
}

class AddressContainer extends Nette\Forms\Container
{
        public function __construct()
        {
                parent::__construct();

                $this->addText('city', 'Město');
                $this->addText('street', 'Ulice');
        }
}

class MyPresenter extends BasePresenter
{
        protected function createComponentPersonWithAddressForm()
        {
                $form = new Nette\Application\UI\Form;
                $form['user'] = new PersonContainer();
                $form['user']['address'] = new AddressContainer();
                $form['user']['correspondence'] = new AddressContainer();
                $form['user']['billing'] = new AddressContainer();

                $form->addSubmit('submit', 'Odeslat');
                $form->onSuccess[] = function () {};

                return $form;
        }
}
paveljanda commented 7 years ago

author: 2118 ()