Tuesday, March 31, 2009

Template Method

Posted on/at 1:19 PM by Admin


Intent
  • Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
  • Base class declares algorithm 'placeholders', and derived classes implement the placeholders.
Problem

Two different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended.

Discussion

The component designer decides which steps of an algorithm are invariant (or standard), and which are variant (or customizable). The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. The variant steps represent "hooks", or "placeholders", that can, or must, be supplied by the component's client in a concrete derived class.

The component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps.

Template Method is used prominently in frameworks. Each framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or interesting client customization options. In so doing, the framework becomes the "center of the universe", and the client customizations are simply "the third rock from the sun". This inverted control structure has been affectionately labelled "the Hollywood principle" - "don't call us, we'll call you".

Structure

Template Method scheme

The implementation of template_method() is: call step_one(), call step_two(), and call step_three(). step_two() is a "hook" method – a placeholder. It is declared in the base class, and then defined in derived classes. Frameworks (large scale reuse infrastructures) use Template Method a lot. All reusable code is defined in the framework's base classes, and then clients of the framework are free to define customizations by creating derived classes as needed.

Template Method scheme

Example

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.

Template Method example

Check list
  1. Examine the algorithm, and decide which steps are standard and which steps are peculiar to each of the current classes.
  2. Define a new abstract base class to host the "don't call us, we'll call you" framework.
  3. Move the shell of the algorithm (now called the "template method") and the definition of all standard steps to the new base class.
  4. Define a placeholder or "hook" method in the base class for each step that requires many different implementations. This method can host a default implementation – or – it can be defined as abstract (Java) or pure virtual (C++).
  5. Invoke the hook method(s) from the template method.
  6. Each of the existing classes declares an "is-a" relationship to the new abstract base class.
  7. Remove from the existing classes all the implementation details that have been moved to the base class.
  8. The only details that will remain in the existing classes will be the implementation details peculiar to each derived class.
Rules of thumb
  • Strategy is like Template Method except in its granularity.
  • Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
  • Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
  • Factory Method is a specialization of Template Method.
Read next

Source:

http://sourcemaking.com/design_patterns/template_method


Visitor Pattern

Posted on/at 1:18 PM by Admin

Intent
  • Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • The classic technique for recovering lost type information.
  • Do the right thing based on the type of two objects.
  • Double dispatch
Problem

Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid "polluting" the node classes with these operations. And, you don't want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation.

Discussion

Visitor's primary purpose is to abstract functionality that can be applied to an aggregate hierarchy of "element" objects. The approach encourages designing lightweight Element classes - because processing functionality is removed from their list of responsibilities. New functionality can easily be added to the original inheritance hierarchy by creating a new Visitor subclass.

Visitor implements "double dispatch". OO messages routinely manifest "single dispatch" - the operation that is executed depends on: the name of the request, and the type of the receiver. In "double dispatch", the operation executed depends on: the name of the request, and the type of TWO receivers (the type of the Visitor and the type of the element it visits).

The implementation proceeds as follows. Create a Visitor class hierarchy that defines a pure virtual visit() method in the abstract base class for each concrete derived class in the aggregate node hierarchy. Each visit() method accepts a single argument - a pointer or reference to an original Element derived class.

Each operation to be supported is modelled with a concrete derived class of the Visitor hierarchy. The visit() methods declared in the Visitor base class are now defined in each derived subclass by allocating the "type query and cast" code in the original implementation to the appropriate overloaded visit() method.

Add a single pure virtual accept() method to the base class of the Element hierarchy.accept() is defined to receive a single argument - a pointer or reference to the abstract base class of the Visitor hierarchy.

Each concrete derived class of the Element hierarchy implements the accept() method by simply calling the visit() method on the concrete derived instance of the Visitor hierarchy that it was passed, passing its "this" pointer as the sole argument.

Everything for "elements" and "visitors" is now set-up. When the client needs an operation to be performed, (s)he creates an instance of the Vistor object, calls the accept() method on each Element object, and passes the Visitor object.

The accept() method causes flow of control to find the correct Element subclass. Then when the visit() method is invoked, flow of control is vectored to the correct Visitor subclass.accept() dispatch plus visit() dispatch equals double dispatch.

The Visitor pattern makes adding new operations (or utilities) easy - simply add a new Visitor derived class. But, if the subclasses in the aggregate node hierarchy are not stable, keeping the Visitor subclasses in sync requires a prohibitive amount of effort.

An acknowledged objection to the Visitor pattern is that is represents a regression to functional decomposition - separate the algorithms from the data structures. While this is a legitimate interpretation, perhaps a better perspective/rationale is the goal of promoting non-traditional behavior to full object status.

Structure

The Element hierarchy is instrumented with a "universal method adapter". The implementation of accept() in each Element derived class is always the same. But – it cannot be moved to the Element base class and inherited by all derived classes because a reference to this in the Element class always maps to the base type Element.

Visitor scheme

When the polymorphic firstDispatch() method is called on an abstract First object, the concrete type of that object is "recovered". When the polymorphic secondDispatch()method is called on an abstract Second object, its concrete type is "recovered". The application functionality appropriate for this pair of types can now be exercised.

Visitor scheme

Example

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.

Visitor example

Check list
  1. Confirm that the current hierarchy (known as the Element hierarchy) will be fairly stable and that the public interface of these classes is sufficient for the access the Visitor classes will require. If these conditions are not met, then the Visitor pattern is not a good match.
  2. Create a Visitor base class with a visit(ElementXxx) method for each Element derived type.
  3. Add an accept(Visitor) method to the Element hierarchy. The implementation in each Element derived class is always the same – accept( Visitor v ) { v.visit( this ); }. Because of cyclic dependencies, the declaration of the Element and Visitor classes will need to be interleaved.
  4. The Element hierarchy is coupled only to the Visitor base class, but the Visitor hierarchy is coupled to each Element derived class. If the stability of the Element hierarchy is low, and the stability of the Visitor hierarchy is high; consider swapping the 'roles' of the two hierarchies.
  5. Create a Visitor derived class for each "operation" to be performed on Element objects.visit() implementations will rely on the Element's public interface.
  6. The client creates Visitor objects and passes each to Element objects by calling accept().
Rules of thumb
  • The abstract syntax tree of Interpreter is a Composite (therefore Iterator and Visitor are also applicable).
  • Iterator can traverse a Composite. Visitor can apply an operation over a Composite.
  • The Visitor pattern is like a more powerful Command pattern because the visitor may initiate whatever is appropriate for the kind of object it encounters.
  • The Visitor pattern is the classic technique for recovering lost type information without resorting to dynamic casts.
Notes

The November 2000 issue of JavaPro has an article by James Cooper (author of a Java companion to the GoF) on the Visitor design pattern. He suggests it "turns the tables on our object-oriented model and creates an external class to act on data in other classes ... while this may seem unclean ... there are good reasons for doing it."

His primary example. Suppose you have a hierarchy of Employee-Engineer-Boss. They all enjoy a normal vacation day accrual policy, but, Bosses also participate in a "bonus" vacation day program. As a result, the interface of class Boss is different than that of class Engineer. We cannot polymorphically traverse a Composite-like organization and compute a total of the organization's remaining vacation days. "The Visitor becomes more useful when there are several classes with different interfaces and we want to encapsulate how we get data from these classes."

His benefits for Visitor include:

  • Add functions to class libraries for which you either do not have the source or cannot change the source
  • Obtain data from a disparate collection of unrelated classes and use it to present the results of a global calculation to the user program
  • Gather related operations into a single class rather than force you to change or derive classes to add these operations
  • Collaborate with the Composite pattern

Visitor is not good for the situation where "visited" classes are not stable. Every time a new Composite hierarchy derived class is added, every Visitor derived class must be amended.

Source:

http://sourcemaking.com/design_patterns/visitor

Observer Pattern

Posted on/at 1:17 PM by Admin

 

Intent
  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy.
  • The "View" part of Model-View-Controller.
Problem

A large monolithic design does not scale well as new graphing or monitoring requirements are levied.

Discussion

Define an object that is the "keeper" of the data model and/or business logic (the Subject). Delegate all "view" functionality to decoupled and distinct Observer objects. Observers register themselves with the Subject as they are created. Whenever the Subject changes, it broadcasts to all registered Observers that it has changed, and each Observer queries the Subject for that subset of the Subject's state that it is responsible for monitoring.

This allows the number and "type" of "view" objects to be configured dynamically, instead of being statically specified at compile-time.

The protocol described above specifies a "pull" interaction model. Instead of the Subject "pushing" what has changed to all Observers, each Observer is responsible for "pulling" its particular "window of interest" from the Subject. The "push" model compromises reuse, while the "pull" model is less efficient.

Issues that are discussed, but left to the discretion of the designer, include: implementing event compression (only sending a single change broadcast after a series of consecutive changes has occurred), having a single Observer monitoring multiple Subjects, and ensuring that a Subject notify its Observers when it is about to go away.

The Observer pattern captures the lion's share of the Model-View-Controller architecture that has been a part of the Smalltalk community for years.

Structure

Observer scheme

Subject represents the core (or independent or common or engine) abstraction. Observer represents the variable (or dependent or optional or user interface) abstraction. The Subject prompts the Observer objects to do their thing. Each Observer can call back to the Subject as needed.

Example

The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically. Some auctions demonstrate this pattern. Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and "observes" when a paddle is raised to accept the bid. The acceptance of the bid changes the bid price which is broadcast to all of the bidders in the form of a new bid.

Observer example

Check list
  1. Differentiate between the core (or independent) functionality and the optional (or dependent) functionality.
  2. Model the independent functionality with a "subject" abstraction.
  3. Model the dependent functionality with an "observer" hierarchy.
  4. The Subject is coupled only to the Observer base class.
  5. The client configures the number and type of Observers.
  6. Observers register themselves with the Subject.
  7. The Subject broadcasts events to all registered Observers.
  8. The Subject may "push" information at the Observers, or, the Observers may "pull" the information they need from the Subject.
Rules of thumb
  • Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility passes a sender request along a chain of potential receivers. Command normally specifies a sender-receiver connection with a subclass. Mediator has senders and receivers reference each other indirectly. Observer defines a very decoupled interface that allows for multiple receivers to be configured at run-time.
  • Mediator and Observer are competing patterns. The difference between them is that Observer distributes communication by introducing "observer" and "subject" objects, whereas a Mediator object encapsulates the communication between other objects. We've found it easier to make reusable Observers and Subjects than to make reusable Mediators.
  • On the other hand, Mediator can leverage Observer for dynamically registering colleagues and communicating with them.

 

Source:

http://sourcemaking.com/design_patterns/observer

Monday, March 30, 2009

Facade Pattern

Posted on/at 3:46 PM by Admin

 
  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface.
Problem

A segment of the client community needs a simplified interface to the overall functionality of a complex subsystem.

Discussion

Facade discusses encapsulating a complex subsystem within a single interface object. This reduces the learning curve necessary to successfully leverage the subsystem. It also promotes decoupling the subsystem from its potentially many clients. On the other hand, if the Facade is the only access point for the subsystem, it will limit the features and flexibility that "power users" may need.

The Facade object should be a fairly simple advocate or facilitator. It should not become an all-knowing oracle or "god" object.

Structure

Facade takes a "riddle wrapped in an enigma shrouded in mystery", and interjects a wrapper that tames the amorphous and inscrutable mass of software.

Facade scheme

SubsystemOne and SubsystemThree do not interact with the internal components ofSubsystemTwo. They use the SubsystemTwoWrapper "facade" (i.e. the higher level abstraction).

Facade scheme

Example

The Facade defines a unified, higher level interface to a subsystem that makes it easier to use. Consumers encounter a Facade when ordering from a catalog. The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to the order fulfillment department, the billing department, and the shipping department.

Facade example

Check list
  1. Identify a simpler, unified interface for the subsystem or component.
  2. Design a 'wrapper' class that encapsulates the subsystem.
  3. The facade/wrapper captures the complexity and collaborations of the component, and delegates to the appropriate methods.
  4. The client uses (is coupled to) the Facade only.
  5. Consider whether additional Facades would add value.
Rules of thumb
  • Facade defines a new interface, whereas Adapter uses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.
  • Whereas Flyweight shows how to make lots of little objects, Facade shows how to make a single object represent an entire subsystem.
  • Mediator is similar to Facade in that it abstracts functionality of existing classes. Mediator abstracts/centralizes arbitrary communications between colleague objects. It routinely "adds value", and it is known/referenced by the colleague objects. In contrast, Facade defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes.
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  • Facade objects are often Singletons because only one Facade object is required.
  • Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. While Facade routinely wraps multiple objects and Adapter wraps a single object; Facade could front-end a single complex object and Adapter could wrap several legacy objects.

Question: So the way to tell the difference between the Adapter pattern and the Facade pattern is that the Adapter wraps one class and the Facade may represent many classes?

Answer: No! Remember, the Adapter pattern changes the interface of one or more classes into one interface that a client is expecting. While most textbook examples show the adapter adapting one class, you may need to adapt many classes to provide the interface a client is coded to. Likewise, a Facade may provide a simplified interface to a single class with a very complex interface. The difference between the two is not in terms of how many classes they "wrap", it is in their intent.

source:

http://sourcemaking.com/design_patterns/facade

Factory Method

Posted on/at 3:44 PM by Admin

 

· Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

· Defining a "virtual" constructor.

· The new operator considered harmful.

Problem

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.

Discussion

Factory Method is to creating objects as Template Method is to implementing an algorithm. A superclass specifies all standard and generic behavior (using pure virtual "placeholders" for creation steps), and then delegates the creation details to subclasses that are supplied by the client.

Factory Method makes a design more customizable and only a little more complicated. Other design patterns require new classes, whereas Factory Method only requires a new operation.

People often use Factory Method as the standard way to create objects; but it isn't necessary if: the class that's instantiated never changes, or instantiation takes place in an operation that subclasses can easily override (such as an initialization operation).

Factory Method is similar to Abstract Factory but without the emphasis on families.

Factory Methods are routinely specified by an architectural framework, and then implemented by the user of the framework.

Structure

The implementation of Factory Method discussed in the Gang of Four (below) largely overlaps with that of Abstract Factory. For that reason, the presentation in this chapter focuses on the approach that has become popular since.

clip_image001

An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Unlike a constructor, an existing object might be reused, instead of a new object created. Unlike a constructor, factory methods can have different and more descriptive names (e.g. Color.make_RGB_color(float red, float green, float blue) andColor.make_HSB_color(float hue, float saturation, float brightness)

clip_image002

The client is totally decoupled from the implementation details of derived classes. Polymorphic creation is now possible.

clip_image003

Example

The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold.

clip_image004

Check list

1. If you have an inheritance hierarchy that exercises polymorphism, consider adding a polymorphic creation capability by defining a static factory method in the base class.

2. Design the arguments to the factory method. What qualities or characteristics are necessary and sufficient to identify the correct derived class to instantiate?

3. Consider designing an internal "object pool" that will allow objects to be reused instead of created from scratch.

4. Consider making all constructors private or protected.

Rules of thumb

· Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.

· Factory Methods are usually called within Template Methods.

· Factory Method: creation through inheritance. Prototype: creation through delegation.

· Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

· Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.

· The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.

· Some Factory Method advocates recommend that as a matter of language design (or failing that, as a matter of style) absolutely all constructors should be private or protected. It's no one else's business whether a class manufactures a new object or recycles an old one.

· The new operator considered harmful. There is a difference between requesting an object and creating one. The new operator always creates an object, and fails to encapsulate object creation. A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.

 

source:

http://sourcemaking.com/design_patterns/factory_method

Mediator Pattern

Posted on/at 3:41 PM by Admin

Intent
  • Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  • Design an intermediary to decouple many peers.
  • Promote the many-to-many relationships between interacting peers to "full object status".
Problem

We want to design reusable components, but dependencies between the potentially reusable pieces demonstrates the "spaghetti code" phenomenon (trying to scoop a single serving results in an "all or nothing clump").

Discussion

In Unix, permission to access system resources is managed at three levels of granularity: world, group, and owner. A group is a collection of users intended to model some functional affiliation. Each user on the system can be a member of one or more groups, and each group can have zero or more users assigned to it. Next figure shows three users that are assigned to all three groups.

Mediator example

If we were to model this in software, we could decide to have User objects coupled to Group objects, and Group objects coupled to User objects. Then when changes occur, both classes and all their instances would be affected.

An alternate approach would be to introduce "an additional level of indirection" - take the mapping of users to groups and groups to users, and make it an abstraction unto itself. This offers several advantages: Users and Groups are decoupled from one another, many mappings can easily be maintained and manipulated simultaneously, and the mapping abstraction can be extended in the future by defining derived classes.

Mediator example

Partitioning a system into many objects generally enhances reusability, but proliferating interconnections between those objects tend to reduce it again. The mediator object: encapsulates all interconnections, acts as the hub of communication, is responsible for controlling and coordinating the interactions of its clients, and promotes loose coupling by keeping objects from referring to each other explicitly.

The Mediator pattern promotes a "many-to-many relationship network" to "full object status". Modelling the inter-relationships with an object enhances encapsulation, and allows the behavior of those inter-relationships to be modified or extended through subclassing.

An example where Mediator is useful is the design of a user and group capability in an operating system. A group can have zero or more users, and, a user can be a member of zero or more groups. The Mediator pattern provides a flexible and non-invasive way to associate and manage users and groups.

Structure

Mediator scheme

Colleagues (or peers) are not coupled to one another. Each talks to the Mediator, which in turn knows and conducts the orchestration of the others. The "many to many" mapping between colleagues that would otherwise exist, has been "promoted to full object status". This new abstraction provides a locus of indirection where additional leverage can be hosted.

Mediator scheme

Example

The Mediator defines an object that controls how a set of objects interact. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other. The control tower at a controlled airport demonstrates this pattern very well. The pilots of the planes approaching or departing the terminal area communicate with the tower rather than explicitly communicating with one another. The constraints on who can take off or land are enforced by the tower. It is important to note that the tower does not control the whole flight. It exists only to enforce constraints in the terminal area.

Mediator example

Check list
  1. Identify a collection of interacting objects that would benefit from mutual decoupling.
  2. Encapsulate those interactions in the abstraction of a new class.
  3. Create an instance of that new class and rework all "peer" objects to interact with the Mediator only.
  4. Balance the principle of decoupling with the principle of distributing responsibility evenly.
  5. Be careful not to create a "controller" or "god" object.
Rules of thumb
  • Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility passes a sender request along a chain of potential receivers. Command normally specifies a sender-receiver connection with a subclass. Mediator has senders and receivers reference each other indirectly. Observer defines a very decoupled interface that allows for multiple receivers to be configured at run-time.
  • Mediator and Observer are competing patterns. The difference between them is that Observer distributes communication by introducing "observer" and "subject" objects, whereas a Mediator object encapsulates the communication between other objects. We've found it easier to make reusable Observers and Subjects than to make reusable Mediators.
  • On the other hand, Mediator can leverage Observer for dynamically registering colleagues and communicating with them.
  • Mediator is similar to Facade in that it abstracts functionality of existing classes. Mediator abstracts/centralizes arbitrary communication between colleague objects, it routinely "adds value", and it is known/referenced by the colleague objects (i.e. it defines a multidirectional protocol). In contrast, Facade defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes (i.e. it defines a unidirectional protocol where it makes requests of the subsystem classes but not vice versa).

Source:

 

http://sourcemaking.com/design_patterns/mediator

Visual Studio shortcut keys

Posted on/at 6:38 AM by Admin

 

You are familiar with many of Visual Studio's shortcut keys, but not all of them. Here is a handy reference that can make your .NET lifestyle easier and a lot more productive. Note: the 'must-know' shortcut keys are highlighted.

General

Shortcut

Description

Ctrl-X or
Shift-Delete

Cuts the currently selected item to the clipboard

Ctrl-C or
Ctrl-Insert

Copies the currently selected item to the clipboard

Ctrl-V or
Shift-Insert

Pastes the item in the clipboard at the cursor

Ctrl-Z or
Alt-Backspace

Undo previous editing action

Ctrl-Y or
Ctrl-Shift-Z

Redo the previous undo action

Ctrl-Shift-V or
Ctrl-Shift-Insert

Pastes an item from the clipboard ring tab of the Toolbox at the cursor in the file and automatically selects the pasted item. Cycle through the items on the clipboard by pressing the shortcut keys repeatedly

Esc

Closes a menu or dialog, cancels an operation in progress, or places focus in the current document window

Ctrl-S

Saves the selected files in the current project (usually the file that is being edited)

Ctrl-Shift-S

Saves all documents and projects

Ctrl-P

Displays the Print dialog

F7

Switches from the design view to the code view in the editor

Shift-F7

Switches from the code view to the design view in the editor

F8

Moves the cursor to the next item, for example in the TaskList window or Find Results window

Shift-F8

Moves the cursor to the previous item, for example in the TaskList window or Find Results window

Shift-F12

Finds a reference to the selected item or the item under the cursor

Ctrl-Shift-G

Opens the file whose name is under the cursor or is currently selected

Ctrl-/

Switches focus to the Find/Command box on the Standard toolbar

Ctrl-Shift-F12

Moves to the next task in the TaskList window

Ctrl-Shift-8

Moves backward in the browse history. Available in the object browser or Class View window

Alt-Left Arrow

Go back in the web browser history

Alt-Right Arrow

Go forward in the web browser history

clip_image001return to top

Text navigation

Shortcut

Description

Left Arrow

Moves the cursor one character to the left

Right Arrow

Moves the cursor one character to the right

Down Arrow

Moves the cursor down one line

Up Arrow

Moves the cursor up one line

Page Down

Scrolls down one screen in the editor window

Page Up

Scrolls up one screen in the editor window

End

Moves the cursor to the end of the current line

Home

Moves the cursor to the beginning of the line. If you press Home when the cursor is already at the start of the line, it will toggle the cursor between the first non-whitespace character and the real start of the line

Ctrl-End

Moves the cursor to the end of the document

Ctrl-Home

Moves the cursor to the start of the document

Ctrl-G

Displays the Go to Line dialog. If the debugger is running, the dialog also lets you specify addresses or function names to go to

Ctrl-]

Moves the cursor to the matching brace in the document. If the cursor is on an opening brace, this will move to the corresponding closing brace and vice versa

Ctrl-K, Ctrl-N

Moves to the next bookmark in the document

Ctrl-K, Ctrl-P

Moves to the previous bookmark

Ctrl-K, Ctrl-I

Displays Quick Info, based on the current language

Ctrl-Down Arrow

Scrolls text down one line but does not move the cursor. This is useful for scrolling more text into view without losing your place. Available only in text editors

Ctrl-Up Arrow

Scrolls text up one line but does not move the cursor. Available only in text editors

Ctrl-Right Arrow

Moves the cursor one word to the right

Ctrl-Left Arrow

Moves the cursor one word to the left

Ctrl-Shift-1

Navigates to the next definition, declaration, or reference of an item. Available in the object browser and Class View window. Also available in source editing windows if you have already used the Edit.GoToReference (Shift-F12) shortcut

Ctrl-Shift-2

Navigates to the previous definition, declaration, or reference of an item

clip_image001[1]return to top

Text manipulation

Shortcut

Description

Enter

Inserts a new line

Delete

Deletes one character to the right of the cursor

Insert

Toggles between insert and overtype insertion modes

Tab

Indents the currently selected line or lines by one tab stop. If there is no selection, this inserts a tab stop

Shift-Tab

Moves current line or selected lines one tab stop to the left

Backspace or
Shift-Backspace

Deletes one character to the left of the cursor

Ctrl-K, Ctrl-C

Marks the current line or selected lines of code as a comment, using the correct comment syntax for the programming language

Ctrl-K, Ctrl-U

Removes the comment syntax from the current line or currently selected lines of code

Ctrl-T or
Shift-Enter

Swaps the characters on either side of the cursor. (For example, AC|BD becomes AB|CD.) Available only in text editors

Ctrl-K, Ctrl-L

Removes all unnamed bookmarks in the current document

Ctrl-M, Ctrl-O

Automatically determines logical boundaries for creating regions in code, such as procedures, and then hides them. This collapses all such regions in the current document

Alt-Right Arrow or
Ctrl-Spacebar

Displays statement completion based on the current language or autocompletes word if existing text unambiguously identifies a single symbol

Ctrl-K, Ctrl-\

Removes horizontal whitespace in the selection or deletes whitespace adjacent to the cursor if there is no selection

Ctrl-K, Ctrl-F

Applies the indenting and space formatting for the language as specified on the Formatting pane of the language in the Text Editor section of the Options dialog to the selected text.

Ctrl-L

Cuts all selected lines or the current line if nothing has been selected to the clipboard

Ctrl-Shift-L

Deletes all selected lines or the current line if no selection has been made

Ctrl-Enter

Inserts a blank line above the cursor

Ctrl-Shift-Enter

Inserts a blank line below the cursor

Shift-Alt-T

Moves the line containing the cursor below the next line

Ctrl-J

Lists members for statement completion when editing code

Ctrl-U

Changes the selected text to lowercase characters

Ctrl-Shift-U

Changes the selected text to uppercase characters

Ctrl-Shift-Spacebar

Displays a tooltip that contains information for the current parameter, based on the current language

Ctrl-M, Ctrl-U

Removes the outlining information for the currently selected region

Ctrl-M, Ctrl-P

Removes all outlining information from the entire document

Ctrl-R, Ctrl-P

Swaps the anchor and endpoint of the current selection

Ctrl-M, Ctrl-L

Toggles all previously marked hidden text sections between hidden and display states

Ctrl-K, Ctrl-K

Sets or removes a bookmark at the current line

Ctrl-M, Ctrl-M

Toggles the currently selected hidden text section or the section containing the cursor if there is no selection between the hidden and display states

Ctrl-K, Ctrl-H

Sets or removes a shortcut in the tasklist to the current line

Ctrl-R, Ctrl-R

Enables or disables word wrap in an editor

Ctrl-R, Ctrl-W

Shows or hides spaces and tab marks

Ctrl-Delete

Deletes the word to the right of the cursor

Ctrl-Backspace

Deletes the word to the left of the cursor

Ctrl-Shift-T

Transposes the two words that follow the cursor. (For example, |End Sub would be changed to read Sub End|.)

clip_image001[2]return to top

Text selection

Shortcut

Description

Shift-Left Arrow

Moves the cursor to the left one character, extending the selection

Shift-Alt-Left Arrow

Moves the cursor to the left one character, extending the column selection

Shift-Right Arrow

Moves the cursor to the right one character, extending the selection

Shift-Alt-Right Arrow

Moves the cursor to the right one character, extending the column selection

Ctrl-Shift-End

Moves the cursor to the end of the document, extending the selection

Ctrl-Shift-Home

Moves the cursor to the start of the document, extending the selection

Ctrl-Shift-]

Moves the cursor to the next brace, extending the selection

Shift-Down Arrow

Moves the cursor down one line, extending the selection

Shift-Alt-Down Arrow

Moves the cursor down one line, extending the column selection

Shift-End

Moves the cursor to the end of the current line, extending the selection

Shift-Alt-End

Moves the cursor to the end of the line, extending the column selection

Shift-Home

Moves the cursor to the start of the line, extending the selection

Shift-Alt-Home

Moves the cursor to the start of the line, extending the column selection

Shift-Up Arrow

Moves the cursor up one line, extending the selection

Shift-Alt-Up Arrow

Moves the cursor up one line, extending the column selection

Shift-Page Down

Extends selection down one page

Shift-Page Up

Extends selection up one page

Ctrl-A

Selects everything in the current document

Ctrl-W

Selects the word containing the cursor or the word to the right of the cursor

Ctrl-=

Selects from the current location in the editor back to the previous location in the navigation history

Ctrl-Shift-Page Down

Moves the cursor to the last line in view, extending the selection

Ctrl-Shift-Page Up

Moves the cursor to the top of the current window, extending the selection

Ctrl-Shift-Alt-Right Arrow

Moves the cursor to the right one word, extending the column selection

Ctrl-Shift-Left Arrow

Moves the cursor one word to the left, extending the selection

Ctrl-Shift-Alt-Left Arrow

Moves the cursor to the left one word, extending the column selection

clip_image001[3]return to top

Project related

Shortcut

Description

Ctrl-Shift-B

Builds the solution

Ctrl-N

Displays the New File dialog. Note: files created this way are not associated with a project. Use Ctrl-Shift-A to add a new file in a project

Ctrl-Shift-N

Displays the New Project dialog

Ctrl-O

Displays the Open File dialog

Ctrl-Shift-O

Displays the Open Project dialog

Shift-Alt-A

Displays the Add Existing Item dialog

Ctrl-Shift-A

Displays the Add New Item dialog

Ctrl-Alt-Insert

Allows you to override base class methods in a derived class when an overridable method is highlighted in the Class View pane

clip_image001[4]return to top

Window manipulation

Shortcut

Description

Shift-Alt-Enter

Toggles full screen mode

Ctrl-+

Goes back to the previous location in the navigation history. (For example, if you press Ctrl-Home to go to the start of a document, this shortcut will take the cursor back to wherever it was before you pressed Ctrl-Home.)

Ctrl-Shift-+

Moves forward in the navigation history. This is effectively an undo for the View.NavigateBackward operation

Ctrl-F4

Closes the current MDI child window

Shift-Esc

Closes the current tool window

Ctrl-F2

Moves the cursor to the navigation bar at the top of a code view

Ctrl-Tab

Cycles through the MDI child windows one window at a time

Ctrl-F6,
Ctrl-Shift-Tab

Moves to the previous MDI child window

Alt-F6,
Ctrl-Shift-F6

Moves to the next tool window

Shift-Alt-F6

Moves to the previously selected window

F6

Moves to the next pane of a split pane view of a single document

Shift-F6

Moves to the previous pane of a document in split pane view

Ctrl-Pagedown

Moves to the next tab in the document or window (e.g., you can use this to switch the HTML editor from its design view to its HTML view

Ctrl-PageUp

Moves to the previous tab in the document or window

clip_image001[5]return to top

Control editor (designer)

Shortcut

Description

Ctrl-Down Arrow

Moves the selected control down in increments of one on the design surface


Down Arrow

Moves the selected control down to the next grid position on the design surface

Ctrl-Left Arrow

Moves the control to the left in increments of one on the design surface

Left Arrow

Moves the control to the left to the next grid position on the design surface

Ctrl-Right Arrow

Moves the control to the right in increments of one on the design surface

Right Arrow

Moves the control to the right into the next grid position on the design surface

Ctrl-Up Arrow

Moves the control up in increments of one on the design surface

Up Arrow

Moves the control up into the next grid position on the design surface

Tab

Moves to the next control in the tab order

Shift-Tab

Moves to the previous control in the tab order

Ctrl-Shift-Down Arrow

Increases the height of the control in increments of one on the design surface

Shift-Down Arrow

Increases the height of the control to the next grid position on the design surface

Ctrl-Shift-Left Arrow

Reduces the width of the control in increments of one on the design surface

Shift-Left Arrow

Reduces the width of the control to the next grid position on the design surface

Ctrl-Shift-Right Arrow

Increases the width of the control in increments of one on the design surface

Shift-Left Arrow

Increases the width of the control to the next grid position on the design surface

Ctrl-Shift-Up Arrow

Decreases the height of the control in increments of one on the design surface

Shift-Up Arrow

Decreases the height of the control to the next grid position on the design surface

clip_image001[6]return to top

Search and replace

Shortcut

Description

Ctrl-F

Displays the Find dialog

Ctrl-Shift-F

Displays the Find in Files dialog

F3

Finds the next occurrence of the previous search text

Ctrl-F3

Finds the next occurrence of the currently selected text or the word under the cursor if there is no selection

Shift-F3

Finds the previous occurrence of the search text

Ctrl-Shift-F3

Finds the previous occurrence of the currently selected text or the word under the cursor

Ctrl-D

Places the cursor in the Find/Command line on the Standard toolbar

Alt-F3, H

Selects or clears the Search Hidden Text option for the Find dialog

Ctrl-I

Starts an incremental search—after pressing Ctrl-I, you can type in text, and for each letter you type, VS.NET will find the first occurrence of the sequence of letters you have typed so far. This is a very convenient facility, as it lets you find text by typing in exactly as many characters as are required to locate the text and no more. If you press Ctrl-I a second time without typing any characters, it recalls the previous pattern. If you press it a third time or you press it when an incremental search has already found a match, VS.NET searches for the next occurrence.

Alt-F3, C

Selects or clears the Match Case option for Find and Replace operations

Alt-F3, R

Selects or clears the Regular Expression option so that special characters can be used in Find and Replace operations

Ctrl-H

Displays the Replace dialog

Ctrl-Shift-H

Displays the Replace in Files dialog

Ctrl-Shift-I

Performs an incremental search in reverse direction

Alt-F3, S

Halts the current Find in Files operation

Alt-F3, B

Selects or clears the Search Up option for Find and Replace operations

Alt-F3, W

Selects or clears the Match Whole Word option for Find and Replace operations

Alt-F3, P

Selects or clears the Wildcard option for Find and Replace operations

clip_image001[7]return to top

Help

Shortcut

Description

Ctrl-Alt-F1

Displays the Contents window for the documentation

Ctrl-F1

Displays the Dynamic Help window, which displays different topics depending on what items currently have focus. If the focus is in a source window, the Dynamic Help window will display help topics that are relevant to the text under the cursor

F1

Displays a topic from Help that corresponds to the part of the user interface that currently has the focus. If the focus is in a source window, Help will try to display a topic relevant to the text under the cursor

Ctrl-Alt-F2

Displays the Help Index window

Shift-Alt-F2

Displays the Index Results window, which lists the topics that contain the keyword selected in the Index window

Alt-Down Arrow

Displays the next topic in the table of contents. Available only in the Help browser window

Alt-Up Arrow

Displays the previous topic in the table of contents. Available only in the Help browser window

Ctrl-Alt-F3

Displays the Search window, which allows you to search for words or phrases in the documentation

Shift-Alt-F3

Displays the Search Results window, which displays a list of topics that contain the string searched for from the Search window.

Shift-F1

Displays a topic from Help that corresponds to the user interface item that has the focus

clip_image001[8]return to top

Debugging

Shortcut

Description

Ctrl-Alt-V, A

Displays the Auto window to view the values of variables currently in the scope of the current line of execution within the current procedure

Ctrl-Alt-Break

Temporarily stops execution of all processes in a debugging session. Available only in run mode

Ctrl-Alt-B

Displays the Breakpoints dialog, where you can add and modify breakpoints

Ctrl-Alt-C

Displays the Call Stack window to display a list of all active procedures or stack frames for the current thread of execution. Available only in break mode

Ctrl-Shift-F9

Clears all of the breakpoints in the project

Ctrl-Alt-D

Displays the Disassembly window

Ctrl-F9

Enables or disables the breakpoint on the current line of code. The line must already have a breakpoint for this to work

Ctrl-Alt-E

Displays the Exceptions dialog

Ctrl-Alt-I

Displays the Immediate window, where you can evaluate expressions and execute individual commands

Ctrl-Alt-V, L

Displays the Locals window to view the variables and their values for the currently selected procedure in the stack frame

Ctrl-Alt-M, 1

Displays the Memory 1 window to view memory in the process being debugged. This is particularly useful when you do not have debugging symbols available for the code you are looking at. It is also helpful for looking at large buffers, strings, and other data that does not display clearly in the Watch or Variables window

Ctrl-Alt-M, 2

Displays the Memory 2 window

Ctrl-Alt-M, 3

Displays the Memory 3 window

Ctrl-Alt-M, 4

Displays the Memory 4 window

Ctrl-Alt-U

Displays the Modules window, which allows you to view the .dll or .exe files loaded by the program. In multiprocess debugging, you can right-click and select Show Modules for all programs

Ctrl-B

Opens the New Breakpoint dialog

Ctrl-Alt-Q

Displays the Quick Watch dialog with the current value of the selected expression. Available only in break mode. Use this command to check the current value of a variable, property, or other expression for which you have not defined a watch expression

Ctrl-Alt-G

Displays the Registers window, which displays CPU register contents

Ctrl-Shift-F5

Terminates the current debugging session, rebuilds if necessary, and then starts a new debugging session. Available in break and run modes

Ctrl-Alt-N

Displays the Running Documents window that displays the set of HTML documents that you are in the process of debugging. Available in break and run modes

Ctrl-F10

Starts or resumes execution of your code and then halts execution when it reaches the selected statement. This starts the debugger if it is not already running

Ctrl-Shift-F10

Sets the execution point to the line of code you choose

Alt-NUM *

Highlights the next statement to be executed

F5

If not currently debugging, this runs the startup project or projects and attaches the debugger. If in break mode, this allows execution to continue (i.e., it returns to run mode).

Ctrl-F5

Runs the code without invoking the debugger. For console applications, this also arranges for the console window to stay open with a "Press any key to continue" prompt when the program finishes

F11

Executes code one statement at a time, tracing execution into function calls

Shift-F11

Executes the remaining lines of a function in which the current execution point lies

F10

Executes the next line of code but does not step into any function calls

Shift-F5

Available in break and run modes, this terminates the debugging session

Ctrl-Alt-V, T

Displays the This window, which allows you to view the data members of the object associated with the current method

Ctrl-Alt-H

Displays the Threads window to view all of the threads for the current process

F9

Sets or removes a breakpoint at the current line

Ctrl-F11

Displays the disassembly information for the current source file. Available only in break mode

Ctrl-Alt-W, 1

Displays the Watch 1 window to view the values of variables or watch expressions

Ctrl-Alt-W, 2

Displays the Watch 2 window

Ctrl-Alt-W, 3

Displays the Watch 3 window

Ctrl-Alt-W, 4

Displays the Watch 4 window

Ctrl-Alt-P

Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes

clip_image001[9]return to top

Object browser

Shortcut

Description

Alt-F12

Displays the Find Symbol dialog

Ctrl-F12

Displays the declaration of the selected symbol in the code

F12

Displays the definition for the selected symbol in code

Ctrl-Alt-F12

Displays the Find Symbol Results window

Ctrl-Alt-J

Displays the Object Browser to view the classes, properties, methods, events, and constants defined either in your project or by components and type libraries referenced by your project

Alt-+

Moves back to the previously selected object in the selection history of the object browser

Shift-Alt-+

Moves forward to the next object in the selection history of the object browser

clip_image001[10]return to top

Tool window

Shortcut

Description

Ctrl-Shift-M

Toggles the Command window into or out of a mode allowing text within the window to be selected

Ctrl-Shift-C

Displays the Class View window

Ctrl-Alt-A

Displays the Command window, which allows you to type commands that manipulate the IDE

Ctrl-Alt-T

Displays the Document Outline window to view the flat or hierarchical outline of the current document

Ctrl-Alt-F

Displays the Favorites window, which lists shortcuts to web pages

Ctrl-Alt-O

Displays the Output window to view status messages at runtime

F4

Displays the Properties window, which lists the design-time properties and events for the currently selected item

Shift-F4

Displays the property pages for the item currently selected. (For example, use this to show a project's settings.)

Ctrl-Shift-E

Displays the Resource View window

Ctrl-Alt-S

Displays the Server Explorer window, which allows you to view and manipulate database servers, event logs, message queues, web services, and many other operating system services

Ctrl-Alt-R

Displays the web browser window, which allows you to view pages on the Internet

Ctrl-Alt-L

Displays the Solution Explorer, which lists the projects and files in the current solution

Ctrl-Alt-K

Displays the TaskList window, which displays tasks, comments, shortcuts, warnings, and error messages

Ctrl-Alt-X

Displays the Toolbox, which contains controls and other items that can be dragged into editor and designer windows

clip_image001[11]return to top

Html editor (Design View)

Shortcut

Description

Ctrl-B

Toggles the selected text between bold and normal

Ctrl-Shift-T

Decreases the selected paragraph by one indent unit

Ctrl-T

Indents the selected paragraph by one indent unit

Ctrl-I

Toggles the selected text between italic and normal

Ctrl-Shift-K

Prevents an absolutely positioned element from being inadvertently moved. If the element is already locked, this unlocks it

Ctrl-G

Toggles the grid

Ctrl-Shift-G

Specifies that elements be aligned using an invisible grid. You can set grid spacing on the Design pane of HTML designer options in the Options dialog, and the grid will be changed the next time you open a document

Ctrl-U

Toggles the selected text between underlined and normal

Ctrl-Shift-L

Displays the Bookmark dialog

Ctrl-J

Inserts <div></div> in the current HTML document

Ctrl-L

When text is selected, displays the Hyperlink dialog

Ctrl-Shift-W

Displays the Insert Image dialog

Ctrl-Alt-Up Arrow

Adds one row above the current row in the table

Ctrl-Alt-Down Arrow

Adds one row below the current row in the table

Ctrl-Alt-Left Arrow

Adds one column to the left of the current column in the table

Ctrl-Alt-Right Arrow

Adds one column to the right of the current column in the table

Ctrl-Shift-Q

Toggles display of marker icons for HTML elements that do not have a visual representation, such as comments, scripts, and anchors for absolutely positioned elements

Ctrl-Page Down

Switches from design view to HTML view and vice versa

Ctrl-Q

Displays a 1-pixel border around HTML elements that support a BORDER attribute and have it set to zero, such as tables, table cells, and divisions

clip_image001[12]return to top

Macro

Shortcut

Description

Alt-F8

Displays the Macro Explorer window, which lists all available macros

Alt-F11

Launches the macros IDE

Ctrl-Shift-R

Places the environment in macro record mode or completes recording if already in record mode

Ctrl-Shift-P

Plays back a recorded macro

source:

http://www.dofactory.com/ShortCutKeys/ShortCutKeys.aspx

About Me

Developers house is a blog for posting technical articles in different technology like Microsft, Java, Oracle ..etc Microsoft technology includes c#,VB.net,ASP.net,Ajax,SilverLight,TFS,VS.NET 2003,2005,2008,2010 , SQL Server 2000, 2005 , Expression Blend , ...etc I hope it is helpful for all of you and if you are interested to post articles on it, only send me at ahmad.eed@gmail.com