Posts

Showing posts with the label oop

Accessing a private field from one class to another not related to the former class in C++ (OOP)

Accessing a private field from one class to another not related to the former class in C++ (OOP) I have 2 classes - A and B . Class A has a member list<int> and a method addData() that adds a number to this list. The second class B has a function displayContent() - it iterates through the list of the class A and prints its content (the added numbers). The problem I face is that I cannot access the private (cannot be changed to public) field list of class A from the newly instantiated object (from class B ). I would like to ask how can this be achieved with getters/setters , or a copy constructor ? A and B A list<int> addData() B displayContent() A private list A B getters/setters copy constructor //identifier 'a' is undefined? - you didn't pass an instance of A as a variable and B is not a child of A, so how can a be defined? – user4035 Jun 30 at 7:39 ...

Best design pattern to avoid instanceof when working with classes that cannot be modified?

Best design pattern to avoid instanceof when working with classes that cannot be modified? I am overriding a method provided by third-party library class and trying to record exceptions that occur: @Override someJavaMethod(arg1, arg2, Exception ex){ //Declare some variables with default values if (ex instanceof SpecificException1) //Set values for the variables else if (ex instanceof SpecificException2) //Set some other values for the variables else //Do nothing } The issue here is that SpecificException1 and SpecificException2 are both third-party exceptions and I cannot modify them. SpecificException1 SpecificException2 I understand that using instanceof is not a great way to handle this. What design pattern / OO principles I should use to handle this? instanceof 2 Answers 2 There are no fundamentally different ways of solving this (at least not in Ja...

Object Mapping without losing existing data

Object Mapping without losing existing data Usually Object Mappers are for scenarios to map a larger set of data to small set (example: Entity object has a lot of data but we want to return only a few of them). Entity Object mappers usually create a new Instance of destination object of small set and set the required fields from source object with larger set, but I have opposite scenario: I have a destination object which already contains some data and now I need to map a new source object with smaller data set into destination object. Source Class public class SrcObj { private String name; private int i; Destination Class public class DestObj { private String name; private int i; private float f; private boolean b; DesObj already contains the value of int i and float f, SrcObj has String name and int, i need to save SrcObj data into DestObj without losing existing data. Model Mapper always creating a new Object of DestObj which will map SrcObj into it but rest of the...