Design Mode C # description-simple factory Mode

Source: Internet
Author: User
Previous words: design patterns are one of the essence of the software development field. Learning design patterns well is a required course for every developer. At present, there are many books on the design model, among which there are better Chinese translations of gof, but they are not very suitable for beginners. Another is Java and pattern, which is suitable for beginners. It is strongly recommended here. However, the disadvantage of this book is that it is too cumbersome in some places. In many places, you only need to explain it briefly, but it is very expensive, which makes the book very thick and looks laborious. It is described in Java, which makes it difficult for some people who only know C. I am. net advocates, in order to read this book I specifically read some Java books, I feel that Java is more diverse than the book. net is much better, and many books are of high quality. Maybe it is the reason why Java is mature now. For convenience. net fans learn the design pattern. Here, I will take the learning notes from the book "Java and model" and re-describe it in C #, hoping to help beginners.
In fact, the design model is not a profound theory. I personally think it is not like what some people say: "I have never written 0.1 million. Code Do not talk about design patterns. "As long as you study and practice with your heart, you can fully master the design patterns.

The simple factory mode is the class creation mode, also called the static factory method mode. A factory class determines which product class instance to create based on the input parameters. Generally, three roles are involved (for example ):

Factory: this role is at the core of the factory method model and contains business logic closely related to applications. The factory class creates a product object under direct calls from the client. It is often implemented by a specific class.
Abstract Product role: the class that assumes this role is the parent class of the object created by the factory method mode, or the interfaces they share. It is generally implemented by an interface or abstract class.
Specific product role: Any pair created in the factory method mode
Instances of this role are implemented by specific classes.

Advantages and disadvantages of the simple factory model:
The core of the model is the factory class, which is responsible for the creation of products, and the client can avoid the responsibility for product creation, which achieves the division of responsibility. However, because the factory class integrates the creation logic of all products, if it fails to work properly, it will have a great impact on the system. To add a new product, you must modify the source code of the factory role.

The following describes the implementation of the pattern by using the gardener as an example:
Fruit fruit interface, specifying some common characteristics of Fruits
The apple class is derived from the fruit interface.
The strawberry class is derived from the fruit interface.
Fruitgardener gardeners are responsible for creating strawberries and apples.
When the client creates a fruit (Apple or strawberry object), it calls the factory method of the gardener class to create it. The UML diagram is as follows:

The Code is as follows:
Fruit. CS
Namespace simple_factory
{
Public interface fruit
{
// Growth
Void grow ();
// Gains
Void harvest ();
// Planting
Void plant ();
}
}
Apple. CS
Namespace simple_factory
{
Public class Apple: Fruit
{
Public Apple ()
{
}
# Region fruit Member
Public void grow ()
{
Console. writeline ("Apple is growing .......");
}
Public void harvest ()
{
Console. writeline ("Apple is harvesting .......");
}
Public void plant ()
{
Console. writeline ("Apple is planting .......");
}
# Endregion
}
}
Strawberry. CS
Namespace simple_factory
{
Public class strawberry: Fruit
{
Public strawberry ()
{
}
# Region fruit Member
Public void grow ()
{
Console. writeline ("Strawberry is growing .......");
}
Public void harvest ()
{
Console. writeline ("Strawberry is harvesting .......");
}
Public void plant ()
{
Console. writeline ("Strawberry is planting .......");
}
# Endregion
}
}
Fruitgardener. CS
Namespace simple_factory
{
Public class fruitgardener
{
// Static factory Method
Public static Fruit Factory (string which)
{
If (which. Equals ("apple "))
{
Return new Apple ();
}
Else if (which. Equals ("strawberry "))
{
Return new strawberry ();
}
Else
{
Return NULL;
}
}
}
}
Client. CS
Using system;
Namespace simple_factory
{
Class Client
{
[Stathread]
Static void main (string [] ARGs)
{
Fruit afruit = fruitgardener. Factory ("apple"); // creat Apple
Afruit. Grow ();
Afruit. Harvest ();
Afruit. Plant ();
Afruit = fruitgardener. Factory ("strawberry"); // creat strawberry
Afruit. Grow ();
Afruit. Harvest ();
Afruit. Plant ();
}
}
}
The output is as follows:
Apple is growing .......
Apple is harvesting .......
Apple is planting .......
Strawberry is growing .......
Strawberry is harvesting .......
Strawberry is planting .......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.