Preliminary Research on the Design Pattern-structure: because it is simple and beautiful

Source: Internet
Author: User

A good program has a public standard called high cohesion and low coupling. However, this is a matter of degree. The high cohesion cannot avoid communication and communication, and it is abstracted blindly, inheritance is unscientific. We should look for a better solution. In this kind of design model, our predecessors have already paved the way for us. Structure? What is a structure? It is a load-bearing part of the building, which means a skeleton. Only a good skeleton can make us work more effectively. In the object-oriented design, A class is a root of a bone. It connects these things with "relationships". How to deal with these relationships is something we need to learn in this design model! It's still the same. For specific examples, you can read a book. Here we will talk about two types of extended knowledge: Aggregation and combination (or synthesis) aggregation, which are weak relationships, for example, the football field and the audience are in an aggregation relationship. They are independent of each other. They only get together when there is a football match. Combination is a strong relationship. For example, people and limbs, people and limbs are an integral part, and they are not transferred by will. In programming, these two relationships help us solve the problem of too many inheritance! Specific Application: Aggregation [csharp] // abstract class Soft {public abstract void Run () ;}// Game class Game: Soft {public override void Run () {Console. writeLine ("Run a mobile game! ") ;}} // Address book class AdressList: Soft {public override void Run () {Console. WriteLine (" Run the phone address book! ") ;}}// Abstract Brand abstract class Brand {protected Soft soft; // set the software public void SetSoft (Soft soft) {this. soft = soft;} public abstract void Run ();} // Brand N class BrandN: Brand {public override void Run () {soft. run () ;}// Brand M class BrandM: Brand {public override void Run () {soft. run () ;}}2: Hashtable its Chinese name is a hash table. It controls the objects stored in it through the key value. It is a container that facilitates retrieval, quick search. This is simple to understand. Let's take a look at the application [csharp] using System; using System. collections; // when using Hashtable, you must introduce this namespace class hashtable {public static void Main () {Hashtable ht = new Hashtable (); // create a Hashtable instance // the key value is unique and the value can be repeated. ht. add ("E", "e"); // Add key/key value pair ht. add ("A", "a"); ht. add ("C", "c"); ht. add ("B", "B"); string s = (string) ht ["A"]; if (ht. contains ("E") // determines whether the hash table Contains a specific key. The return value is true or false Console. writeLine ("the E key: exist"); ht. remove ("C"); // Remove a key/key-value pair from the Console. writeLine (ht ["A"]); // output a ht here. clear (); // remove all elements from the Console. writeLine (ht ["A"]); // there will be no output here }}

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.