1

Topic: Assignment of properties and call of the virtual method from the designer

Here a century live, a century study. It appears in the majority of OOP of languages (Java, C#, TypeScript) there is assignment possibility  to a call  no super class, and a call of the virtual method from  - the bad practice. We consider the following code: class AButton {constructor () {this.fill ();} protected fill () {//it is supposed that it builds the button on a basis getColor ()//generally, the code of this method could contain quite and in the designer} protected abstract getColor (): string;} class ButtonWithCustomColor extends AButton {protected color: string; constructor (color: string) {this.color = color;//we Receive an error here - access to properties still is not present super ();} protected getColor () {return this.color;} } Does not quit  a floret. It is necessary to cause each time not only the designer, but also a method fill () from the outside (and from its designer we throw out), than completely we kill sense of concealment of guts in object. Was: btn = new Button (); Became: btn = new Button (); btn.fill ();//do not allow to you to forget god it to cause btn.fillSomethingElse ();//and at us for certain and others   outside sooner or later That the most interesting, so it that anything as a matter of fact does not change, except the notation. Nothing hinders the compiler to give access at least to  object to a call of the designer of a super class. Somebody can explain to me, what best practice for similar cases? And why the simple logical decision is replaced by the bad illogical?

2

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: Here it is possible to esteem with all links etc.

3

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> Here a century live, a century study. S> It appears in the majority of OOP of languages (Java, C#, TypeScript) there is assignment possibility  to a call  no super class, and a call of the virtual method from  - the bad practice. Well if it is resolved, actually not so and bad But it is necessary to consider singularities. In Java, C#, and one of the first in the Object Pascal () was so that the designer of the ancestor by a call of the virtual method actually causes a method of the descendant (the one whom told to construct). In a C ++, on the contrary, methods of the class and ancestors are accessible to it only, but descendants - still are not present. It reduces functionality, but raises safety. S> became: S> btn = new Button (); S> btn.fill ();//do not allow to you to forget god it to cause S> btn.fillSomethingElse (); //and at us for certain and others   outside sooner or later There where it is possible to cause the designer in the form of the explicit designer, it is possible to cause and something like public static Button make (), which itself causes the designer (probably, declared private not to show not complete state), causes the necessary methods and returns ready result. To such itself the designer who is not looking syntactically as the designer. (In native library Java there is a set of methods with not intuitive title valueOf () which do something similar.) After that specificity of giblets will be hidden only in the given class and its descendants who should reflect specificity of such formation at itself, but not at users. In my opinion, it quite the decision which covers the overwhelming majority of such situations. S> that the most interesting, so it that anything as a matter of fact does not change, except the notation. S> nothing hinders the compiler to give access at least to  object to a call of the designer of a super class. S> Somebody can explain to me, what best practice for similar cases? And why the simple logical decision is replaced by the bad illogical? That's just the point that "simple logical" here is not present generally. There are 2-3 extreme variants, everyone with the cockroaches, and attempt of a choice of the compromise in between. It is possible as in a C ++ - it is begun always with the ancestor, the ancestor knows nothing about descendants. It is possible as in Java, C# - it is begun always with the ancestor, but through the virtual methods we can receive the dosed knowledge of descendants, plus protective khaki like preliminary initialization of fields (see the link from Sharov). It does not allow to come into in advance the coordinated fortune of the descendant as a whole, but allows to do hardly improvings. It is possible as in Python - where and as the designer of the ancestor - purely on conscience of the programmer is caused, protection is only at level of integrity of the environment of execution. There are still variants?

4

Re: Assignment of properties and call of the virtual method from the designer

Hello, netch80, you wrote: N> There where it is possible to cause the designer in the form of the explicit designer, it is possible to cause and something like public static Button make (), which itself causes the designer (probably, declared private not to show not complete state), causes the necessary methods and returns ready result. To such itself the designer who is not looking syntactically as the designer. (In native library Java there is a set of methods with not intuitive title valueOf () which do something similar.) after that specificity of giblets will be hidden only in the given class and its descendants who should reflect specificity of such formation at itself, but not at users. And on mine it is called Factory? Not? Well in that sense that the designer at us private, and in any way to create object, except as this factory method does not quit. And he   incurs besides creation also all necessary additional initialization...

5

Re: Assignment of properties and call of the virtual method from the designer

Hello, Carc, you wrote: a C> Hello, netch80, you wrote: N>> There where it is possible to cause the designer in the form of the explicit designer, it is possible to cause and something like public static Button make (), which itself causes the designer (probably, declared private not to show not complete state), causes the necessary methods and returns ready result. To such itself the designer who is not looking syntactically as the designer. (In native library Java there is a set of methods with not intuitive title valueOf () which do something similar.) After that specificity of giblets will be hidden only in the given class and its descendants who should reflect specificity of such formation at itself, but not at users. The C> And on mine it is called Factory? Not? A C> Well in that sense that the designer at us private, and in any way to create object, except as this factory method does not quit. And he   incurs besides creation also all necessary additional initialization... It seems that the factory here a good method to hide implementation, because in case of such hierarchy: Button <CustomColorButton <CustomColorButtonWithSomeOtherFeature CustomColorButtonWithSomeOtherFeature would receive two static methods create (), one of which to it as to a goat . But there is a nuance. If we want to give to the programmer (or 3-rd parties) to be inherited from Button to us is mandatory it is necessary to it  that he did not forget to cause fill () after creation of the button. And it is grief to a certain extent because is mandatory it wants to programmers  for case Button <ToolbarButton that it is banal at once for all buttons to specify height and width.

6

Re: Assignment of properties and call of the virtual method from the designer

Hello, Sharov, you wrote: S> Hello, sharez, you wrote: S> Here it is possible to esteem with all links etc.> When you combine these two facts you are left with the problem that if you make a virtual method call in a constructor, and it is not the most derived type in its inheritance hierarchy, that it will be called on a class whose constructor has not been run, and therefore may not be in a suitable state to have that method called. Just it also is a problem that the successor has possibility to redefine behavior of object, but has no possibility it is behavior to adjust (to specify variables/fields/arguments) to its call. Differently in the designer there is a taboo on a call of the virtual methods since we do not have possibility to transfer to them any optional data necessary for successors (at least through fields). I cannot understand in any way, what for generally compilers are implemented so that have no possibility to process the following construction: class Child extends Parent {protected data: SomeObject; constructor (data: SomeObject) {this.data = data;//we specify new adjustment of object super ();//also we cause construction}//it will be caused during time  designer Parent public init () {super.init (data.x);//and old implementation corrected useAnotherFeature (data.y);//and a new functional added}} after all with such construction at all there would be no problems if it was supported, unless are not present?

7

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> S> class Child extends Parent {S> protected data: SomeObject; S> constructor (data: SomeObject) {S> this.data = data;//we specify new adjustment of object S> super ();//also we cause construction S>} S>//it will be caused during time  designer Parent S> public init () {S> super.init (data.x);//and old implementation corrected S> useAnotherFeature (data.y);//and a new functional added S>} S>} S> S> after all with such construction at all there would be no problems if it was supported, unless are not present? . Idea that reversal to this before creation of parent object not . In my opinion, quite reasonably and safely.

8

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> But there is a nuance. If we want to give to the programmer (or 3-rd parties) to be inherited from Button to us is mandatory it is necessary to it  that he did not forget to cause fill () after creation of the button. And it is grief to a certain extent because is mandatory it wants to programmers  for case Button <ToolbarButton that it is banal at once for all buttons to specify height and width. And if to try  what-thread NVI-pattern? I.e. type to make in the upper base class what-thread ShowButton which shows the button. And to make its not virtual for all the same there an unpretentious functional in style to show \hide, and nobody will superimpose it all the same, for not what for. And here it non virtual ShowButton, will already cause that virtual fill which requests attributes (colors there or that there was). I.e. something of a type//somewhere in CButton is a method virtual virtual void fill (); //or GetColor - that there there was that? CButton:: ShowButton (BOOL bShow) {if (bShow)//if we show the button fill ();//that the virtual method which all at first requests CWidget:: ShowButton (bShow) is caused inside;//well and it is any unpretentious thing: to show to hide the button} I.e. thus we explicitly we provoke the subsequent coders (developers of successors from CButton) to cause not virtual method of show of the button. And here it already causes necessary virtual fill since actually the object by the time of call ShowButton already is completely created, all designers of all hierarchy fulfilled. A crutch certainly, but at least the explicit. Without ShowButton anything is not present in UI it (is not visible), to call mandatory. And here it already completes all unobvious initialization.

9

Re: Assignment of properties and call of the virtual method from the designer

Hello, Carc, you wrote: the C> And if to try  what-thread NVI-pattern? A C> I.e. type to make in the upper base class what-thread ShowButton which shows the button. And to make its not virtual for all the same there an unpretentious functional in style to show \hide, and nobody will superimpose it all the same, for not what for. The C> And here it non virtual ShowButton, will already cause that virtual fill which requests attributes (colors there or that there was). And I about such Lazy Initialization also made. As a matter of fact that the button to add in DOM-tree HTML it is necessary to fulfill element.appendChild (button.getElement ()); Actually in getElement () I also cause fill () if it it has not been still made. However, as always here again there is a nuance. There are methods setEnabled (bool), setTitle (string) and etc. which now instead of simple modification of an element, should check still that it generally has been created, i.e. at least: public setTitle (title: string) {if (this.el == null) this.fill (); this.el.textContent = title;} and so in everything addressing to an element methods. It is possible, of course, to force to address all methods to this.el on a method this.getElement (), except this.fill: public setTitle (title: string) {this.getElement ().textContent = title;} For this purpose it is necessary to make this.el private and to transfer it in fill () as parameter. Plus should to be forgotten then at  base class that this el should be used only through getElement () though possibility such is. With lengthening of a chain of inheritance (Button <ButtonWithPrivateEl) we do not consider a variant because of getting out of hand over-engineering'a. In general, all it becomes, yes, but whether it seems, what we unduly complicated architecture simply because of far-fetched restriction on the job of fields of a class to a call ?

10

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> Here a century live, a century study. It is a bayan. S> Somebody can explain to me, what best practice for similar cases? It is necessary to do hierarchy redesign, to use RAII and to apply pair of templates of designing. S> and why the simple logical decision is replaced by the bad illogical? A problem there only that fields were not initialized. If you very much would like, it is possible to transfer all through parameters of the virtual function and accurately to supervise  class fields. A question in other, what for then OOP and all its ecosystem of classes if it is necessary to bypass the same system?

11

Re: Assignment of properties and call of the virtual method from the designer

Hello, Sharov, you wrote: S> Osn. Idea that reversal to this before creation of parent object not . In my opinion, quite reasonably and safely. Personally I see here disagreements that the designer should do, and it should: 1) to Select storage (it is such atavism from which roots) 2 grow) to Set properties and as it technically is also function 3) to Initialize itself but this step for some reason  between 1 and 2 for the inexplicable reason. I will often come across on the Internet that it is not necessary to use property of the designer-function, i.e. to use (1,2) and not to use (3). I think that selection of storage it as though preconstruction of object which generally should be carried out for limits of controllable process to Java, and logically these  should be grouped here so (1) - (2, 3) or (1) - (3, 2) where the step (1) generally is hidden from us, and is caused on a chain separately from the designer. We in modern Java tend to splice absolutely heterogeneous processes (1,2) only for the sake of the correct selection of storage. Here is how should be: when we cause  it causes on a chain all predesigners (without any parameters) simply for storage selection under all fields of all ancestors, and already then we operate with the designer as actually virtual function, we cause its ancestors as that we want (or not to cause  if we do not want). Almost such now it is impossible, but unless it does not pull on  continuation of the concept of OOP? I am done not abandoned by thought that developers of compilers all the same could be guided by something, without implementing such obvious thing. But how many I do not outline in mind various tasks, and I will not come across the answer, than.

12

Re: Assignment of properties and call of the virtual method from the designer

Hello, Kernan, you wrote: K> Hello, sharez, you wrote: S>> Somebody can explain to me, what best practice for similar cases? K> it is necessary to do hierarchy redesign, to use RAII and to apply pair of templates of designing. It is alternative to the simple decision. K> a problem there only that fields were not initialized. I above offered the decision with sharing of the designer on the predesigner without parameters (it is uncontested implicitly caused on a line-up at the very beginning of the designer) and  function (which can cause or not cause virtual-functions-designers ancestors in any order). But it at language level. K> if you very much would like, it is possible to transfer all through parameters of the virtual function and accurately to supervise  class fields. A question in other, what for then OOP and all its ecosystem of classes if it is necessary to bypass the same system? And how  functions something to transfer, if it is caused by designer Parent' who even cannot transfer parameters about which knows only Child? To provide in it parameter params: HashMap that dirty image through Reflections to set prothrown through params fields? It is really necessary for avoiding.

13

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> Hello, Sharov, you wrote: S>> Osn. Idea that reversal to this before creation of parent object not . In my opinion, quite reasonably and safely. S> personally I see here disagreements that the designer should do, and it should: S> 1) to Select storage (it is such atavism from which roots) S> 2 grow) to Set properties S> and as it technically is also function S> 3) to Initialize itself S> but this step for some reason  between 1 and 2 for the inexplicable reason. S> I will often come across on the Internet that it is not necessary to use property of the designer-function, i.e. to use (1,2) and not to use (3). S> I think that selection of storage it as though preconstruction of object which generally should be carried out for limits of controllable process to Java, and logically these  should be grouped here so (1) - (2, 3) or (1) - (3, 2) where the step (1) generally is hidden from us, and is caused on a chain separately from the designer. S> we in modern Java tend to splice absolutely heterogeneous processes (1,2) only for the sake of the correct selection of storage. S> here is how should be: when we cause  it causes on a chain all predesigners (without any parameters) simply for storage selection under all fields of all ancestors, and already then we operate with the designer as actually virtual function, we cause its ancestors as that we want (or not to cause  if we do not want). You argue from that point of view that causing n predesigners in us will be n objects. In a reality child and parent . Itself one object (area in storage). I.e. the designer child does not replace, and the designer expands parent. Without parent the designer to tell about child sense is not present. Therefore dancings round basic designers.

14

Re: Assignment of properties and call of the virtual method from the designer

Hello, Sharov, you wrote: S> you argue from that point of view that causing n predesigners in us will be n objects. In a reality child and parent . Itself one object (area in storage). I.e. the designer child does not replace, and the designer expands parent. Without parent the designer to tell about child sense is not present. Therefore dancings round basic designers. No, paternal, designer Parent selects storage under instance.parent_field_x, instance.parent_field_y, then Child selects storage under instance.child_field_z. We cause on a chain predesigners and beforehand we select storage for all object entirely that Child could use it. In the same way, by the way, it is possible to clean automatically storage in  which now is not obliged to be virtual (as in With ++), and here  will be virtual always.

15

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> Hello, Sharov, you wrote: S>> you argue from that point of view that causing n predesigners in us will be n objects. In a reality child and parent . Itself one object (area in storage). I.e. the designer child does not replace, and the designer expands parent. Without parent the designer to tell about child sense is not present. Therefore dancings round basic designers. S> is not present, paternal, designer Parent selects storage under instance.parent_field_x, instance.parent_field_y, then Child selects storage under instance.child_field_z. S> It is caused on a chain predesigners and beforehand we select storage for all object entirely that Child could use it. Well it is correct, at first . Objects of a class, then we use fields of the given class. In the circuit resulted above attempt . Fields to an object construction. The resulted code is rather simple, and here if the method which will use fields Parent is caused generally, and it (Parent) is not created yet.

16

Re: Assignment of properties and call of the virtual method from the designer

17

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> in general, all it becomes, yes, but whether it seems, what we unduly complicated architecture simply because of far-fetched restriction on the job of fields of a class to a call ? It not seems. Without dependence from possibility to do the virtual call in the designer -  (button) will be: 1. To have a state attached/detached (to DOM). Especially in those  who can  on already existing element and-or to complete the house with . 2. Properties on similarity title and color - they or useless fields, or have  and they can use both to and after  with reflection of changes in DOM.  - reading of values from DOM. 3. Events - are similar (2) - podpisyvaemsja/otpisyvaemsja at any time, but in DOM they should podpisyvatsja/otpisyvatsja . Otherwise  will be. (Though the modern browsers if still are not present, is fast and with it consult). I at all do not see what for here a method fill especially in the designer. The designer should create object in a consistent state and only. Thus necessary for an object construction - it is necessary to transfer all parameters through parameters of the designer (paradox?). For this purpose what not to cause ton  after an object construction - it is possible to build ControlParams, ButtonParams or generally faceless/dynamic object-holder with adjustments at initialization. At the same time it could simplify creation  factory on already ready DOM with parameters (or a part) which lie in attributes of an element. Actually something similar I "invented" for itself(himself), it is quite good enough  is organized in google closure tools (library).

18

Re: Assignment of properties and call of the virtual method from the designer

Hello, Sharov, you wrote: S> Well it is correct, at first . Objects of a class, then we use fields of the given class. In the circuit resulted above attempt . Fields to an object construction. The resulted code is rather simple, and here if the method which will use fields Parent is caused generally, and it (Parent) is not created yet. In the circuit with predesigners of the such cannot be. Any method (no less than a body of a method-designer) will be fulfilled only after performance of all predesigners. It is possible to give the chance to redefine them (but this.method () it is impossible to do calls of them categorically, only this.property). Filling of fields  their task would be values (including initialization of links to fields-objects such as protected prop = new ArrayList ()) just.

19

Re: Assignment of properties and call of the virtual method from the designer

Hello, fddima, you wrote: F> Hello, sharez, you wrote: S>> in general, all it becomes, yes, but whether it seems, what we unduly complicated architecture simply because of far-fetched restriction on the job of fields of a class to a call ? F> it not seems. F> without dependence from possibility to do the virtual call in the designer -  (the button) will be: F> 1. To have a state attached/detached (to DOM). Especially in those  who can  on already existing element and-or to complete the house with . F> 2. Properties on similarity title and color - they or useless fields, or have  and they can use both to and after  with reflection of changes in DOM.  - reading of values from DOM. F> 3. Events - are similar (2) - podpisyvaemsja/otpisyvaemsja at any time, but in DOM they should podpisyvatsja/otpisyvatsja . Otherwise  will be. (Though the modern browsers if still are not present, is fast and with it consult). F> I at all do not see what for here a method fill especially in the designer. F> the designer should create object in a consistent state and only. Thus necessary for an object construction - it is necessary to transfer all parameters through parameters of the designer (paradox?) . F> For this purpose what not to cause ton  after an object construction - it is possible to build ControlParams, ButtonParams or generally faceless/dynamic object-holder with adjustments at initialization. At the same time it could simplify creation  factory on already ready DOM with parameters (or a part) which lie in attributes of an element. F> actually something similar I "invented" for itself(himself), it is quite good enough  is organized in google closure tools (library). In that case all setTitle (), setColor () inside besides DOM-tree change should will also check, whether it is created - too quite good such complication, especially, if such methods much (and them much). The main idea was that it is convenient to create this.el with all inner pattern without  to a DOM-tree of immediately document, and to add it only by an explicit call, somehow so: element.appendChild (button.getElement ()); Thus to us prevents to set nothing already at a stage of a call of the designer Title, Color in a tree stored in an internal field this.el. It is possible even to do addEventListener/removeEventListener. Unfortunately, with Google Closure Tools I not a sign, but will look at a leisure.

20

Re: Assignment of properties and call of the virtual method from the designer

21

Re: Assignment of properties and call of the virtual method from the designer

Hello, Carc, you wrote: a C> Hello, netch80, you wrote: N>> There where it is possible to cause the designer in the form of the explicit designer, it is possible to cause and something like public static Button make (), which itself causes the designer (probably, declared private not to show not complete state), causes the necessary methods and returns ready result. To such itself the designer who is not looking syntactically as the designer. (In native library Java there is a set of methods with not intuitive title valueOf () which do something similar.) After that specificity of giblets will be hidden only in the given class and its descendants who should reflect specificity of such formation at itself, but not at users. The C> And on mine it is called Factory? Not? Probably,  not: In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. That I described, it is precisely known, what object of a class will be created. It is possible to try to argue, but it is normally useless. It is better to try to pick up a similar name, but another.

22

Re: Assignment of properties and call of the virtual method from the designer

Hello, sharez, you wrote: S> In that case all setTitle (), setColor () inside besides DOM-tree change should will also check, whether it is created - too quite good such complication, especially, if such methods much (and them much). Complexity not such and big, and it is faster a question of requirements. If we want to have such properties - anyway to check. It is possible certainly and not to check and always to change/read from DOM, but, , possibility  is more favourable. (*) S> the main idea was that it is convenient to create this.el with all inner pattern without  to a DOM-tree of immediately document, and to add it only by an explicit call, somehow so: Not-not-not, binding of object and DOM an element it already also is that . Only, with a difference that the element is under construction . S> Thus to us prevents to set nothing already at a stage of a call of the designer Title, Color in a tree stored in an internal field this.el. It is possible even to do addEventListener/removeEventListener. Prevents: how them to set if conditional createElement is not caused yet? We are returned to the beginning (*), and equally it is necessary to all of us  it is created or not in . If speech about the job only values by default - that of objects-templates/parameters helps with successors. Besides, setting of new values by default in successors -  the bad variant in itself: it is in places it can be unexpected (no less than on the contrary). Actually where it is more interesting that to do if the successor wants to give the DOM tree, and basic setTitle not virtual and writes the text not there. And in the ending - chain Control - Button is difficult in itself. I mean that Control will be terribly overloaded anyway, and  there will be all bound classes at once (and those which not button too). I did to myself Button and LinkButton, but LinkButton installed only  a basic name of style (to be exact a prefix). And here in process  such  from 10-15  -  this canoe time and again, and as a result came to that directly I in  all the same did not create them, and created on the basis of a template (dom tree) where  elements (we tell xcontrol) were uncovered in already present dom. And the code already addressed to them on names. I to that am what well here I do not see necessity of considered possibility for the life of me. Or we know in advance property sets/values and we can freely transfer them in the designer, or we use  after. In the designer  can be quite used - after all while the element not  - to break there is nothing (certainly, if properties not virtual). Besides, it is not necessary and to forget that DOM is always , therefore the approach: changes only through  instead of in DOM directly works a little bit better. (After all over it it is necessary still  to construct, so after all?) . In general, I will finish perhaps that I at faith level consider that the base class should not cause the successor. But, I admit that occasionally in  with me cases in  I sin. And to create convenient  - challenging task. Good luck in it. By the way half modern with ease do   sheet and there and then  somewhere in the most inconvenient place. S> unfortunately, with Google Closure Tools I not a sign, but will look at a leisure. Without fanaticism only, the code + the long history there is a lot of. Examples can are loaded long since not compiled js consists from big kol-va units/files. But they (units) there at least are... Instead of profanation as in TS. I not with all there agree, but inspiration in due time obtained and much. Especially system of units and disposable (in ) it was pleasant. Which that from ideas transferred also to itself. It seems to me it is the most adult dial-up , but  JS repels, and for their compiler it is necessary also correctly to write it. And really normal editors are not present, props everywhere one.