Drink Remodeling Series [17]--Extract factory class

Source: Internet
Author: User

Overview

Creating an object in the program and setting the properties of the object is what we do for a long time. When you create an object that requires a lot of duplicate code, the code looks less elegant. From the perspective of the responsibility of the class, the business class must implement certain logic, but also responsible for the creation of objects, business class is a bit more. Object creation is also "one thing", we can extract "this matter" from the business code, let the special class to do "This matter", this specialized class is generally "factory class", so that the business class and factory class each other, code cleanliness to improve. This is the subject of this article-Extracting factory class.

Factory For example what all do PC maker

A PC manufacturer produces a series of laptops, and if all the parts of a laptop computer are produced by a PC manufacturer, the first batch of laptops has not yet been produced, and the PC maker has collapsed. Because it needs to develop its own CPU, graphics, memory, hard disk and other core hardware, these hardware need different teams to support, maintain and manage these teams is very time-consuming.

We think of the "class" as the "PC Manufacturer", all the hardware is developed by the PC manufacturer, meaning that the class has a lot of responsibilities, too much responsibility also means that this class of robustness is poor.
Even if the PC maker is producing all the other parts except the CPU, it will still not be able to produce a PC that works properly. PC-only manufacturer

So what would be more appropriate? This PC manufacturer to find their own position in the market, set up the brand of these notebooks, ready for pre-sales strategy, as for the CPU, graphics, memory, hard disk these core hardware, contact high-quality manufacturers, by them to supply. Then, take these manufacturers to provide hardware, in their own factories to produce assembled laptops, paste their own brand, and ultimately to the market.
The advantage of this is that the individual parts of the PC do not need to be developed by their own team, do not need their own production, they are responsible for contacting the right suppliers, from the supplier to produce these hardware, they only focus on brand design, PC assembly and marketing these links, which also makes their overall risk reduction.

This process consists of three parties: the Client (PC manufacturer), Factory (Hardware vendor), and product (hardware product), which can be represented in the following diagram.

From the point of view of hardware manufacturers, PC manufacturers are their customers, and hardware manufacturers provide PC parts for PC manufacturers.

Note: "Specificity" in this example refers to the PC manufacturer's focus on brand design, PC assembly, and marketing.

Example before refactoring

The new () method of the Policecarcontroller class contains all the logic for creating the Policecar

public class policecarcontroller{Public    policecar New (int mileage, bool servicerequired)    {        Policecar Policecar = new Policecar ();        policecar.servicerequired = servicerequired;        Policecar.mileage = mileage;        return policecar;}    } public class policecar{Public    bool Servicerequired {get; set;}    public int mileage {get; set;}}

If the properties of Policecar are many, the New () method will become very large, and the readability and maintainability of the code will be reduced. From the "duties of class" Perspective, the Policecarcontroller class assumed the responsibility of creating policecar, and we should take this responsibility out. After refactoring

The Policecarcontroller class is intuitive after refactoring, and the New () method is removed.
Now that it relies on the Ipolicecarfactory interface, Policecarcontroller doesn't need to know Policecar's creation details, and Policecar's creation logic is isolated from the factory interface.
This not only streamlines Policecarcontroller's duties, but also reduces the coupling between Policecarcontroller and Policecar.

In addition, the life cycle of the original Policecar object is managed by Policecarcontroller, and the refactoring is managed by the factory class.
By centralizing the management of the object's life cycle by the factory class, you can ensure that the program has consistent behavior (consistency).

Hide Code
public class policecarcontroller{    Public ipolicecarfactory policecarfactory {get; set;}    Public Policecarcontroller (Ipolicecarfactory policecarfactory) {policecarfactory = policecarfactory; } public policecar New (int mileage, bool servicerequired) {return policecarfactory.create (mileage, servicere    quired);    }}public class policecar{public bool servicerequired {get; set;} public int mileage {get; set;}} public interface ipolicecarfactory{Policecar Create (int mileage, bool servicerequired);}        public class policecarfactory:ipolicecarfactory{public policecar Create (int mileage, bool servicerequired) {        Policecar Policecar = new Policecar ();        policecar.servicerequired = servicerequired;        Policecar.mileage = mileage;    return policecar; }}

Note: When reading this code, consider it as a two-part piece.
Part of it is ipolicecarfactory and policecarfactory, and the other part is Policecarcontroller and Policecar.

Introduction to Simple Factory mode

We are familiar with the "factory model" There are three kinds, they are also known as the "factory model family", or "three brothers in the factory."

    • Simple Factory (Factory)
    • Factory approach (Factory method)
    • Abstract Factory Factory.

This article is used in the simple factory model, these three have many different places, the use of the scene is also different.
(as this article is not specifically designed to talk about design patterns, so I do not too much to introduce their differences and use of the scene)

Defined Simple Factory pattern:creates objects without exposing the instantiation logic to the client. Refers to the newly created object through a common interface.

The simple factory model consists of 3 parts: Customers, factories and products.
In a word can be summed up as: customer needs of the product should be created by the factory, product implementation details should not be exposed to the customer.
Users use the iphone is produced by Foxconn, the user corresponding to the client,iphone corresponding product, Foxconn corresponding to factory, users do not need to know how Foxconn production of the iphone.

In the simple Factory mode, you can return instances of different classes depending on the parameters. A simple factory pattern requires a class (or interface) specifically defined to create an instance of another class, and the instances that are created typically have a common parent class. Usage Scenarios

The simple factory model is suitable for the following scenarios:

    • The factory class is responsible for creating fewer objects: because fewer objects are created, it does not cause the business logic in the factory method to be too complex.
    • The client knows only the parameters of the incoming factory class, and does not care about how to create the object: The client does not need to care about the creation details, even the class names do not need to be remembered, just need to know the parameters of the type.
"Attention" Keepfool

Drink Remodeling Series [17]--Extract factory class

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.