Object-oriented: Details of factory design patterns, and Object-Oriented Design Patterns

Source: Internet
Author: User

Object-oriented: Details of factory design patterns, and Object-Oriented Design Patterns

Factory Design Model

As a matter of fact, we all know a factory: it can produce different models of the same type of products. Now I am a product that requires help from the factory. I just need to tell the factory, which product do I need, regardless of how the product is implemented. What I care about is to provide me with a factory product.

I will explain the factory model with a simple example of C # (the idea of the design model is to solve the reusability problem and abstract problem ):

Public class _ base
{
Public virtual DateTime get_date ()
{
Return DateTime. Now;
}
}
// The result is obtained based on days.
Public class base_1: _ base
{
Int set_day;
Public base_1 (int date)
{
Set_day = date;
}
Public override DateTime get_date ()
{
Return DateTime. Now. AddDays (set_day );
}
}
// Obtain the time based on the month.
Public class base_2: _ base
{
Int set_day;
Public base_2 (int month)
{
Set_day = month;
}
Public override DateTime get_date ()
{
Return DateTime. Now. AddMonths (set_day );
}
}
// Obtain the week-based time
Public class base_3: _ base
{
Int set_day;
Public base_3 (int week)
{
Set_day = week * 7;
}
Public override DateTime get_date ()
{
Return DateTime. Now. AddMonths (set_day );
}
}
// Obtain the time based on the year
Public class base_4: _ base
{
Int set_day;
Public base_4 (int year)
{
Set_day = year;
}
Public override DateTime get_date ()
{
Return DateTime. Now. AddYears (set_day );
}
}
// Get the past time based on the hour
Public class base_5: _ base
{
Int set_day;
Public base_5 (int hours)
{
Set_day = hours;
}
Public override DateTime get_date ()
{
Return DateTime. Now. AddHours (set_day );
}
}
Public class expiration date
{
Private _ base nw;

Public 1_date (int type, int date)
{
Switch (type)
{
Case 1:
{
Nw = new base_1 (date );
Break;
}
Case 2:
{
Nw = new base_2 (date );
Break;
}
Case 3:
{
Nw = new base_3 (date );
Break;
}
Case 4:
{
Nw = new base_4 (date );
Break;
}
Case 5:
{
Nw = new base_5 (date );
Break;
}
}
}
Public DateTime get_date ()
{
Return nw. get_date ();
}
}

It is a time factory class. I don't care whether C #, C ++, or java is used. The important thing is thinking. I am also a learner. I still have little experience in this field and I am also a learner, learn to make yourself happy.

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.