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.