473,594 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design pattern .. factory i suspect

I'm going through modern C++ design looking for tips and while hi-tech
I suspect one solution to my issue would involve the factory design
pattern.

// algorithms.h
class Algorithms {
protected:
typedef std::deque<doub le> DDEQUE; // need to make this even more
generic to support floats .. i.e float and double
public:
virtual ~Algorithms() {};
virtual double mean1( const DDEQUE& queue );
virtual double mean2( const DDEQUE& queue );
// FFT and all sorts of funny math _stuff_
};

/////////// algorithms.hpp
#include "algorithms.hpp "
#include <iostream>
#include <numeric>

#ifndef NDEBUG
# define PRINT_CALLING_F UN() std::cout << __func__ << "() was called"
<< std::endl
#else
# define PRINT_CALLING_F UN()
#endif

double Algorithms::mea n1( const DDEQUE& queue )
{
double val = std::accumulate ( queue.begin(), queue.end(), 0. ) /
queue.size();
PRINT_CALLING_F UN();
return val;
}

double Algorithms::mea n2( const DDEQUE& queue )
{
double val = std::accumulate ( queue.begin(), queue.end(), 0. ) /
queue.size();
PRINT_CALLING_F UN();
return val;
}

I realize, there's no difference between mean1 and mean2 but they're
there for illustration purposes.

------------------------------------------------------------------------------------------------------
I've got two classes Ying and Yang.

Yang will use/is only interested in mean2 from Algorithm and Ying will
use/is only interested in mean1 from Algorithm. The current approach:

class Algorithms_Ying : public Algorithms {
public:
double mean1( const DDEQUE& queue ) {}
};

class Algorithms_Yang : public Algorithms {
public:
double mean2( const DDEQUE& queue ) {}
};

class Yang {
private:
typedef std::deque<doub le> DDEQUE;
Algorithms *alg;
public:
Yang(Algorithms *a) {alg = a;};
virtual ~Yang() {};
void test(const DDEQUE& q) { alg->mean2(q); }
};
class Ying {
private:
typedef std::deque<doub le> DDEQUE;
Algorithms *alg;
public:
Ying(Algorithms *a) {alg = a;};
virtual ~Ying() {};
void test(const DDEQUE& q) { alg->mean1(q); }
};

int main{}
{
Algorithms_Ying a1;
Algorithms_Yang a2;

Ying ying(&a1);
Yang yang(&a2);
}
That today seems silly and _appears_ like a maintenance nightmare.
What I'm trying to determine is a way to have (may not be saying this
right) a factory of algorithms, then have Ying/Yang or whomever go
inside and literally pull out what they want. You see, I receive from
the _outside_ world composite type (literally) YingStuff and
YangStuff.

enum ALGO { mean1, mean2, FFT, IFFT }; // etc etc
struct YingToDo { ALGO enum; };
struct YangToDo { ALGO enum; };

struct YingStuff { YingToDo todo[ 5 ] ; } ;
struct YangStuff { YingToDo todo[ 5 ] ; } ;

The tricky piece to this though surrounds the fact that I can't do -
for example - a mean1 on Yang. The point being the outside world will
need to ensure the YingStuff and YangStuff composites are filled out
properly. Simply put the factory I suspect could return an object that
contains the necessary functions and if I'm asked to do something I'm
can't do I wont do it.

The question. How would I achieve this? Source snippet or pseudo code
would help me better envision the design.
Thanks

Apr 2 '06 #1
0 1535

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3133
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
11
4292
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns. They are still online at http://www.fluffycat.com/java/patterns.html Since September 2003 I've mainly been using PHP, and now that PHP 5 is becoming more available I am going to try the same thing I did in Java with PHP.
2
1567
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site http://www.dofactory.com/Patterns/PatternAbstract.aspx). Could anybody try to explain me in his own words how this pattern work and what the purpose is? thanks in advance
0
311
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms { protected: typedef std::deque<double> DDEQUE; // need to make this even more generic to support floats .. i.e float and double public:
2
2283
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and ChildB) derived from class Base. Now i want to create an object, but i don't want to know which class to instantiate.
2
1393
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory pattern since I am dealing with EAN13, UPCA12 UPC8 etc. Can someone provide a small sample on how to start. I have looked at the factory implementation online but still can't figure it out. Can someone provide a sample jsut to start with.
10
2332
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. #include<iostream>
2
2559
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html. While I've read about implementing Data Access Objects by Factory Method Pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/images09/figure09_07.gif) and Abstract Factory Pattern...
4
5002
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8234
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6654
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.