How to write multiple classes in pseudocode - pseudocode

I have to write a report where I create and implement the Data Type (Set) using java.
I have to write pseudocode for it an include it in my report.
How do I write a class and call it from another pseudocode program.
say I have the Set class:
public class Set() {
public Set() {
//constructor
basicMethod();
}
public void basicMethod() {
//do something
}
}
and my driver class:
public class Driver {
public static void main(String[] args) {
Set s = new Set();
}
}
How would I write this as pseudocode?
How would instantiate a new object inside of another class?
how would i encompass the class entirely? To define a class in pseudocode would i just say:
Set {
// pseudocode in here
}
also, how would I write a constructor in pseudocode?

Related

How do I resolve MarkMembersAsStatic FxCop error?

I have a method ResetMethod(ClassA a) in a class and I have accessed this method by property of ResetMethod's class like this:
public class MyClass1
{
public MyClass1()
{
}
public void ResetMethod(ClassA a)
{
}
}
public class MyClass2
{
MyClass1 class1;
public MyClass2()
{
ClassA a= new ClassA();
MyClass1.ResetMethod(a);
}
public MyClass1 MyClass1
{
get
{
if (myClass1 == null)
myClass1 = new MyClass1 ();
return myClass1 ;
}
set
{
myClass1 = value;
}
}
}
While running FxCop rules, for method ResetMethod, it shows this error:
The 'this' parameter (or 'Me' in Visual Basic) of 'MyClass1.ResetMethod(MyClassA)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate.
How do I resolve this error?
You invocation of MyClass1.ResetMethod(a); is already calling a static method. So the code you posted does not compile to my understanding.
So all that's left to the do is to make the method itself static:
public static void ResetMethod(ClassA a)
{
// ...
}

how to check the call is from the which specific child controller?

I have two controller classes extending basecontroller. I have a common functionality to be implemented and implementation is in basecontroller.
public class BaseController{
protected void populateWidget(List li, int zipcode){
//implementation for child A
populate only 10 employee records
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
populateWidget(li, 90034)
}
I have written setChildB method and working on it. like below
public class BaseController{
protected boolean childB;
protected void setIsChildB(boolean childB){ this.childB = childB;}
protect boolean isChildB(){ return childB; }
protected void populateWidget(List li, int zipcode){
//implementation for child A
if(!isChildB())
populate only 10 employee records
else
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
setChildB(true); //setting as child B call
populateWidget(li, 90034)
}
Please advice the best way to do it.
I recommend implementing a Template Method Pattern. You could have your base controller like this:
public class BaseController {
protected abstract void getPeople(List li, int zipcode);
protected void populateWidget(List li, int zipcode) {
//Do generic logic here
//Refer to child
getPeople(List li, int zipcode)
//Do some more generic stuff
}
}
And then each of your children like this:
public class ChildA extends BaseController {
#Override
protected void getPeople(List li, int zipcode) {
//Specific Logic here
}
}
The children each override the getPeople() method to do their own thing.
That way if you need a ChildC, you can simply extend the parent without changing it.
Update: Based on the comments, here is an alternative way of doing this.
I would not do it as a setter, rather I would make a constructor argument on the base class that takes the type of child as an argument. This makes sure that the compiler enforces that you will always have a value set
You could use a boolean but I recommend strongly using an Enum, String constant or int constant.
The above two points are mostly because I always anticipate that I might have to do a ChildC when some requirements change comes along and at that point you won't have to do changes to the base class. It also falls into the set of patterns that other programmers I work with would expect and therefore makes the code easier readable and maintainable by others.
Sample base class:
public class BaseController {
private int childType;
protected static final int CHILD_A = 1;
protected static final int CHILD_B = 2;
public BaseController(int aChildType) {
childType = aChildType;
}
protected void populateWidget(List li, int zipcode) {
switch (childType) {
case CHILD_A:
//Handle Child A
case CHILD_B:
//Handle Child B
}
}
}
The child implementation would look like this:
public class ChildA extends BaseController {
public ChildA () {
super(CHILD_A);
}
//The rest of your code goes here...
}
Note, that in the above example I am using integer constants for the sake of brevity. In the 21s century Java creating an Enum would be the preferred way to go.

Can you create multiple instances of a class using method injection or some other form of injection?

public interface IFoo {}
public class Foo : IFoo {}
public sealed class NinjaModule : NinjectModule //Appropriately configured in project
{
public override void Load()
{
Bind<IFoo>.To<Foo>.InTransientScope();
}
}
public class SomeOtherClass : ISomeOtherInterface
{
public SomeOtherClass();
public IFoo GetFoo(IFoo foo)
{
return foo;
}
public void GetFoos()
{
foreach (var thing in everything)
{
var localFoo = GetFoo();
localFoo.UniqueProperty = "I am unique";
_fooList.Add(localFoo);
}
}
}
I need my code to look something like that.
Yes, I am fairly new to inject. I am fairly certain that I am missing a basic principle.
Thanks for any help.
I think the best approach here would be to use a factory to create the different Foo instances. And with Ninject and the Factory extension that's pretty easy.
public sealed class NinjaModule : NinjectModule //Appropriately configured in project
{
public override void Load()
{
Bind<IFoo>().To<Foo>().InTransientScope();
Bind<IFooFactory>().ToFactory();
}
}
public interface IFooFactory
{
IFoo CreateFoo();
}
public class SomeOtherClass : ISomeOtherInterface
{
private IFooFactory fooFactory;
public SomeOtherClass(IFooFactory fooFactory)
{
this.fooFactory = fooFactory;
}
public IFoo GetFoo(IFoo foo)
{
return this.fooFactory.CreateFoo();
}
public void GetFoos()
{
foreach (var thing in everything)
{
var localFoo = GetFoo();
localFoo.UniqueProperty = "I am unique";
_fooList.Add(localFoo);
}
}
}
This was a bad question. I figured out this needed to be implemented higher up by the class controlling these implementations.

Method table layout in clr 4.0

I have tow classes Class1 and Program a shown below:
class Class1
{
public void xx()
{
}
}
class Program : Class1
{
static void Main(string[] args)
{
}
}
But when i see the method table i not see the inherited public functions in Program class.
Why this is happened?I think inherited methods should be present in method table program class.

How to test Singleton class that has a static dependency

I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet as seen in the TekPub video. The class represents a cached list of reference data for dropdowns in an MVC 3 UI.
To get the list data the class calls a static method on a static class in my DAL.
Now I'm moving into testing an I want to implement an interface on my DAL class but obviously cannot because it is static and has only one static method so there's no interface to create. So I want to remove the static implementation so I can do the interface.
By doing so I can't call the method statically from the reference class and because the reference class is a singleton with a private ctor I can't inject the interface. How do I get around this? How do I get my interface into the reference class so that I can have DI and I can successfully test it with a mock?
Here is my DAL class in current form
public static class ListItemRepository {
public static List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
This is what I want it to look like
public interface IListItemRepository {
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository {
public List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
And here is my singleton reference class, the call to the static method is in the CheckRefresh call
public sealed class ListItemReference {
private static readonly Lazy<ListItemReference> instance =
new Lazy<ListItemReference>(() => new ListItemReference(), true);
private const int RefreshInterval = 60;
private List<ReferenceDTO> cache;
private DateTime nextRefreshDate = DateTime.MinValue;
public static ListItemReference Instance {
get { return instance.Value; }
}
public List<SelectListDTO> SelectList {
get {
var lst = GetSelectList();
lst = ReferenceHelper.AddDefaultItemToList(lst);
return lst;
}
}
private ListItemReference() { }
public ReferenceDTO GetByID(int id) {
CheckRefresh();
return cache.Find(item => item.ID == id);
}
public void InvalidateCache() {
nextRefreshDate = DateTime.MinValue;
}
private List<SelectListDTO> GetSelectList() {
CheckRefresh();
var lst = new List<SelectListDTO>(cache.Count + 1);
cache.ForEach(item => lst.Add(new SelectListDTO { ID = item.ID, Name = item.Name }));
return lst;
}
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = ListItemRepository.All(); // Here is the call to the static class method
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
}
You can use the singleton based on instance(not based on static), for which you can declare interface like this.
public interface IListItemRepository
{
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository
{
static IListItemRepository _current = new ListItemRepository();
public static IListItemRepository Current
{
get { return _current; }
}
public static void SetCurrent(IListItemRepository listItemRepository)
{
_current = listItemRepository;
}
public List<ReferenceDTO> All()
{
.....
}
}
Now, you can mock IListItemRepository to test.
public void Test()
{
//arrange
//If Moq framework is used,
var expected = new List<ReferneceDTO>{new ReferneceDTO()};
var mock = new Mock<IListItemRepository>();
mock.Setup(x=>x.All()).Returns(expected);
ListItemRepository.SetCurrent(mock.Object);
//act
var result = ListItemRepository.Current.All();
//Assert
Assert.IsSame(expected, result);
}
Which DI framework are you using? Depending on your answer, IOC container should be able to handle single-instancing so that you don't have to implement your own singleton pattern in the caching class. In your code you would treat everything as instanced classes, but in your DI framework mappings you would be able to specify that only one instance of the cache class should ever be created.
One way to test it would be if you refactor your ListItemReference by adding extra property:
public sealed class ListItemReference {
...
public Func<List<ReferenceDTO>> References = () => ListItemRepository.All();
...
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = References();
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
And then in your test you could do:
ListItemReference listReferences = new ListItemReference();
listReferences.References = () => new List<ReferenceDTO>(); //here you can return any mock data
Of course it's just temporary solution and I would recommend getting rid of statics by using IoC/DI.

Resources