Design Pattern C # Description--Simple Factory mode

Source: Internet
Author: User
Design Preface: Design pattern is one of the essence of software development field. Learning the design pattern is a required course for every developer at present. At present, there are a lot of books about design mode, among which the better one is GOF, but it is not very suitable for beginners. There is also a "Java and mode", more suitable for beginners to use, in this strongly recommended. But the book's shortcomings are some places too cumbersome, a lot of places to simply explain it, but a large amount of ink, making books very thick, look laborious. And is described in Java, which makes some people who only know C # to start. I am a. NET supporter, in order to read this book I purposely read some Java books, I feel that Java in the diversity of books than. NET much better, and many books of high quality. It may be the reason Java is more mature now. For the convenience of. NET enthusiasts to learn design patterns, here I study the Java and mode of the book's learning notes issued, and in the C # language to recreate, hoping to help beginners.
In fact, the design model is not a profound theory, personal thought is not like some people say "did not write 100,000 code do not talk about design patterns", as long as the intention to learn and practice is fully able to grasp.

A simple factory pattern is a class creation pattern, also known as a static factory method pattern. Is that a factory class determines which instance of the product class is created based on the incoming parameters. Generally involves three kinds of roles (pictured below):

Factory class: This role is the core of the factory approach model, which contains business logic that is closely related to the application. The factory class creates the product object under the direct call of the client, which is often implemented by a specific class.
Abstract Product Role: The class that holds this role is the parent class of objects created by the factory method pattern, or the interfaces they share. Typically implemented by an interface or abstract class.
Specific product role: The factory method pattern creates any pair of
Elephants are examples of this role, implemented by specific classes.


Advantages and disadvantages of simple factory model:
The core of the pattern is the factory class, which is responsible for creating the product, and the client can dispense with the responsibility for creating the product, which enables the division of responsibility. However, because the factory class centralizes all product creation logic, it can have a significant impact on the system if it does not work properly. If you add new products, you must modify the source code for the factory role.

Take the gardener to grow fruit as an example to discuss the specific implementation of the model:
Fruit fruit interface that stipulates some common characteristics of fruit
Apple class derived from fruit interface
Strawberry Strawberry class derived from fruit interface
The Fruitgardener gardener is responsible for strawberry and apple creation work.
When the client wants to create the fruit (Apple or Strawberry object), call the factory method of the Gardener class to create: The UML diagram reads as follows:

The code is as follows:
Fruit.cs
Namespace Simple_factory
{
public interface Fruit
{
Growth
void grow ();
Harvest
void Harvest ();
Planting
void plant ();
}
}
Apple.cs
Namespace Simple_factory
{
public class Apple:fruit
{
Public Apple ()
{
}
#region Fruit Members
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 Members
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 to 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.