Design Patterns: a simple introduction from the big talk Design Patterns

Source: Internet
Author: User

Simple factory mode:

Originally,

Function A () {console. Log ("")}

Function B () {console. Log ("B ")}

Function C () {console. Log ("C ")}

Now,

VaR A = Factory ("");

A (); Output

VaR B = Factory ("B ");

B (); Output B

 

Rule mode:

Originally,

Function A () {console. Log ("")}

Function B () {console. Log ("B ")}

Function C () {console. Log ("C ")}

To Output A, A (), B, and C are similar.

Now,

Function context (){}

Context (a); Context (); Output

Context (B); Context (); Output B

Context (c); Context (); Output C

 

Development-closed Principle

That is, function extensions are developed and function modifications are closed.

Demand will change, but we cannot predict its changes. So when the change happens, we will take action. Abstract The change. For example, you are supposed to write an addition program. You have written a class to implement the addition function. Later, you need to add a subtraction function. You need to reconstruct this class and add an abstract operation class. The addition class inherits the operation class, And the subtraction class also inherits the operation class. In the future, similar division and multiplication will inherit this class without modifying the original class.

 

Dependency reversal Principle

Higher-order functions do not depend on lower-order functions. Both higher-order and lower-order functions depend on intermediate interfaces.

 

Decoration mode:

It turns out to be

Function (){}

Function B (){}

Function C (){}

C (B (A ("ABC ")));

Now,

C. setcomponet (B );

B. setcomponet ();

C ("ABC ");

 

Responsibility Chain Mode:

Originally,

Function (){}

Function B (){}

Function C (){}

C (B (A ("ABC ")));

Now,

A. setsuccessor (B );

B. setsuccessor (C );

A ("ABC ");

 

Proxy mode:

Originally,

Function A () {console. Log ("")}

A ();

Now,

Function proxy (){}

Proxy () is used to output string.

The difference is that the proxy function includes processing function A, that is, more processing can be performed on function.

 

Factory method mode:

Originally,

VaR A = Factory ("");

A (); Output

VaR B = Factory ("B ");

B (); Output B

Now,

VaR factory = ifacloud ();

VaR myfunction_a = factory. create_a ();

Myfunction_a (); Output string.

The difference is,

A simple project returns different methods based on input parameters, while a factory method returns different methods using different members instead of parameters.

 

Prototype:

Originally,

VaR A = new function_object ();

VaR B = new fanction_object ();

Now,

VaR A = new function_object ();

VaR B = A. Clone ();

The difference is,

The initialization overhead is reduced. Value replication and reference replication are also involved. The optical copy reference is not required, so the referenced object is also copied.

 

Template Method mode:

Originally,

Class testpaper
{
Public void testquestion1 ()
{
Console. writeline ("Yang once got it, and later gave Guo Jing the Xuan iron formed by Yi tianjian and Tu longdao []. ball Milling Cast Iron B. tinplate C. high-speed alloy steel D. carbon fiber ");
Console. writeline ("Answer:" + answer1 ());
}

Public void testquestion2 ()
{
Console. writeline ("Yang Guo, Cheng Ying, Lu wushuang wiped out the love flower, resulting in []. make this plant no longer harm B. extinction of a rare species C. destroying the ecological balance of the biosphere D. ");
Console. writeline ("Answer:" + answer2 ());
}

Public void testquestion3 ()
{
Console. writeline ("The Blue Phoenix makes Huashan shitu and taogu Liuxian vomit. If you are a doctor, what medicine will you give them? []. aspirin B. niuhuang Jiedu tablets C. d. let them drink a lot of raw milk E. none of the above ");
Console. writeline ("Answer:" + answer3 ());
}

Protected virtual string answer1 ()
{
Return "";
}

Protected virtual string answer2 ()
{
Return "";
}

Protected virtual string answer3 ()
{
Return "";
}

}
// Exam copied by student
Class testpapera: testpaper
{
Protected override string answer1 ()
{
Return "B ";
}

Protected override string answer2 ()
{
Return "C ";
}

Protected override string answer3 ()
{
Return "";
}
}
// Exam copied by student B
Class testpaperb: testpaper
{
Protected override string answer1 ()
{
Return "C ";
}

Protected override string answer2 ()
{
Return "";
}

Protected override string answer3 ()
{
Return "";
}

}

Appearance mode:

Originally,

A ();

B ();

C ();

Now,

Function facade (){

A ();

B ();

C ()

}

Facade ();

 

Builder mode:

Originally,

VaR thin = draw ();

Thin. A1 ();

Thin. A2 ();

Thin. A3 ();

Now,

VaR thin = draw ("thin ");

The difference is,

The execution sequence of A1, A2, and A3 is fixed, but the execution content in A1, A2, and A3 is changing at all times.

 

Observer mode:

Notification class, observer class,

Notification owner S. Add an observer (generate an observer (S, "x "));

Notification owner S. Add the observer (generate an observer (S, "Y "));

Notification recipient S. Notification content = "ABC ";

Notification by S. Notification ();

The execution result is,

The new status of observer X is ABC.

The new state of observer y is ABC.

 

Related Article

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.