//because you want to use façade mode, instantiate the façade class142Facade * M_facade =Newfacade ();143 //let customers, unified use of the façade, to unify to dispatch those devices, decoupling144M_facade->Activate ();145cout "On the Live!"Endl;146M_facade->Deactivate ();147 148 return 0;149}activating the device to start live !Camera Open !Rotate the camera by 90degrees!Lights Open !Display Open !in Live !turn off the device !Camera off !Rotate the camera by 0degrees!Light o
Public voidVISITELEMENTB (CONCRETEELEMENTB b) - { - + } - } + A Abstract classElement at { - Abstract Public voidAccept (Visitor v); - } - - classconcreteelementa:element - { in PublicVisitor Myvisitor; - Override Public voidAccept (Visitor v) to { +Myvisitor =v; - } the * Public voidOperationa () $ {Panax Notoginseng - } the + Public voiddosomewor
. println ("\ n \ N "); 17 18 // Liu Bei is happy, remove the second 19 system. out. println ("-------- Liu Bei is happy, remove the second --------"); 20 context = new context (New givengreenlight (); 21 context. operate (); // executes the second tip 22 system. out. println ("\ n"); 23 24 // What should I do if Sun Quan's sub-troops have followed suit? Remove the third 25 system. out. println ("-------- Sun Quan's small army chased, what should we do, split the third --------"); 26 context = n
Design Patterns-singleton patterns
The Singleton mode is an object creation mode. The Singleton mode ensures that only unique instance objects are generated for a class. That is to say, in the whole program space, the class only has one instance object.
This ensures that only one instance exists for a class and provides a globally unique access point for this c
Design Patterns-factory patterns
1. Abstract base class inheritance is used to construct the entire system based on the subordination between the abstract base class and the abstract base class to achieve a relatively clear class hierarchy.
2. Maintain scalability and minimum maintenance costs for the entire system.
Have I made it clear? Above code, ^ _ ^.
Design Patterns-abstractfactory patterns
1. Replace the abstract base class in the factory mode with the interface class. The difference is that the abstract base class cannot be instantiated, while the interface class can.
For example, replacing a car with a 4S shop,
Class car 4s stores{Public:Automobile 4S stores ();Vitrual ~ Automobile 4S stores ();
Virtual ti
Create bean Factory all useFactory methodFor the production of fixed products in the same grade structure support to increase any productAbstract FactoryAll products for the production of different product familiesAbstract product family can be added to the inability of the productPrototype mode (copy, clone)You need to implement the Cloneable interface using the Clone () method to copy a close () method such as a file streamCreating an object with new requires very tedious data preparation or
implementation is packaged into a class, if there are many alternative strategies, then the number of these classes is very large.To recognize a policy pattern from a code perspective:(Zhao Yun with three tricks to solve the problem, there are three elements in this scene: Three ingenious tricks, one capsule, one Zhao Yun)1. First create a policy interface (contains an algorithm that can be executed by each of the tips)2. Then write three implementation classes, because there are three clever t
of the current object */
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
ObjectOutputStream oos = new ObjectOutputStream (BOS);
Oos.writeobject (this);
/* read out the new object generated by the binary stream */
Bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ());
ObjectInputStream ois = new ObjectInputStream (bis);
return Ois.readobject ();
}
Public String getString () {
return string;
}
public void SetString (string string) {
, filterinputstream,filteroutputstream
Concretedecorate Specific Decorative roles: responsible for adding new responsibilities to component objects, Bufferedinputstream,bufferedoutputstream, etc.
Advantages
Extended object functionality is more flexible than inheritance and does not result in a dramatic increase in the number of classes.
You can decorate an object multiple times, creating a combination of different behaviors and getting more powerful objects.
The concre
observer;/** * Specific observer classes, such as LV Zeng in the example, Zhang Wei is an example of the Observer * @author WL * */public class Concretewatcher implements watcher { String Watcheredname; String Name;public concretewatcher (String name,string watcheredname) {this.name=name;this.watcheredname= Watcheredname;} @Overridepublic void Updata (String str) {System.out.println (name+ "Heart secretly happy" Hey!!! The opportunity came, my "+watcheredname+" said she ' "+str+" ' "");}Package
);/** * update Price */ Food.setprice (1.5f);}}third, the application scenarioGUI frame, meteorological observation, etc.Iv. SummaryBe sure to call the Setchanged () method before notifying all observers to set the state of the observer to be changed so that notifyobservers () will not be updated with the Observer Update method. If there are different opinions or omissions in the above content, please give us some valuable suggestions or comments.Finally, you can try to use the following UML mod
();
}
}
}
public class Mysubject extends Abstractsubject {
@Override
public void operation () {
System.out.println ("Update self!");
Notifyobservers ();
}
}
Test class:
public class Observertest {
public static void Main (string[] args) {
Subject sub = new Mysubject ();
Sub.add (New Observer1 ());
Sub.add (New Observer2 ());
Sub.operation ();
}
}
Output:Update self!Observer1 has rece
There are many similarities between structural patterns because there are several relationships in the object model structure: inheritance and implementation between classes, plus object combinations, which are the main purposes of the PHP design pattern.
The greatest advantage of the combination of objects is that you can build a relationship based on configuration at run time without modifying the actual
Adoption of design patterns in ANDROID-behavior patterns 1 responsibility chain model
The purpose of the responsibility chain mode is to allow multiple objects to process requests, so as to avoid coupling between request senders and receivers. Connect these objects into a chain and pass the request along the chain until an object processes it. So that multiple objects have the opportunity to process the re
What is strategy? For example, we want to go to a place to travel, can be based on the actual situation to choose the route of travel.1, the definition of the policy model
If you don't have time but don't care about money, you can choose to take a plane.If there is no money, you can choose to take the bus or train.If you are poorer, you can choose to ride a bike.In the program design, we also often encounter a similar situation, to achieve a certain
Command mode, which is the mechanism that encapsulates a common operation.
If you are familiar with C or PHP, you may have encountered the command, which is equivalent to a callback (callback) in the program. Callbacks typically use a function pointer or data structure such as a string and array implementation in PHP, which is an abstraction over a method invocation that absorbs all the object-oriented benefits: compositing, inheritance, and processing.
For example, the book "
object is often subject to drastic changes, however, the algorithms that combine sub-objects in each part are relatively stable. To put it simply, sub-objects change frequently and are relatively stable for the combined sub-object algorithm.This is a solution to the creation of a complex object. The changed part and the relatively stable part have been clarified. What we need to do is to isolate the change and how to isolate the sub-objects from the algorithm is a problem to be solved.In
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.