1

I am trying to implement a basic strategy pattern for understanding. I am new to programming. what am i doing wrong in the following code.

Can some one give a basic c implementation of strategy pattern.Thanks in adavance


#include <stdio.h>
#include <stdlib.h>

typedef int (*CustomerPriceStrategy)(int);
int bronzePriceStrategy(int);
int silverPriceStrategy(int);
int goldPriceStrategy(int);


struct Customer
{
 const char* name;
 CustomerPriceStrategy priceStrategy;

};
void placeOrder(struct Customer* customer)
{
    int a;
a=customer->priceStrategy(3);
printf("%d",a);

}

int main(void) {
    struct Customer *customer;
    customer->name="bronze";
    customer->priceStrategy=&bronzePriceStrategy;
    placeOrder(customer);
    return EXIT_SUCCESS;
}
int bronzePriceStrategy(int a)
{
 printf(" 40+ shipping");
 return (a+40);
}
int silverPriceStrategy(int a)
{
 printf(" 25+ shipping");
 return (a+25);
}
int goldPriceStrategy(int a)
{
 /* Free shipping for gold customers. */
 printf(" no shipping fee");
 return a;
}



madhu madi
  • 85
  • 8
  • Your functions are not using their argument `int a`. Also, `printf(" amount * 0.90")` does no multiplication, you might as well have used `puts()`. It is unclear why those functions always `return 3;` – Weather Vane Mar 05 '20 at 19:03
  • `struct Customer *customer;` is an *uninitiased* pointer. So `customer->name="bronze";` is undefined behaviour. – Weather Vane Mar 05 '20 at 19:08

1 Answers1

1
struct Customer *customer;

Is an uninialized pointer so:

customer->name="bronze";
customer->priceStrategy=&bronzePriceStrategy;

Will invoke undefined behavior.

You can replace this by:

struct Customer customer;
customer.name="bronze";
customer.priceStrategy=&bronzePriceStrategy;
placeOrder(&customer);
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Thanks, I got it. I allocated memory for the structure and then it worked. And This implementation is it a right strategy pattern implementation example. Well I did not do any logic behind. I wrote it just for some basic understanding of the pattern working. But I agree on your statement about a. – madhu madi Mar 05 '20 at 19:26
  • @madhumadi, glad to help, I appreciate your concern about using the best practices and patterns, that's the way to go, – anastaciu Mar 05 '20 at 19:32
  • It is not necessarily "wrong" to let a function parameter go unused. It may be indicative of a problem, and some compilers will warn about it, but occasionally there is a good reason to do it. – John Bollinger Mar 05 '20 at 19:40
  • On the other hand, the claim that the OP's strategy functions do not use their parameter is itself incorrect. Each one uses its parameter in its `return` statement. – John Bollinger Mar 05 '20 at 19:44
  • @JohnBollinger the question was edited, `a` was unused before. I admit that "wrong" is maybe too strong but I can't see the usefulness of having unused parameter in this instance. – anastaciu Mar 05 '20 at 20:06
  • @anastaciu, `int everythingCostsOneDollarStrategy(int a)`. – John Bollinger Mar 05 '20 at 20:09
  • @JohnBollinger, I'm intrigued. – anastaciu Mar 05 '20 at 20:17