My Component Design experience
¥DÃD: ¤¸¥ó³]p²¤¶
¥Øªº
¥¼¨Ó¸ê°T¨t²Î´Â¦V¤À´²¦¡ N-Tie¤Îºô»Úºô¸ôªº¶}µo¼Ò¦¡,¨t²Î¶}µo¤Hû¥²»Ý¨ã³Æ¤¸¥óªº¶}µo¯à¤O,
¦Û¦æ³]p¨t²Î¯S®í»Ý¨Dªº¤¸¥ó,¤~¯àº¡¨¬¥¼¨Ó¸ê°T¨t²Îµo®iªºÁͶÕ.
®Éµ{: 6HR
Object ªº·§©À(2 HR)
Component ³]p(4 HR)
½Òµ{¤jºõ
¤@. Object ªº·§©À(2HR)
1. Class:
A class is a user-defined data type.
A class has some internal data and some methods,in the form of procedures or functions,
and usually described the generic characteristics and behavior of a number of very similar
object.
1>class property / method / event
1>>property: A class property is used to maintain general information related to the class
2>> method: A class method is a method related to the whole class,not its objects.
3>> event: A class event provide a time for class to process windows message(mouseclick/keypress/keydown..).
2>event handle
Event handle is an object procedure to process something while event occur.
It is like a procedure type,but refers to an object method.
Ex: procedure Button1Click(Sender: TObject);
2. Object:
An object is an instance of a class. Objects are real entities.
1>The relationship between object and class is the same as the one between variable and type.
each variable of a class type,does not hold the value of the object,Rather it contains
reference,or a pointer, to indicate the memory location where the object has been stored.
2> Object instances must be created manually,at least for the objects of the classes you define.
Instances of the component you place on a form are built automatically.
3> To create an instance of an object,we can call its Create method,which is a constructor.
A Constructor is a special procedure used to allocate memeoy for new objects and initialize
them.
4> Self can be defined as a pointed to the current object.
5> Example
1_ClassObject.11_ClassObject
6> Constructor & Destrcutor & Self
1>>To allocate the memory for the object,we call the create method.
However before we can actually use the object,we often need to initialize it.
To do this, we can add a constructor to the class.
2>>Just as a constrcutor call allocates memory for the object,a destructor call frees the memeoy.
3>> Example
1_ClassObject.12_ClassObject
¤G.Object oriented ªº¯S©Ê
1. inherited(Ä~©Ó)
2. encapsulated(«Ê¥])
1>For a good object-oriented approach, data should be hidden,or encapsulated, inside the class
using it .
2>You seldom know which internal data the object has,and usually have no way to access it directly,
Of course you are supposed to use methods to access the data,which is shielded from unauthorized
access. The is the object-oriented approach to a classical programming theory known as
information hiding.
3>¬[ºc:
Public
init
SetValue <----------|
LeapYear |
|
------------------------------------------------|
|
Private | from Another class
Year |
Month <---X------|
Day
4> private / protected / public / published
1.Private:
The private keyword denotes fields and methods of a class that are not accessible outside
the unit.
2.Protected:
The protected keyword is used to indicate partially protected methods and fields.
Protected elements can be accessed by the current class and all its descendent classes.
3.Public:
The public keyword denotes methods and fields that are freely accessible from any other portion of the code of a program ,as well as in the unit in which they are defined.
4.Published:
A published field or method is not only available at run-time but also at design-time.
In fact,only the published methods of your form can show up in the Object Inspector.
5>Example
2_Private
3 Polymorphism(¦h«¬)
Object-oriented programming languages,include Object Pascal,allow the use of another form
of binding,known as dynamic binding or late binding.In this case,the actual address of the
method to be called is determined at run-time
The advantage of this approach is known as polymorphism.
1> Static & Dynamic & Virtual & Abstract method
1>> Static method:
Pascal function and procedure are usually based on static binding.
This means that a method call is resolved by the compiler and the linker.
2>> Virtual method
another form of binding,known as dynamic binding or late binding.
In this case,the actual address of the method to be called is determined at run-time
you write the call to a method,but the code actually called depends on the type of the
object.
3>>.Override Virtual method:
To override a virtual method in a descendent class,you need to use the override keyword.
this can take place only if the method was defined as virtual in the ancestor class.
Otherwise ,if it was a static method ,there is no way to make it dynamic,other then by
changing the code of ancestor class.why you need to use the override keyword.
In other languages ,when you redefine a method in a subclass,you automatically override
the original one.However ,having a specific keyword allows the compiler to check the
correspondence between the names of methods of the methods of the ancestor class and
subclass.
PS: Overload method
Object Pascal also supports overloaded funtcions and methods:
you can have multiple methods with the same name,provided that
the parameters are different.
By checking the parameters,the compiler can determine which of the versions of the
routine you want to call. Example : 2_private
4>>Dynamic method:
The result of Dynamic method use is also the same with Virtual.
The different between dynamic & virtual is the internal mechanism used by the compiler to
implement dynamic binding.
Virtual methods are based on a virtual method table(VMT).
Dynamic medhods are dispatched using a unique code indicating the method. slightly different
speed or memory usage between virtual & dynamic method.
If the method is going to be overridden by nearly every descendent,make it virtual.
If the method is not going to be overridden very often by descendent,make it dynamic.
5>>Abstract method:
The abstract keyword is used to declare methods that will be implemented only in subclasses if the current class.
The Abstract method must be also Virtual or Dynamic method.
Example
1_ClassObject.Real
1.6 Run-time Type information
The Object Pascal type-compatibility rule for descendent classes allows you to use a
descendent class where an ancestor class is expected.
Question:
Suppose that the Dog class has an Eat function,which is not present in the Animal class.
if the variable,MyAnimal refers to a dog,it should be possible to call the function.
the variable is referring to another class,the result is an error.
Solution:
we can use some techniques based on RTTI.in short, each object knows its type and its
inheritance.
Ex:
Procedure DoEat(MyAnimal:TAnimal);
begin
if MyAnimal is Dog then begin
MyDog:= Dog(MyAnimal); // MyDog := MyAnimal as Dog
MyDog.Eat;
end;
end;
ð (MyAnimal as Dog).Eat; // ¤]¥i
ð
1.7 Example:
3_Polymorphism
2 Component ³]p(3HR)
2.1 Create Component
Create a new component manually requires four steps:
1>Create a new unit
2>Deriving the component object
TMyComponent = class(TComponent)
private
..
published
..
end;
3>design component field / method / property /event
4>Registering the component
Registering a component is a simple process that tells Delphi what components to
add to its components library and which pages of the Component palette the components
should appear on.
call RegisterComponents procedure for each component you want to register.
RegisterComponent take two parameters:
.the name of a Component palette page
.a set of component types.
Ex:
procedure Register;
begin
RegisterComponents('Samples',[TMyComponent]) ;
end;
5>Install Component
Example : 4_FirstComp.31_NewButton
¥Øªº: ¦Û¦æ³]p NewButton Object ,Ä~©Ó¦Û TButton
¦ý·s¼W¤@ Property ==> Value :integer;
method ==> procedure ShowValue;
2.2 Create Property
1.To declare a property,you specify three things:
.The name of the property
.The type of the property
.Methods to read and/or set the value of the property
2.conventions for property:
There are no restrictions on how you store the data for a property,In general,
however,Delphi's component follow these conventions:
.Property data is stored in object fields.
.Identifier for object fields start with the letter f.
.Object fields for property data should be declared as private.
This ensures that the component that declares the property has accesss to then ,
but component users and descendant component don't.if a method or or another property needs
to change that data,it should do so through the property, not by direct access to the stored
data.
.The syntax for property declarations
property :
read