Поддерживать
www.wikidata.ru-ru.nina.az
U etogo termina sushestvuyut i drugie znacheniya sm Prototip Prototip angl Prototype porozhdayushij shablon proektirovaniya PrototipPrototypeTip porozhdayushijOpisan v Design Patterns DaNaznachenieZadayot vidy sozdavaemyh obektov s pomoshyu ekzemplyara prototipa i sozdayot novye obekty putyom kopirovaniya etogo prototipa On pozvolyaet ujti ot realizacii i pozvolyaet sledovat principu programmirovanie cherez interfejsy V kachestve vozvrashayushego tipa ukazyvaetsya interfejs abstraktnyj klass na vershine ierarhii a klassy nasledniki mogut podstavit tuda naslednika realizuyushego etot tip Proshe govorya eto pattern sozdaniya obekta cherez klonirovanie drugogo obekta vmesto sozdaniya cherez konstruktor PrimeneniePattern ispolzuetsya chtoby izbezhat dopolnitelnyh usilij po sozdaniyu obekta standartnym putyom imeetsya v vidu ispolzovanie konstruktora tak kak v etom sluchae takzhe budut vyzvany konstruktory vsej ierarhii predkov obekta kogda eto nepozvolitelno dorogo dlya prilozheniya izbezhat nasledovaniya sozdatelya obekta object creator v klientskom prilozhenii kak eto delaet pattern abstract factory Ispolzujte etot shablon proektirovaniya kogda sisteme bezrazlichno kak imenno v nej sozdayutsya komponuyutsya i predstavlyayutsya produkty instanciruemye klassy opredelyayutsya vo vremya vypolneniya naprimer s pomoshyu dinamicheskoj zagruzki izbezhat postroeniya ierarhij klassov ili fabrik parallelnyh ierarhii klassov produktov ekzemplyary klassa mogut nahoditsya v odnom iz neskolkih razlichnyh sostoyanij Mozhet okazatsya udobnee ustanovit sootvetstvuyushee chislo prototipov i klonirovat ih a ne instancirovat kazhdyj raz klass vruchnuyu v podhodyashem sostoyanii PrimeryPrimer na Python Ishodnyj tekst na yazyke Python usr bin env python coding utf 8 import copy class Prototype def init self self objects def register object self name obj Register an object self objects name obj def unregister object self name Unregister an object del self objects name def clone self name attr Clone a registered object and update inner attributes dictionary obj copy deepcopy self objects get name obj dict update attr return obj class A def init self self x 3 self y 8 self z 15 self garbage 38 11 19 def str self return format self x self y self z self garbage def main a A prototype Prototype prototype register object objecta a b prototype clone objecta c prototype clone objecta x 1 y 2 garbage 88 1 print str i for i in a b c if name main main OUTPUT 3 8 15 38 11 19 3 8 15 38 11 19 1 2 15 88 1 Primer na C Ishodnyj tekst na yazyke C class Meal public virtual Meal virtual void eat 0 virtual Meal clone const 0 class Spaghetti public Meal public Spaghetti const Spaghetti amp void eat Spaghetti clone const return new Spaghetti this Primer na Java Ishodnyj tekst na yazyke Java Prototype Class public class Cookie implements Cloneable protected int weight Override public Cookie clone throws CloneNotSupportedException Cookie copy Cookie super clone In an actual implementation of this pattern you might now change references to the expensive to produce parts from the copies that are held inside the prototype return copy Concrete Prototypes to clone public class CoconutCookie extends Cookie Client Class public class CookieMachine private Cookie cookie Could have been a private Cloneable cookie public CookieMachine Cookie cookie this cookie cookie public Cookie makeCookie throws CloneNotSupportedException return Cookie this cookie clone public static void main String args throws CloneNotSupportedException Cookie tempCookie null Cookie prot new CoconutCookie CookieMachine cm new CookieMachine prot for int i 0 i lt 100 i tempCookie cm makeCookie Primer na Scala Ishodnyj tekst na yazyke Scalapackage com package object prototype class Waffle protected var name String protected var primaryFilling String protected var specialFilling Option String None extends Cloneable override def clone Waffle super clone asInstanceOf Waffle def output Unit println s Waffle name with primary filling primaryFilling if specialFilling None specialFilling get else object PrototypeTest def main args Array String Unit println Output val chocolateWaffle new Waffle ChocolateWaffle Chocolate chocolateWaffle output chocolateWaffle clone output val coconutWaffle new Waffle CoconutWaffle Condensed milk Some Coconut coconutWaffle output coconutWaffle clone output Output Waffle ChocolateWaffle with primary filling Chocolate Waffle ChocolateWaffle with primary filling Chocolate Waffle CoconutWaffle with primary filling Condensed milkCoconut Waffle CoconutWaffle with primary filling Condensed milkCoconut Primer na C Ishodnyj tekst na yazyke C using System namespace Prototype class MainApp static void Main Create two instances and clone each Prototype prototype1 new ConcretePrototype1 I Prototype clonedPrototype1 prototype1 Clone Console WriteLine Cloned 0 clonedPrototype1 Id Prototype prototype2 new ConcretePrototype2 II Prototype clonedPrototype2 prototype2 Clone Console WriteLine Cloned 0 clonedPrototype2 Id Prototype public abstract class Prototype Constructor public Prototype string id this Id id Console Write Base constructor is called Property public string Id get private set public virtual Prototype Clone Shallow copy return Prototype this MemberwiseClone ConcretePrototype1 public class ConcretePrototype1 Prototype Constructor public ConcretePrototype1 string id base id ConcretePrototype2 public class ConcretePrototype2 Prototype Constructor public ConcretePrototype2 string id base id Primer na PHP Ishodnyj tekst na yazyke PHP lt php namespace RefactoringGuru Prototype Conceptual Primer klassa imeyushego vozmozhnost klonirovaniya My posmotrim kak proishodit klonirovanie znachenij polej raznyh tipov class Prototype public primitive public component public circularReference PHP imeet vstroennuyu podderzhku klonirovaniya Vy mozhete klonirovat obekt bez opredeleniya kakih libo specialnyh metodov pri uslovii chto ego polya imeyut primitivnye tipy Polya soderzhashie obekty sohranyayut svoi ssylki v klonirovannom obekte Poetomu v nekotoryh sluchayah vam mozhet ponadobitsya klonirovat takzhe i vlozhennye obekty Eto mozhno sdelat specialnym metodom clone public function clone this gt component clone this gt component Klonirovanie obekta kotoryj imeet vlozhennyj obekt s obratnoj ssylkoj trebuet specialnogo podhoda Posle zaversheniya klonirovaniya vlozhennyj obekt dolzhen ukazyvat na klonirovannyj obekt a ne na ishodnyj obekt this gt circularReference clone this gt circularReference this gt circularReference gt prototype this class ComponentWithBackReference public prototype Obratite vnimanie chto konstruktor ne budet vypolnen vo vremya klonirovaniya Esli u vas slozhnaya logika vnutri konstruktora vam mozhet potrebovatsya vypolnit ee takzhe i v metode clone public function construct Prototype prototype this gt prototype prototype Klientskij kod function clientCode p1 new Prototype p1 gt primitive 245 p1 gt component new DateTime p1 gt circularReference new ComponentWithBackReference p1 p2 clone p1 if p1 gt primitive p2 gt primitive echo Primitive field values have been carried over to a clone Yay n else echo Primitive field values have not been copied Booo n if p1 gt component p2 gt component echo Simple component has not been cloned Booo n else echo Simple component has been cloned Yay n if p1 gt circularReference p2 gt circularReference echo Component with back reference has not been cloned Booo n else echo Component with back reference has been cloned Yay n if p1 gt circularReference gt prototype p2 gt circularReference gt prototype echo Component with back reference is linked to original object Booo n else echo Component with back reference is linked to the clone Yay n clientCode Primer na Ruby Ishodnyj tekst na yazyke Rubymodule Prototype Prototype class Prototype Property svojstvo id iznachalno prisutstvuet u kazhdogo obekta poetomu vospolzuemsya svojstvom name attr reader name Constructor def initialize name name name end end end Create an instance and clone it p1 Prototype Prototype new my name obekt klassa Prototype sozdaetsya tradicionnym putyom metodom new p2 p1 clone metod clone sushestvuet u kazhdogo obekta iznachalno ego ne nuzhno opredelyat puts p1 id p1 object id p2 id p2 object id budut napechatany raznye id puts p1 name p1 name p2 name p2 name budut napechatany odinakovye name my name Wait for user gets Primer na VB NET Ishodnyj tekst na yazyke VB NETNamespace Prototype Class MainApp Shared Sub Main Sozdanie dvuh ekzemplyarov i klonirovanie kazhdogo Dim p1 As Prototype New ConcretePrototype1 I Dim c1 As Prototype p1 Clone Console WriteLine Cloned 0 c1 Id Dim p2 As Prototype New ConcretePrototype2 II Dim c2 As Prototype p2 Clone Console WriteLine Cloned 0 c2 Id Console Read End Sub End Class Prototype MustInherit Class Prototype Private m id As String Konstruktor Public Sub New ByVal id As String Me m id id End Sub Svojstvo Public ReadOnly Property Id As String Get Return m id End Get End Property Public MustOverride Function Clone As Prototype End Class ConcretePrototype1 Class ConcretePrototype1 Inherits Prototype Konstruktor Public Sub New ByVal id As String MyBase New id End Sub Public Overrides Function Clone As Prototype Nepolnaya kopiya Return DirectCast Me MemberwiseClone Prototype End Function End Class ConcretePrototype2 Class ConcretePrototype2 Inherits Prototype Konstruktor Public Sub New ByVal id As String MyBase New id End Sub Public Overrides Function Clone As Prototype Nepolnaya kopiya Return DirectCast Me MemberwiseClone Prototype End Function End Class End Namespace Primer na Delphi Ishodnyj tekst na yazyke Delphiprogram PrototypePattern APPTYPE CONSOLE uses SysUtils type TPrototype class public function Clone TPrototype virtual abstract end type TPrototypeType class TPrototype private FID Integer FInfo String public property ID Integer read FID write FID property Info String read FInfo write FInfo function Clone TPrototype override end function TPrototypeType Clone TPrototype var vClone TPrototypeType begin vClone TPrototypeType Create vClone ID ID vClone Info Info Result vClone end procedure CloneAndShow Prototype TPrototypeType var vClone TPrototypeType begin vClone Prototype Clone try Write vClone ID Write vClone Info finally vClone Free end WriteLn end var vConcretePrototype1 vConcretePrototype2 TPrototypeType begin vConcretePrototype1 TPrototypeType Create vConcretePrototype2 TPrototypeType Create try vConcretePrototype1 ID 10 vConcretePrototype1 Info Prototype1 vConcretePrototype2 ID 11 vConcretePrototype2 Info Prototype2 CloneAndShow vConcretePrototype1 CloneAndShow vConcretePrototype2 finally vConcretePrototype1 Free vConcretePrototype2 Free end ReadLn end Primer na CoffeeScript Primer na CoffeeScriptclass PresidentPrototype constructor proto gt clone gt customer new President customer first proto first customer last proto last customer aka proto aka customer class President constructor first last aka gt say gt console log His name is first last aka aka run gt proto new President Jimmy Wales Jimbo prototype new PresidentPrototype proto customer prototype clone customer say run Primer na Io Ishodnyj tekst na yazyke IoFoo Object clone Foo smth 2 Bar Foo cloneLiteraturaE Gamma R Helm R Dzhonson Dzh Vlissides Priemy obektno orientirovannogo proektirovaniya Patterny proektirovaniya Design Patterns Elements of Reusable Object Oriented Software SPb Piter 2007 S 366 ISBN 978 5 469 01136 1 takzhe ISBN 5 272 00355 1 Sm takzhePrototipnoe programmirovaniePrimechaniyaPrototip na PHP rus refactoring guru Data obrasheniya 18 iyunya 2023 18 iyunya 2023 goda SsylkiPattern Prototype Prototip naznachenie opisanie dva varianta realizacii C dostoinstva i nedostatki
Вершина