C # How to Use the decoration mode to change frogs

Source: Internet
Author: User
Document directory
  • The implementation process is to first define the system, and frogs are all animal types.
  • Then the swimming behavior and breathing behavior are abstracted as objects (not defined as interfaces here)
  • God is responsible for the process of creation and evolution

In the object-oriented theory, the emphasis is always on abstraction and combination, and some other object-oriented mechanisms such as inheritance, encapsulation, and polymorphism.

In the previous article, we will discuss what abstraction is. As for how to use abstraction, We will summarize the implementation and remove the characteristics from the common, so that we can obtain the abstract and implementation layers, therefore, most of the design patterns use this method to achieve object-oriented abstraction, so that the software can reduce the impact of changes.

 

We know that C # is a strong type of language, that is, the object behavior should be determined when being defined. If you want to use another behavior, or use the Inheritance Mechanism in the static definition period, another method is to combine (It is a dynamic mechanism that can add actions to objects during the program running. This creates individual differences and is consistent with our development concept. In fact, in the design process, it is a static and balanced process .)

 

Next we will talk about adding behavior to the object instance in a dynamic period.

The story is an animal, that is, a cockroach created by God. It may be able to swim after being born. Then, after a period of evolution, the frog child will be able to breathe.

It is not difficult to add behavior to an object instance dynamically in the implementation of JavaScript dynamic weak types. However, it is difficult to add behavior to an object instance in the static type mechanism C, however, C # currently supports dynamic attributes.

 

There is also a decorator mode in the design pattern, which aims to add behavior to the object.

 

 

The implementation process is to first define the system, and frogs are all animal types.

Namespace decoratorpattern
{
Abstract class animal
{
Public abstract void run ();
}
}

 

Namespace decoratorpattern
{
Class tadpole: Animal
{
Private animal;

Public tadpole (animal)
{
This. Animal = animal;
}

Public override void run ()
{
Console. writeline ("I Am a donkey ");
}
}
}

 

Namespace decoratorpattern
{

Class frog: Animal
{
Private animal;

Public Frog (animal)
{
This. Animal = animal;
}

Public override void run ()
{
Console. writeline ("I Am a frog ");
}
}
}

 

Then the swimming behavior and breathing behavior are abstracted as objects (not defined as interfaces here)

Namespace decoratorpattern
{
Class retriable: Animal
{
Private animal;

Public retriable (animal)
{
This. Animal = animal;
}

Public override void run ()
{
Animal. Run ();
Console. writeline ("I can swim ");
}
}
}

 

Namespace decoratorpattern
{
Class breathable: Animal
{
Private animal;

Public breathable (animal)
{
This. Animal = animal;
}

Public override void run ()
{
Animal. Run ();
Console. writeline ("I 've landed, I can breathe .");
}
}
}

 

 

God is responsible for the process of creation and evolution

In this way, the client does not see any changes in the background, and uses the static factory mode to hide the process of creation and evolution. In this way, our client code looks beautiful and smooth (haha, by the way, my code is also included ).

Namespace decoratorpattern
{
Class God
{
Public static tadpole createkedou ()
{
Return new tadpole (null );
}

Internal static retriable firstjinhua (animal Single)
{
Retriable = new retriable (New tadpole (single ));
Return retriable;
}

Internal static breathable secondjinhua (animal Single)
{
Breathable breathable = new breathable (new frog (single ));
Return breathable;
}
}
}

 

For specific source code, see the attachment. Tagpolebecomesfrog

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.