1. Trang chủ
  2. » Kỹ Năng Mềm

PatternPrototypepdf

6 2 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

z C++ default copy constructor does a member-wise copy Æ pointers will be shared between the copy and the original. z Initializing clones[r]

(1)

Prototype Pattern

Prototype Pattern

CSIE Department, NTUT Woei-Kae Chen

Prototype: Intent

Prototype: Intent

z

Specify the kinds of objects to create using a

prototypical instance, and create new objects

by copying this prototype

z

Use a pointer (reference) to a base class object

to create (clone) a concrete class object.

AbstractClass *p, *p1; // client

p1 = new AbstractClass; // error

p1 = new ConcreteClass; // ok but

(2)

Prototype: Motivation

Prototype: Motivation

Tool Manipulate()

GraphicTool

Manipulate()

WholeNote

Draw()

HalfNote

Draw() Clone() Clone()

return copy of self return copy of self prototype

p=prototype->Clone() while (user drags mouse) {

p->Draw(new position) }

insert p into drawing

Graphic Draw() Clone()

Staff

Draw() Clone()

MusiclNote

Client

Prototype: Applicability

Prototype: Applicability

z

When system should be independent of how

its products are

created

, composed, and

represented.

– when the classes to instantiate are specified at run-time

– to avoid building a class hierarchy of factories that parallelsthe class hierarchy of products

(3)

Prototype: Structure

Prototype: Structure

ConcretePrototype2 Client

Operation()

Prototype Clone()

ConcretePrototype1

Clone() Clone()

prototype

p=prototype->Clone()

return copy of self return copy of self

Prototype: Participants

Prototype: Participants

z

Prototype (Graphic)

– declares an interface for cloning itself

z

ConcretePrototype (Staff, etc )

– implements an operation for cloning itself

z

Client (GraphicTool)

(4)

Prototype: Collaboration

Prototype: Collaboration

z

A Client ask a prototype to clone itself

AbstractClass *p, *p1; // client

p1 = p->Clone(); // prototype

Prototype: Consequences (1)

Prototype: Consequences (1)

zHidesthe concrete product classes from the clientỈ

reducing the number of names clients know about

– Adding and removing products at run-time

zadd a new concrete class into a system by simply registering a prototypical instance

– Specifying new objects by varying values zvary the values of an object’s variables and clone

– Specifying new objects by varying structure zcloning a subcircuit (composed as a Composite Pattern)

(5)

Prototype: Consequences (2)

Prototype: Consequences (2)

z

Each subclass of Prototype must implement the

Clone

operation

– may be difficult when the internals of a concrete subclass include objects that does not support copying

– may be difficult when the internals of a concrete subclass includes objects that have circular references

Prototype: Implementation (1)

Prototype: Implementation (1)

zUsing a prototype manager

– client keeps a registry of available prototypes zthe number of prototypes need not be fixed

zenables clients to extend without writing code zImplementing the Cloneoperation

– shallowcopy versus deepcopy

zdo the clone and the original share variables?

zC++ default copy constructor does a member-wise copy Ỉpointers will be shared between the copy and the original

zInitializing clones

– no parameter for the Cloneoperation (uniform interface)

zreset states after clone

zintroduce an Initializeoperation

Original Ptr Object

Ptr Object

deep copy

(6)

Prototype: Implementation (2)

Prototype: Implementation (2)

z

C++ example:

class AbstractClass { public:

virtual AbstractClass *Clone() const;

};

class ConcreteClass { public:

AbstractClass *Clone() const {

return new ConcreteClass(*this); }

};

Copy Constructor Shallow copy? Deep copy?

Prototype: Related Patterns

Prototype: Related Patterns

z

Prototype and Abstract Factory are

competing patterns.

Ngày đăng: 20/05/2021, 03:44

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN