Get From C# Patterns
时间:2006-08-06 来源:netyang
Creational Patterns
With the foregoing description of objects, inheritance, and interfaces in
hand, we are now ready to begin discussing design patterns in earnest.
Recall that these are merely recipes for writing better object-oriented
programs. We have divided them into the Gang of Four’s three groups:
creational, structural and behavioral. We’ll start out in this section with the
creational patterns. All of the creational patterns deal with ways to create instances of objects.
This is important because your program should not depend on how objects
are created and arranged. In C#, of course, the simplest way to create an
instance of an object is by using the new operator. Fred fred1 = new Fred(); //instance of Fred class However, this really amounts to hard coding, depending on how you
create the object within your program. In many cases, the exact nature of
the object that is created could vary with the needs of the program, and
abstracting the creation process into a special “creator” class can make
your program more flexible and general. The Factory Method pattern provides a simple decision-making class
that returns one of several possible subclasses of an abstract base class,
depending on the data that are provided. We’ll start with the Simple
Factory pattern as an introduction to factories and then introduce the
Factory Method Pattern as well. The Abstract Factory pattern provides an interface to create and return
one of several families of related objects. The Builder pattern separates the construction of a complex object from
its representation so that several different representations can be created,
depending on the needs of the program. The Prototype pattern starts with an instantiated class and copies or
clones it to make new instances. These instances can then be further
tailored using their public methods. The Singleton pattern is a class of which there can be no more than one
instance. It provides a single global point of access to that instance.
hand, we are now ready to begin discussing design patterns in earnest.
Recall that these are merely recipes for writing better object-oriented
programs. We have divided them into the Gang of Four’s three groups:
creational, structural and behavioral. We’ll start out in this section with the
creational patterns. All of the creational patterns deal with ways to create instances of objects.
This is important because your program should not depend on how objects
are created and arranged. In C#, of course, the simplest way to create an
instance of an object is by using the new operator. Fred fred1 = new Fred(); //instance of Fred class However, this really amounts to hard coding, depending on how you
create the object within your program. In many cases, the exact nature of
the object that is created could vary with the needs of the program, and
abstracting the creation process into a special “creator” class can make
your program more flexible and general. The Factory Method pattern provides a simple decision-making class
that returns one of several possible subclasses of an abstract base class,
depending on the data that are provided. We’ll start with the Simple
Factory pattern as an introduction to factories and then introduce the
Factory Method Pattern as well. The Abstract Factory pattern provides an interface to create and return
one of several families of related objects. The Builder pattern separates the construction of a complex object from
its representation so that several different representations can be created,
depending on the needs of the program. The Prototype pattern starts with an instantiated class and copies or
clones it to make new instances. These instances can then be further
tailored using their public methods. The Singleton pattern is a class of which there can be no more than one
instance. It provides a single global point of access to that instance.
相关阅读 更多 +