Thursday, February 16, 2006

Foreign Key Reference for Composite Key

Example 1
---------

when you have a unique constraint on the column we can use it as a foreign key even though it is a part of a composite key

CREATE TABLE TEST
(
a INT UNIQUE,
b INT UNIQUE,
c VARCHAR2(1),
CONSTRAINT PK_test PRIMARY KEY (a, b )
)


CREATE TABLE test_child
(
child_a INT REFERENCES TEST(a),
child_b INT ,
child_c VARCHAR2(1)

)


Example 2
---------
the following example shows how to have composite foreign kye on child table referencing composite key on base table.

CREATE TABLE TEST
(
a INT,
b INT,
c VARCHAR2(1),
CONSTRAINT PK_test PRIMARY KEY (a, b )
)


CREATE TABLE TEST_CHILD
(
child_a INT ,
child_b INT ,
child_c VARCHAR2(1))

ALTER TABLE TEST_CHILD ADD CONSTRAINT fk_child_test FOREIGN KEY (child_a, child_b)
REFERENCES TEST(a,b)

Monday, February 13, 2006

Pattern Def & Examples

Creational Patterns - way to create objects while hiding the creation logic
AbstractFactoryis used to return one of several groups of classes. In some cases it actually returns a Factory for that group of classes.
Factory on top of factories
FactoryMethodis used to choose and return an instance of a class from a number of similar classes based on data you provide to the factory.
getting an object of a chess coin depending on the argument passed the peice is a base abstract class , and Pawn, Rook e.t.c are sub classes of piece with implementation to all the methods in the Piece class , Factory class has to be constructed so that it returns a required peice depending on the argument passed ,this is like run time type polymorphisam
Builderassembles a number of objects to make a new object, based on the data with which it is presented. Frequently, the choice of which way the objects are assembled is achieved using a Factory.
Separate the construction of a complex object from its representation so that the same construction process can create different representations.Example for Builder pattern is a Computer Assembly. A computer is nothing but the bundling of various components like FDD, HDD, Monitor etc. But when an user buys a computer someone assemble all these components and given to us. Remember that here the building process is completely hidden from the client or user
Prototypecopies or clones an existing class rather than creating a new instance when creating new instances is more expensive.
clone , java cloning
Singletonis a pattern that insures there is one and only one instance of an object, and that it is possible to obtain global access to that one instance.
DB Connection , returning single DB Connection always
Structural Patterns - define ways to compose objects to obtain new functionalities
Adapterused to change the interface of one class to that of another one.
WindowAdapter , window adapter in java gives dummy implementation for all the event methods so that the deriving classes no need to write implementation for each and every method. i.e consider an abtract class having severla abstract methods.so any concrete sub class has to override all the methods of of that abstract class or atleast it has to give the dummy implementation. To avoid dummy implementation in several sub classes , an adapter class will be written to give dummy implementation to all the methods of the abstract class and all the sub classes will extend that adapter instead of abstract class , and gives implementation to only the required methods , and can ignore the remaining methods.
Bridgeintended to keep the interface to your client program constant while allowing you to change the actual kind of class you display or use. You can then change the interface and the underlying class separately.
If the Java code parses the xml using the JAXP API, the unerlying parsers can be changed without changing the code , the parsers will adher to the JAXP API
Compositea collection of objects, any one of which may be either itself a Composite, or just a primitive object.
File Systems : Directories , Files : directorires can have direcotries and files and so ... on , this is a complex structure
Decoratora class that surrounds a given class, adds new capabilities to it, and passes all the unchanged methods to the underlying class.
Milk , choclate Milk , Double Choclate Milk , Rose Milk : choclate , double choclate , rose are decorations another example is Button , Bevel border button etc.
Facadewhich groups a complex object hierarchy and provides a new, simpler interface to access those data.
same Remote to switch on light , tv , fan
Flyweightwhich provides a way to limit the proliferation of small, similar class instances by moving some of the class data outside the class and passing it in during various execution methods.
String class in java , if we delcare two string variables with the same value , they both refer to the same memory location.
Proxywhich provides a simple place-holder class for a more complex class which is expensive to instantiate.
RMI , uses stub and skeleton to implement the proxy pattern.
Behavioral Patterns - concerned with communication between objects
Chain of Respallows an even further decoupling between classes, by passing a request between classes until it is recognized.
servlet Filters,It decouples the sender of the request to the receiver. The only link between sender and the receiver is the request which is sent. Based on the request data sent, the receiver is picked.
Commandprovides a simple way to separate execution of a command from the interface environment that produced it
implementation of AWT event handling in JAVA, ActionListner A classic example of this is a restaurant. A customer goes to restaurant and orders the food according to his/her choice. The waiter/ waitress takes the order (command, in this case) and hands it to the cook in the kitchen.The cook can make several types of food and so, he/she prepares the ordered item and hands it over to the waiter/waitress who in turn serves to the customer The order is an object which depends on the command. The food item will change as soon as the command changes. This is loose-coupling between the client and the implementation.
Interpreterprovides a definition of how to include language elements in a program.
The Interpreter Pattern defines a grammatical representation for a language and an interpreter to interpret the grammar.Regular Expression in Java , REs will have thier own sytnax and implementation even though RE is in JAVA.Another example , suppose user enters 123.78 and we have to translate it into "rupees on hundread and twenty three and paise seveny eight only"
Iteratorformalizes the way we move through a list of data within a class.
The Iterator pattern is one, which allows you to navigate through a collection of data using a common interface without knowing about the underlying implementation. eX : Moving through list
Mediatordefines how communication between classes can be simplified by using another class to keep all classes from having to know about each other.
List Left , Lest Right , CommangButton , Text Box talk thorugh a mediator class , meditor should be in all classes and Meditator shold know all classes ------- flights with base control
MementoMemento
For most applications, it is often very useful to provide mechanisms that allow the users to undo an operation or to recover from a state that violates constraints specified by the application. The standard method is to store the previous states via cloning a copy of all changed objects. But this is often infeasible since the overhead of storing these clones would be too much. An alternative is to create only partial clone, storing only the attributes that are required for the undo operation. The concept is very simple, but is problematic since the attributes usually have restricted access due to encapsulation. To get around this, the idea is to have the class itself provide a helper object with the important data, a memento. When given back a memento, the class can then reset its state according the information contained within the memento
Observerdefines the way a number of classes can be notified of a change,
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. ex : Threads
Stateprovides a memory for a class’s instance variables.
The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion
Strategyencapsulates an algorithm inside a class
Strategy1 , Strategy2 , Strategy3 exists depending on some algorithm we select one of them
Templateprovides an abstract definition of an algorithm
The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses. Home builders use the Template Method when developing a new subdivision. A typical subdivision consists of a limited number of floor plans with different variations available for each. Within a floor plan, the foundation, framing, plumbing, and wiring will be identical for each house. Variation is introduced in the later stages of construction to produce a wider variety of models
Visitoradds function to a class
The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer. Upon entering the taxi the customer, or Visitor, is no longer in control of his or her own transportation, the taxi (driver) is

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]