In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to implement the prototype pattern in C++ and Java, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
GOF defines the prototype pattern as specifying the type of object to be created with prototype instances and creating new objects by copying these prototypes.
The clone () method is provided in both C++ and Java to clone objects, but the interface Cloneable must be implemented in Java.
Indicate the type of object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model pattern allows the dynamic increase or decrease of product classes, which do not need to have any pre-determined hierarchical structure, and the original model pattern is suitable for any hierarchical structure. The disadvantage is that each class must be equipped with a clone method.
1. Composition:
1) customer role: let a prototype clone itself to get a new object.
2) Abstract prototype role: implements its own clone method, the class that plays this role is usually an abstract class, and it has many concrete subclasses.
3) concrete prototype role: the copied object is a concrete subclass of the abstract prototype role.
2. UML class diagram:
Third, code implementation:
JAVA:
Prototype.java:
/ / prototype abstract class
Public abstract class Prototype implements Cloneable {
String name
Public void setName (String name) {
This.name = name
}
Public String getName () {
Return this.name
}
Public Object clone () {
Object object = null
Try {
Object = super.clone ()
} catch (CloneNotSupportedException exception) {
System.err.println ("AbstractPrototype is not Cloneable")
}
Return object
}
}
/ / prototype implementation class
Class ConcretePrototype1 extends Prototype {
Public ConcretePrototype1 () {
/ / TODO Auto-generated method stub
SetName ("ConcretePrototype1")
}
}
Class ConcretePrototype2 extends Prototype {
Public ConcretePrototype2 () {
/ / TODO Auto-generated method stub
SetName ("ConcretePrototype2")
}
}
TestMain.java:
Public class testMain {
Public static void main (String [] args) throws CloneNotSupportedException {
Prototype pt1 = new ConcretePrototype1 ()
Prototype pt2 = (Prototype) pt1.clone ()
Prototype pt3 = new ConcretePrototype2 ()
Prototype pt4 = (Prototype) pt3.clone ()
System.out.println ("1:" + pt1.getName ())
System.out.println ("2:" + pt2.getName ())
System.out.println ("3:" + pt3.getName ())
System.out.println ("4:" + pt4.getName ())
}
}
Output result:
1:ConcretePrototype1
2:ConcretePrototype1
3:ConcretePrototype2
4:ConcretePrototype2
Caterpillar:
Prototype.h:
/ *
* Prototype.h
*
* Created on: 2013-6-23
* Author: yan chao
, /
# ifndef PROTOTYPE_H_
# define PROTOTYPE_H_
/ / Virtual base class, the base class of all prototypes, provides Clone interface functions
Class Prototype
{
Public:
Prototype () {}
Virtual ~ Prototype () {}
Virtual Prototype* Clone () = 0
}
/ / derives from Prototype and implements the Clone method
Class ConcreatePrototype1: public Prototype
{
Public:
ConcreatePrototype1 ()
ConcreatePrototype1 (const ConcreatePrototype1&)
Virtual ~ ConcreatePrototype1 ()
Virtual Prototype* Clone ()
}
/ / derives from Prototype and implements the Clone method
Class ConcreatePrototype2: public Prototype
{
Public:
ConcreatePrototype2 ()
ConcreatePrototype2 (const ConcreatePrototype2&)
Virtual ~ ConcreatePrototype2 ()
Virtual Prototype* Clone ()
}
# endif / * PROTOTYPE_H_ * /
Prototype.cpp:
/ / =
/ / Name: Prototype.cpp
/ / Author: yan chao
/ / Version:
/ / Copyright: Your copyright notice
/ / Description: Hello World in Clippers, Ansi-style
/ / =
# include "Prototype.h"
# include
ConcreatePrototype1::ConcreatePrototype1 ()
{
Std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.