Simple factory and module factory for design module

Source: Internet
Author: User

Simple factory and module factory for design module

Simple factory mode: From the Perspective of the name, the factory is a production product. In java, it must be a creation object. In java, we can create a new object, why should I use the factory to go to the new object? Let's take a look at the simple factory model and try again to explain this problem.


Definition of the simple factory mode: Provides the function of creating instance merit objects without worrying about its specific implementation. The type of the instance to be created can be interfaces, abstract classes, and specific classes.





Design Mode simple factory Mode

A factory is the place where production instances are located. It is simple to name it createInstance () directly (). This method is usually static and includes parameters and return values. For example, if cattle, sheep, horses, and dogs all inherit the animal class, then the return value of createInstance () should be an animal (because the factory is used to produce animals, so the return value should also be an animal), and the parameter should be an animal name (so that the factory knows which animal you want to produce based on the name ). In this way, an animal instance can be generated based on the input animal name. CreateInstance implementation: switch (animal name) case BULL: return new ox (); case goat: return new goat (); case horse: return new horse (); case dog: return new dog ();

C # What is the difference between an abstract factory and a simple factory? Master

Design Mode C # description-simple factory Mode

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 pattern is not a profound theory. I personally think it is not like some people saying that "do not talk about the design pattern if you haven't written 0.1 million code ", as long as you learn and practice with your heart, you can fully master it.

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 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 (& quo ...... remaining full text>

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.