public void send(String str) {
// TODO Auto-generated method stub
System.out.print("This a sms message:" + str);
}
}
Main test class
public class Main {
public static void main(String[] args){
ISendMethod sender = new SendEmail();
AbstractMessage aMessage = new UrgentMessage(sender);
aMessage.sendMessage("Test");
ISendMethod sender2 = new SendSms();
AbstractMessage aMessage2 = new Co
=NewDerivedabstraction_one (); theA.setimplementation (Newderivedimplementation_two ()); the returnA; the } the - Public Static intMain (string[] args) in { theClient C =NewClient (); theAbstraction A =c.setupmyparticularabstraction (); About the //From this on client code thinks it's talking to the the //abstraction, and won't need to be changed as the //derived abstractions is changed. + -
}", strdecoratorname); - } About } $ - - /// - ///Summary description for Client. A /// + Public classClient the { - Component Setup () $ { theConcretecomponent C =NewConcretecomponent ("This is the real component"); the theConcretedecorator d =NewConcretedecorator ("This was a decorator for the component"); the - d.setcomponent (c); in the returnD; the } About the Public Static intMain (string[] args) the
Operationwrapper carries out complex decision-making"); $Console.WriteLine ("which in turn results on calls to the subsystem classes");Panax Notoginseng C1. Operationx (); - if(1==1 /*some really complex decision*/) the { + C2. Operationy (); A } the //lots of complex code here ... + C3. Operationz (); - } $ $ } - - /// the ///Summary description for Client. - /// Wuyi Public classClient the { -
Design Mode Summary
Ii. Structural Mode
1, Adapter Mode
Convert a third-party class method to a method called in another class.
Advantages: Enable classes that are incompatible with the original interface and cannot work together, and reuse methods
Disadvantage: modifying the source class will cause trouble
Summary: it is mostly used for post-maintenance. It is used when similar classes, methods, or control
1. Explanation of the definition of the 1.1 yuan mode in the shared element mode (Structural)
The sharing technology is used to effectively support a large number of fine-grained objects.Share: Share the internal statusKey points of the 1.2 yuan Sharing Model
The focus is on changing and changing the separation.Divides the state of an object into internal and external States. The internal State remains unchanged and the external state changes.The inte
The structure mode, as its name implies, discusses the structure of classes and objects. It uses an Inheritance Mechanism to combine interfaces or implementations (class structure mode ),
Or you can combine some objects to implement new functions (object structure mode ).
There are seven common structural modes:
Adapter Mode
Composite Combination Mode
Facade appearance Mode
Decorator decoration Mode
Bridge Mode
Flyweight meta Mode
Proxy mode.
1. Adaptor (Adapter Mode)Definition: Jiuge two incompatible classes in a structured pattern that requires two identities for Adaptee(adapter) and Adapter(adapters)The goal is to eliminate compatibility issues with classes caused by interface mismatches.We often encounter the need to combine two non-relational classes together, the first way is to modify the respective interfaces, but violate the open and closed principleThe second approach is to use adapter to create a hybrid interface (half-br
1. Enjoy meta mode (Flyweight)Definition: The main purpose of the enjoy meta-mode is to realize the sharing of objects , that is, the shared pool , which can reduce the overhead of memory when there are many objects in the system, usually used in conjunction with the Factory mode .1.1 FactoryFactory: A pool is built in, and if it exists, it is taken out, otherwise the public class Factorypool {private hashtable1.2 Testingpublic class Test {public static void main (string[] args) {//TODO auto-gen
1. Combination mode (Composite)Definition: Combined mode is sometimes called part-the overall mode is convenient when dealing with problems like tree structureUse scene: Combine multiple objects together to manipulate, often used to represent a tree structure, such as a binary tree, number, etc., file directory.2. File Replication ExamplesFile class:Interface to the various operations of the file public interface fileoperate {void copy ();} File: Single document class file implements fileoper
Description:Build student information with structure, student information including number, name, score, build a chain of n studentsTable and outputs the linked list.Input:Enter student information at a time including number, name. 0 0 0 End the program.Sample Input:C1001 Li 70m1002 He 89E1003 Xie 83m1004 Wu 92E1005 Bao Sample output:c1001 Li 70m1002 He 89E1003 Xie 83m1004 Wu 92E1005 Bao 801#include 2#include 3 structStudent4 {5 Charnum[ -];6 Charname[ -];7 floatscore;8 structStu
existing classes, and the interfaces of this class do not meet the needs of the system.2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future. These source classes do not necessarily have very complex interfaces.3. (for object adapters) in the design, you need to change the interfaces of several existing subclasses, and if you use the class
Adapter mode is a structural design pattern
Design Intent: Transform the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
The pattern, like the name of the adapter, is to enable the old interface
, the subject of this book is still close to the object-oriented and design pattern theory. This book revolves around designing an example of a document editor that explains a variety of design patterns, such as creational patterns, Structural and behavioral patterns. Personal humble opinion, this book is the best design
words, the tank class does not need to know that the Decorator,decorator class is external to extend the tank class functionality.
The Decorator class behaves as an inheritance of the Is-a tank class on the interface, that is, decorator inherits the interfaces that the tank class has. In the implementation, it also shows the combination of has-a tank, that is, the decorator class uses another tank class. We can "decorate" a tank object with one or more decorator objects, and the decorated o
object. Even if they are staggered, we are not afraid.The key point and focus of the Bridge Mode lies in the principle of object-oriented design: prefer combination to inheritance. In fact, using multi-inheritance can solve these problems, but it is not recommended by us. This multi-inheritance often violates the single responsibility principle. Besides, C # does not support multi-inheritance, so it must inherit two interfaces. Abstract A and abstrac
say that I can understand it slowly. However, the design pattern still relies on practice. I can only say that I will try to see if I can use the design pattern in the future, of course, it's just to increase experience. Never change the design pattern.
After reading this, I feel that the structural model still n
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.