instance creating instances - c++

How can I create an instance which creates as many instances as I want?
I think I have to create a class Manager for example and inside that class with an aggregation relationship to create the class name salary and bottles.
I want to create an instance of Manager which creates as many instances of bottle and salary I want. How can I do that?

It's called a factory and it looks something like:
class Factory {
Product create(int n);
// ...
}
class Product {
// ...
}
class Prod1 : public Product {
// ...
}
int main() {
Factory factory = Factory();
Product prod[10] = factory.create(10);
// ...
with create simply returning a Product object of some derived type. Of course, there's usually some context passed into the Factory::create function to hint at the type of Product you want.

Use pointers. You can have a pointer which points to as many instances as you want and new them whenever you want.

Related

How does this query method work?

#Query("MATCH (m:Movie)<-[r:ACTED_IN]-(a:Person) RETURN m,r,a LIMIT {limit}")
Collection<Movie> graph(#Param("limit") int limit);
For this query, it's returning "RETURN m, r, a", which is a full subgraph with 3 elements. Then why the return value of 'graph' method is a collection of "Movie" only? Where is the 'r, a' which is also returned.
I am trying to understanding the mechanism behind the scene.
It seems that you have a #RelationshipEntity defined in your class path but do not use it when defining #Relationships in the domain classes.
Sample:
#NodeEntity
class Pet {
// ...
}
#NodeEntity
class Person {
#Relationship(type = "HAS")
private List<Pet> pets;
// ...
}
#RelationshipEntity(type = "HAS")
class HasRelationship {
// ...
}
If Neo4j OGM, that acts behind the scenes of SDN, finds a relationship type, it looks for #RelationshipEntity first and if it finds them, tries to map the returned types back to the #NodeEntity. In this case OGM finds HasRelationship and wants to map it to the Person class. This fails because Person does only know of Pet and the objects get discarded.
Like my answer on GitHub.

Pre-constructor initialization

My problem is like this, I have a class named "Product" and another class named "Agriculture", the "Agriculture" class is inheriting the "Product" class.
When I summon the "Agriculture" constructor obviously the "Product" constructor is summoned first.
The question is, can I initialize one of the product's members via a set method first?
If you have:
class Product { ... };
class Agriculture : public Product { ...};
you can't escape the standard rule that the base object is constructed before the derived object. You have no chance to intervene in this order, nor set anything in Product before it's constructor starts.
Recommendation:
The best design for your need would be to foresee a Product constructor that takes as additional parameter(s) the value(s) that you want to set:
class Product {
string origin;
public:
Product () : origin("tbd") { }
Product (string withorigin) { ...}
void setOrigin (string myorigin) { origin=myorigin; }
};
class Agriculture : public Product {
public:
Agriculture () : Product ("Earth") { ...}
};
Workaround:
If such design would not fit your needs, the only thing you could imagine, would be to have a static member in Product. This member would then be independent of any Product, and could thus be set before an object is constructed.
class Product {
static string defaultCurrency;
string currency;
public:
Product () : currency(defaultCurrency) { ... }
static void setDefaultCurrency (string cur) { defaultCurrency=cur; }
};
class Agriculture : public Product { ... };
int main() {
Product::setDefaultCurrency("EUR");
Agriculture a1;
}
It's more error prone: the construction result depends on order of operations not related to the construction. This could be a problem for example in case of multithreading, if several threads construct objects at same moment.
Product constructor is called firstly, and you set some values inside this constructor. So why you still want to initialize one of the product's members via a set method first?

mock domain class created by another domain class with spock

I'm trying to test a domain class called EnityContact. Inside that class there is a method called initialize which populates some fields when needed. in order to do that the method creates instances of some other domain classes: AisUser, Entity and CPerson. AisUser is the domain class returned by the call to SecurityUtil.retrieveCurrentAisUser(false).
class EntityContact extends BaseObject implements Initializable{
....
#Override
void initialize() {
println "initaliazing"
isMain = false
creationDate = new Date()
createdBy = CPerson.get(SecurityUtil.retrieveCurrentAisUser(false).id)
entity = new Entity()
entity.setId(Long.valueOf(0)) //Id has to be initialized with some value
}
}
What i am trying to do is find a way to return mocks of those classes that i define in my specification.
Any ideas?
In Groovy you can mock static methods using MetaClass.
SecurityUtil.metaClass.'static'.retrieveCurrentAisUser = { boolean param ->
}

Partial Mock or new class or what else?

I have a question about testing.
I have a class that returns anomalies. in this class I have two different method that simply returns two different types of anomalies and one that return all anomalies (of both types)
this is the example code:
public interface IAnomalyService
{
IList<Anomaly> GetAllAnomalies(object parameter1, object parameter2);
IList<Anomaly> GetAnomalies_OfTypeA(object parameter1);
IList<Anomaly> GetAnomalies_OfTypeB(object parameter2);
}
public class AnomalyService : IAnomalyService
{
public IList<Anomaly> GetAllAnomalies(object parameter1, object parameter2)
{
var lstAll = new List<Anomaly>();
lstAll.AddRange(GetAnomalies_OfTypeA(parameter1));
lstAll.AddRange(GetAnomalies_OfTypeB(parameter2));
return lstAll;
}
public IList<Anomaly> GetAnomalies_OfTypeA(object parameter1)
{
//some elaborations
return new List<Anomaly> { new Anomaly { Id = 1 } };
}
public IList<Anomaly> GetAnomalies_OfTypeB(object parameter2)
{
//some elaborations
return new List<Anomaly> { new Anomaly { Id = 2 } };
}
}
class Anomaly
{
public int Id { get; set; }
}
I've created the tests for the two method that retrieve the anomalies of type A and type B (GetAnomalies_OfTypeA and GetAnomalies_OfTypeB).
Now I want to test the function GetAllAnomalies but I'm not sure what I have to do.
I think I have to way for testing it:
1) declare GetAnomalies_OfTypeA and GetAnomalies_OfTypeB in class AnomalyService as virtual, make a mock of the Class AnomalyService, and using Moq I can set CallBase as true and mock the two method GetAnomalies_OfTypeA and GetAnomalies_OfTypeB.
2)move the method GetAllAnomalies in another class called AllAnomalyService (with interface IAllAnomalyService) and in its constructor I will pass an interface of IAnomalyService and after I can test the GetAllAnomalies mocking the IAnomalyService interface.
I'm new at unit testing, so I don't know which solution is better, if is one of the mines or another one.
Can you help me?
thank you
Luca
Mocking is a good tool when a class resists testing. If you have the source, mocking is often not necessary. Try this approach:
Create a factory which can return AnomalyServices with various, defined anomalies (only type A, only type B, both, none, only type C, ...)
Since the three types are connected in some way, you should check all three in each test. If only anomalies of type A are expected, you should check that GetAllAnomalies returns the same result as GetAnomalies_OfTypeA and GetAnomalies_OfTypeB returns an empty list.

How to use a variable from another class

I have a class Overview where i try to save a Customer.
Now i want to use that Customer in another class. Now i'm using Public Static value, but my teacher said it's not good to use static variables. Can you solve this
public class OverView {
public static Customer CurrentCustomer;
CurrentCustomer = new Customer("Tom",23);
}
public class removeCustomer{
Customer removeCustomer = OverView.CurrentCustomer;
}
Your teacher is right, do not interface with static variables directly, implement getter/setter methods
See http://en.wikipedia.org/wiki/Mutator_method for more information!
Even better: in your example, you don't need to touch the instance of Customer at all. The "remove" functionality should be a member method on the Customer class. I'm not even sure that you need currentCustomer to be static, but I kept it static.
public class Customer {
//Customer constructor, etc.
* * *
public void remove() {
//remove the customer, whatever that entails
}
}
public class OverView {
private static Customer currentCustomer;
public static void someMethod() {
currentCustomer = new Customer("Tom",23);
* * *
//all done with this customer
currentCustomer.remove();
//but notice that the currentCustomer object still exists
}
}
You need an instance of Overview to access its non-static members. Try:
public class OverView {
public Customer CurrentCustomer = new Customer("Tom",23);
}
Public class removeCustomer{
OverView ov = new OverView();
Customer removeCustomer = ov.CurrentCustomer;
}
It is also adviseable to not declare the CurrentCustomer as public, and implement public get/set methods to access it