Ruby/Metody singletonowe: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Szymon wro (dyskusja | edycje)
m Ruby/Metody singleton przeniesiono do Ruby/Metody singletonowe: Bardziej po polsku chyba
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Linia 1:
== Metody singletonowe ==
The behavior of an instance is determined by its class, but there may be times we know that a particular instance should have special behavior. In most languages, we must go to the trouble of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods.
 
Zachowanie instancji zdeterminowane jest przez jej klasę, ale mogą być sytuacje, w których wiemy, że konkretna instancja powinna mieć specjalne zachowanie. W większości języków musimy niestety definiować nową klasę, która byłaby wtedy instancjonowana tylko raz. W Rubim możemy dać każdemu obiektowi jego własne metody.
 
<pre>
ruby> class SingletonTest
| def size
Linia 19 ⟶ 22:
ruby> test2.size
10
</pre>
 
W tym przykładzie <tt>test1</tt> i <tt>test2</tt> należą do tej samej klasy, ale <tt>test2</tt> otrzymał przedefiniowaną metodę <tt>size</tt>, więc zachowują obiekty się odmiennie. Metoda dana pojedynczemu obiektowi nazywana jest ''metodą singletonową''.
In this example, test1 and test2 belong to same class, but test2 has been given a redefined size method and so they behave differently. A method given only to a single object is called a singleton method.
 
Metody singletonowe są często używane w elementach graficznego interfejsu użytkownika (ang. GUI), gdzie różne działania muszą być podjęte, w zależności od naciśniętych przycisków.
Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.
 
SingletonMetody methodssingletonowe arenie notwystępują uniquejedynie tow ruby,Rubim. asPojawiają theysię appearrównież inw CLOS, DylanDylanie, etcitd. AlsoPonadto, someniektóre languagesjęzyki, fordla example,przykładu Self andi NewtonScript, haveposiadają singletontylko methodsmetody onlysingletonowe. These areone sometimesczasem callednazywane prototype-based''językami languagesprototypowymi''.
<noinclude>
{{ProstaNawigacja|spis=Ruby|poprzart=Ruby/Kontrola dostępu|poprz=Kontrola dostępu|nastart=Ruby/Moduły|nast=Moduły}}